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. Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy: * No two friends can get balls of the same size. * No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. Input The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball. Output Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). Examples Input 4 18 55 16 17 Output YES Input 6 40 41 43 44 44 44 Output NO Input 8 5 972 3 4 1 4 970 971 Output YES Note In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: 1. Choose balls with sizes 3, 4 and 5. 2. Choose balls with sizes 972, 970, 971. Submitted Solution: ``` n = int(input()) t = list(set(map(int, input().split()))) t.sort() l = len(t) if l < 3: print("NO") exit() f = t[0] s = t[1] tr = False for i in range(2, len(t)): if t[i]-f <= 2: tr = True break else: f = s s = t[i] if tr: print("YES") else: print("NO") ```
instruction
0
97,465
14
194,930
Yes
output
1
97,465
14
194,931
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy: * No two friends can get balls of the same size. * No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. Input The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball. Output Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). Examples Input 4 18 55 16 17 Output YES Input 6 40 41 43 44 44 44 Output NO Input 8 5 972 3 4 1 4 970 971 Output YES Note In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: 1. Choose balls with sizes 3, 4 and 5. 2. Choose balls with sizes 972, 970, 971. Submitted Solution: ``` #!/usr/bin/env python3 import sys n = int(input()) t = sorted(list(set([int(x) for x in input().split()]))) for i in range(0, len(t) - 2): if t[i+1] - t[i] == 1 and t[i+2] - t[i+1] == 1: print('YES') sys.exit(0) print('NO') ```
instruction
0
97,466
14
194,932
Yes
output
1
97,466
14
194,933
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy: * No two friends can get balls of the same size. * No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. Input The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball. Output Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). Examples Input 4 18 55 16 17 Output YES Input 6 40 41 43 44 44 44 Output NO Input 8 5 972 3 4 1 4 970 971 Output YES Note In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: 1. Choose balls with sizes 3, 4 and 5. 2. Choose balls with sizes 972, 970, 971. Submitted Solution: ``` _ = input() balls = sorted(list(set(map(lambda x: int(x), input().split())))) for i in range(len(balls) - 2): a = balls[i] b = balls[i + 2] if b - a <= 2: print('YES') break else: print('NO') ```
instruction
0
97,467
14
194,934
Yes
output
1
97,467
14
194,935
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy: * No two friends can get balls of the same size. * No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. Input The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball. Output Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). Examples Input 4 18 55 16 17 Output YES Input 6 40 41 43 44 44 44 Output NO Input 8 5 972 3 4 1 4 970 971 Output YES Note In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: 1. Choose balls with sizes 3, 4 and 5. 2. Choose balls with sizes 972, 970, 971. Submitted Solution: ``` if __name__=='__main__': n = int(input()) b = list(map(int,input().split(' '))) b.sort() pos = False for i in range(len(b)-2): if len(set(b[i:i+3]))==3: if b[i+2]-b[i]<=2 and b[i+2]-b[i+1]<=1: pos = True if pos: break if pos: print('YES') else: print('NO') ```
instruction
0
97,468
14
194,936
No
output
1
97,468
14
194,937
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy: * No two friends can get balls of the same size. * No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. Input The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball. Output Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). Examples Input 4 18 55 16 17 Output YES Input 6 40 41 43 44 44 44 Output NO Input 8 5 972 3 4 1 4 970 971 Output YES Note In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: 1. Choose balls with sizes 3, 4 and 5. 2. Choose balls with sizes 972, 970, 971. Submitted Solution: ``` c=1; n=int(input()); l=sorted(map(int,input().split())) for i in range(n-1): if l[i+1]-l[i]==1:c+=1 else:c=1 if c==3:break print(["NO","YES"][c==3]) ```
instruction
0
97,469
14
194,938
No
output
1
97,469
14
194,939
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy: * No two friends can get balls of the same size. * No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. Input The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball. Output Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). Examples Input 4 18 55 16 17 Output YES Input 6 40 41 43 44 44 44 Output NO Input 8 5 972 3 4 1 4 970 971 Output YES Note In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: 1. Choose balls with sizes 3, 4 and 5. 2. Choose balls with sizes 972, 970, 971. Submitted Solution: ``` from collections import defaultdict, deque, Counter, OrderedDict def main(): n = int(input()) l = sorted([int(i) for i in input().split()]) check = False for i in range(2,n): a, b, c = l[i-2], l[i-1], l[i] check |= (a != b != c and b-a < 3 and c-a < 3) print("YES" if check else "NO") if __name__ == "__main__": """sys.setrecursionlimit(400000) threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start()""" main() ```
instruction
0
97,470
14
194,940
No
output
1
97,470
14
194,941
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy: * No two friends can get balls of the same size. * No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. Input The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball. Output Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). Examples Input 4 18 55 16 17 Output YES Input 6 40 41 43 44 44 44 Output NO Input 8 5 972 3 4 1 4 970 971 Output YES Note In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: 1. Choose balls with sizes 3, 4 and 5. 2. Choose balls with sizes 972, 970, 971. Submitted Solution: ``` i = int(input()) l = sorted(list(map(int, input().split()))) for x in range(0,i-2): if l[x]+1 == l[x+1]: if l[x] + 2 == l[x + 2]: print('YES') exit() print('NO') ```
instruction
0
97,471
14
194,942
No
output
1
97,471
14
194,943
Provide tags and a correct Python 3 solution for this coding contest problem. Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows: <image> In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. Input The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements. Output Print the only integer — the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
instruction
0
98,312
14
196,624
Tags: dp, two pointers Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) b = [] for i in range(n - 1): b.append(abs(a[i] - a[i + 1])) c = [] s = 1 summ = 0 for i in range(n - 1): summ += s * b[i] s = -s c.append(summ) c.sort() if c[0] < 0: print(c[n - 2] - c[0]) else: print(c[n - 2]) ```
output
1
98,312
14
196,625
Provide tags and a correct Python 3 solution for this coding contest problem. Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows: <image> In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. Input The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements. Output Print the only integer — the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
instruction
0
98,313
14
196,626
Tags: dp, two pointers Correct Solution: ``` n=int(input()) a=list(map(int,input().split())) da,p=[],1 for i in range(n-1): da.append(p*abs(a[i]-a[i+1]));p*=-1 m1,m2,s1,s2=0,0,0,0 for x in da: s1+=x if s1<0: s1=0 s2-=x if s2<0: s2=0 m1=max(m1,s1);m2=max(m2,s2) print(max(m1,m2)) ```
output
1
98,313
14
196,627
Provide tags and a correct Python 3 solution for this coding contest problem. Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows: <image> In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. Input The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements. Output Print the only integer — the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
instruction
0
98,316
14
196,632
Tags: dp, two pointers Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) b = [] sign = 1 for i in range(len(a)-1): b.append( abs(a[i]-a[i+1]) * sign ) sign *= -1 max_ = 0 max1, max2 = 0, 0 a1, a2 = 0, 0 for i in range(n-1): if a1+b[i]>0: a1 += b[i] else: a1 = 0 max1 = max(a1, max1) if a2-b[i]>0: a2 -= b[i] else: a2 = 0 max2 = max(a2, max2) print(max(max1,max2)) ```
output
1
98,316
14
196,633
Provide tags and a correct Python 3 solution for this coding contest problem. Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows: <image> In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. Input The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements. Output Print the only integer — the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
instruction
0
98,317
14
196,634
Tags: dp, two pointers Correct Solution: ``` def maxSubArray( A): if not A: return 0 curSum = maxSum = A[0] for num in A[1:]: curSum = max(num, curSum + num) maxSum = max(maxSum, curSum) return maxSum n = int(input()) L = list(map(int, input().split())) D1 = [] D2 = [] for i in range(1, len(L)): D1.append(abs(L[i] - L[i-1]) * (-1)**i) D2.append(abs(L[i] - L[i-1]) * (-1)**(i+1)) print(max(maxSubArray(D1), maxSubArray(D2))) ```
output
1
98,317
14
196,635
Provide tags and a correct Python 3 solution for this coding contest problem. Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows: <image> In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. Input The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements. Output Print the only integer — the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
instruction
0
98,318
14
196,636
Tags: dp, two pointers Correct Solution: ``` n = int(input()) num = list(map(int, input().split())) diff = [abs(j-i) for i, j in zip(num, num[1:])] s1 = 0 s2 = 0 m1 = 0 m2 = 0 for i, x in enumerate(diff): if s1 < 0: s1 = 0 if s2 < 0: s2 = 0 if i % 2 == 0: s1 += x s2 -= x else: s1 -= x s2 += x if s1 > m1: m1 = s1 if s2 > m2: m2 = s2 print(max(m1, m2)) ```
output
1
98,318
14
196,637
Provide tags and a correct Python 3 solution for this coding contest problem. Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows: <image> In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a. Input The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements. Output Print the only integer — the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
instruction
0
98,319
14
196,638
Tags: dp, two pointers Correct Solution: ``` def max_subarray(A): max_ending_here = max_so_far = A[0] for x in A[1:]: max_ending_here = max(x, max_ending_here + x) max_so_far = max(max_so_far, max_ending_here) return max_so_far n = int(input()) a = [int(v) for v in input().split()] b = [abs(a[i]-a[i+1])*(-1)**i for i in range(n-1)] print(max(max_subarray(b), max_subarray([-v for v in b]))) ```
output
1
98,319
14
196,639
Provide tags and a correct Python 3 solution for this coding contest problem. The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number of good pairs of topics. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of topics. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the interestingness of the i-th topic for the teacher. The third line of the input contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9), where b_i is the interestingness of the i-th topic for the students. Output Print one integer — the number of good pairs of topic. Examples Input 5 4 8 2 6 2 4 5 4 1 3 Output 7 Input 4 1 3 2 4 1 3 2 4 Output 0
instruction
0
98,758
14
197,516
Tags: binary search, data structures, sortings, two pointers Correct Solution: ``` from sys import * n=int(stdin.readline()) a=[int(x) for x in stdin.readline().split()] b=[int(x) for x in stdin.readline().split()] count=0 c=[] for i in range(n): c.append(a[i]-b[i]) c=sorted(c) i=0 j=n-1 while(i<j): if(c[i]+c[j]>0): count+=j-i j=j-1 else: i=i+1 print(count) ```
output
1
98,758
14
197,517
Provide tags and a correct Python 3 solution for this coding contest problem. The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number of good pairs of topics. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of topics. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the interestingness of the i-th topic for the teacher. The third line of the input contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9), where b_i is the interestingness of the i-th topic for the students. Output Print one integer — the number of good pairs of topic. Examples Input 5 4 8 2 6 2 4 5 4 1 3 Output 7 Input 4 1 3 2 4 1 3 2 4 Output 0
instruction
0
98,759
14
197,518
Tags: binary search, data structures, sortings, two pointers Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) c = [a[i]-b[i] for i in range(n)] c.sort() ind = n - 1 wyn = 0 for i in range(n): while True: if ind > i and c[i] + c[ind] > 0: ind -= 1 else: break if ind <= i: ind = min(n-1,i) wyn += (n-1-ind) print(wyn) ```
output
1
98,759
14
197,519
Provide tags and a correct Python 3 solution for this coding contest problem. The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number of good pairs of topics. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of topics. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the interestingness of the i-th topic for the teacher. The third line of the input contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9), where b_i is the interestingness of the i-th topic for the students. Output Print one integer — the number of good pairs of topic. Examples Input 5 4 8 2 6 2 4 5 4 1 3 Output 7 Input 4 1 3 2 4 1 3 2 4 Output 0
instruction
0
98,760
14
197,520
Tags: binary search, data structures, sortings, two pointers Correct Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) part = [] for i in range(n): part.append(a[i] - b[i]) part.sort() ans = 0 for i in range(n): if part[i] > 0: ans += n - i - 1 else: L = 0 R = n while L < R - 1: mid = (L + R) // 2 if part[mid] > abs(part[i]): R = mid else: L = mid ans += n - R print(ans) ```
output
1
98,760
14
197,521
Provide tags and a correct Python 3 solution for this coding contest problem. The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number of good pairs of topics. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of topics. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the interestingness of the i-th topic for the teacher. The third line of the input contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9), where b_i is the interestingness of the i-th topic for the students. Output Print one integer — the number of good pairs of topic. Examples Input 5 4 8 2 6 2 4 5 4 1 3 Output 7 Input 4 1 3 2 4 1 3 2 4 Output 0
instruction
0
98,761
14
197,522
Tags: binary search, data structures, sortings, two pointers Correct Solution: ``` import sys input = sys.stdin.readline from bisect import bisect_left n = int(input()) x = [*map(int, input().split())] y = [xi - yi for xi, yi in zip(x, map(int, input().split()))] #print(y) y.sort() ans = 0 for i in range(n): k = y[i] a = bisect_left(y, -k+1) ans += n-a print((ans - len([i for i in y if i > 0]))// 2) ```
output
1
98,761
14
197,523
Provide tags and a correct Python 3 solution for this coding contest problem. The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number of good pairs of topics. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of topics. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the interestingness of the i-th topic for the teacher. The third line of the input contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9), where b_i is the interestingness of the i-th topic for the students. Output Print one integer — the number of good pairs of topic. Examples Input 5 4 8 2 6 2 4 5 4 1 3 Output 7 Input 4 1 3 2 4 1 3 2 4 Output 0
instruction
0
98,762
14
197,524
Tags: binary search, data structures, sortings, two pointers Correct Solution: ``` n=int(input()) ans=i=0 j=n-1 diff=[] p=[] j=n-1 list1=list(map(int,input().split())) list2=list(map(int,input().split())) zip_object = zip(list1,list2) for list1_i, list2_i in zip_object: diff.append(list1_i-list2_i) #print(diff) diff.sort() #print(diff) while(i<j): if diff[i]+diff[j]>0: ans+=j-i j=j-1 else: i+=1 print(ans) ```
output
1
98,762
14
197,525
Provide tags and a correct Python 3 solution for this coding contest problem. The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number of good pairs of topics. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of topics. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the interestingness of the i-th topic for the teacher. The third line of the input contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9), where b_i is the interestingness of the i-th topic for the students. Output Print one integer — the number of good pairs of topic. Examples Input 5 4 8 2 6 2 4 5 4 1 3 Output 7 Input 4 1 3 2 4 1 3 2 4 Output 0
instruction
0
98,763
14
197,526
Tags: binary search, data structures, sortings, two pointers Correct Solution: ``` import collections import bisect def solve(n, teacherInterests, studentInterests): diffs = [] for t, s in zip(teacherInterests, studentInterests): diffs.append(t-s) diffs.sort() ans = 0 # print(diffs) for i, difference in enumerate(diffs): if difference <= 0: continue pos = bisect.bisect_left(diffs, -difference + 1) ans += i - pos return ans n = int(input().strip()) teacherInterests = list(map(int, input().strip().split())) studentInterests = list(map(int, input().strip().split())) print(solve(n, teacherInterests, studentInterests)) ```
output
1
98,763
14
197,527
Provide tags and a correct Python 3 solution for this coding contest problem. The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number of good pairs of topics. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of topics. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the interestingness of the i-th topic for the teacher. The third line of the input contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9), where b_i is the interestingness of the i-th topic for the students. Output Print one integer — the number of good pairs of topic. Examples Input 5 4 8 2 6 2 4 5 4 1 3 Output 7 Input 4 1 3 2 4 1 3 2 4 Output 0
instruction
0
98,764
14
197,528
Tags: binary search, data structures, sortings, two pointers Correct Solution: ``` def binarySearch(A,start,end,x): if start>end: return start mid=(start+end)//2 if A[mid]==x: return mid elif A[mid]>x: return binarySearch(A,start,mid-1,x) else: return binarySearch(A,mid+1,end,x) n=int(input()) A=[int(i) for i in input().split()] B=[int(i) for i in input().split()] pos=[] neg=[] for i in range(n): tmp=A[i]-B[i] if tmp>0: pos.append(tmp) else: neg.append(tmp) pos.sort() neg.sort() toplam=0 for i in range(len(neg)): tmp=-neg[i]+1 s=binarySearch(pos,0,len(pos)-1,tmp) if s<len(pos) and pos[s]==tmp: while(pos[s]==tmp and s>=0): s-=1 s+=1 toplam+=len(pos)-s toplam+=(len(pos)*(len(pos)-1))//2 print(toplam) ```
output
1
98,764
14
197,529
Provide tags and a correct Python 3 solution for this coding contest problem. The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number of good pairs of topics. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of topics. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the interestingness of the i-th topic for the teacher. The third line of the input contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^9), where b_i is the interestingness of the i-th topic for the students. Output Print one integer — the number of good pairs of topic. Examples Input 5 4 8 2 6 2 4 5 4 1 3 Output 7 Input 4 1 3 2 4 1 3 2 4 Output 0
instruction
0
98,765
14
197,530
Tags: binary search, data structures, sortings, two pointers Correct Solution: ``` def binary_search(arr, st, ed, comp): ans = -1 while st <= ed: mid = (st + ed) // 2 if arr[mid] + comp > 0: ans = mid ed = mid-1 else: st = mid+1 return ans n = int(input()) a = input().split() b = input().split() nums = [] for i in range(n): a[i] = int(a[i]) b[i] = int(b[i]) nums.append(a[i] - b[i]) nums.sort() num_pairs = 0 for i in range(n): large_start_idx = binary_search(nums, i+1, n-1, nums[i]) if large_start_idx == -1: continue num_pairs += n-large_start_idx print(num_pairs) ```
output
1
98,765
14
197,531
Provide tags and a correct Python 3 solution for this coding contest problem. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
instruction
0
98,864
14
197,728
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` # SHRi GANESHA author: Kunal Verma # import os import sys from bisect import bisect_right from collections import Counter, defaultdict, deque from heapq import * from io import BytesIO, IOBase from math import gcd, inf, sqrt, ceil def lcm(a, b): return (a * b) // gcd(a, b) ''' mod = 10 ** 9 + 7 fac = [1] for i in range(1, 2 * 10 ** 5 + 1): fac.append((fac[-1] * i) % mod) fac_in = [pow(fac[-1], mod - 2, mod)] for i in range(2 * 10 ** 5, 0, -1): fac_in.append((fac_in[-1] * i) % mod) fac_in.reverse() def comb(a, b): if a < b: return 0 return (fac[a] * fac_in[b] * fac_in[a - b]) % mod ''' #MAXN = 10000004 # spf = [0 for i in range(MAXN)] # adj = [[] for i in range(MAXN)] def sieve(): global spf, adj, MAXN spf[1] = 1 for i in range(2, MAXN): spf[i] = i for i in range(2, MAXN): if i * i > MAXN: break if (spf[i] == i): for j in range(i * i, MAXN, i): if (spf[j] == j): spf[j] = i def getdistinctFactorization(n): global adj, spf, MAXN for i in range(1, n + 1): index = 1 x = i if (x != 1): adj[i].append(spf[x]) x = x // spf[x] while (x != 1): if (adj[i][index - 1] != spf[x]): adj[i].append(spf[x]) index += 1 x = x // spf[x] def printDivisors(n): i = 2 z = [1, n] while i <= sqrt(n): if (n % i == 0): if (n / i == i): z.append(i) else: z.append(i) z.append(n // i) i = i + 1 return z def create(n, x, f): pq = len(bin(n)[2:]) if f == 0: tt = min else: tt = max dp = [[inf] * n for _ in range(pq)] dp[0] = x for i in range(1, pq): for j in range(n - (1 << i) + 1): dp[i][j] = tt(dp[i - 1][j], dp[i - 1][j + (1 << (i - 1))]) return dp def enquiry(l, r, dp, f): if l > r: return inf if not f else -inf if f == 1: tt = max else: tt = min pq1 = len(bin(r - l + 1)[2:]) - 1 return tt(dp[pq1][l], dp[pq1][r - (1 << pq1) + 1]) def SieveOfEratosthenes(n): 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 x = [] for i in range(2, n + 1): if prime[i]: x.append(i) return x def main(): n,m=map(int,input().split()) z=[[0,0]for i in range(n+1) ] xx=[] for i in range(n): p=int(input()) if p>0: z[p][1]+=1 else: z[abs(p)][0]+=1 xx.append(p) s=0 for i in range(1,n+1): s+=z[i][1] an=[] # print(z) for i in range(1,n+1): # print(n - z[i][0] - s + z[i][1],s) if n-z[i][0]-s+z[i][1]==m: an.append(i) z=Counter(an) m=[0]*n # print(an) for j in range(n): if xx[j]>0: if z[xx[j]]==len(an): m[j]=1 elif z[xx[j]]==0: m[j]=0 else: m[j]=0.7 else: if z[abs(xx[j])]==len(an): m[j]=0 elif z[abs(xx[j])]>0: m[j]=0.7 else: m[j]=1 #print(an) for i in range(n): if m[i]==1: m[i]="Truth" elif m[i]==0: m[i]= "Lie" else: m[i]="Not defined" print(*m,sep='\n') # Fast IO Region 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") if __name__ == '__main__': main() ```
output
1
98,864
14
197,729
Provide tags and a correct Python 3 solution for this coding contest problem. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
instruction
0
98,865
14
197,730
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` a,b=map(int,input().split()) r=[0]*a c1=[0]*(a+1) c2=[0]*(a+1) f=0 for i in range(a): n=int(input()) r[i]=n if n>0:c1[n]+=1 else: f+=1 c2[-n]+=1 possible=[False]*(a+1) # print(c1,c2) np=0#number of suspects for i in range(1,a+1): if c1[i]+f-c2[i]==b: #f-c2[i] is truth possible[i]=True np+=1 # print(possible) for i in range(a): if r[i]>0:#he said + if possible[r[i]] and np==1:#unique print("Truth") elif not possible[r[i]]:#Lie print("Lie") else: print("Not defined") else:#claims r[i] is not r[i]*=-1 if possible[r[i]] and np==1:print("Lie") elif not possible[r[i]]:print("Truth") else:print("Not defined") # print (possible) ```
output
1
98,865
14
197,731
Provide tags and a correct Python 3 solution for this coding contest problem. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
instruction
0
98,866
14
197,732
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` import sys input=lambda:sys.stdin.readline().strip() print=lambda s:sys.stdout.write(str(s)+"\n") a,b=map(int,input().split()) r=[0]*a c1=[0]*(a+1) c2=[0]*(a+1) f=0 for i in range(a): n=int(input()) r[i]=n if n>0:c1[n]+=1 else: f+=1 c2[-n]+=1 possible=[False]*(a+1) # print(c1,c2) np=0#number of suspects for i in range(1,a+1): if c1[i]+f-c2[i]==b: #f-c2[i] is truth possible[i]=True np+=1 # print(possible) for i in range(a): if r[i]>0:#he said + if possible[r[i]] and np==1:#unique print("Truth") elif not possible[r[i]]:#Lie print("Lie") else: print("Not defined") else:#claims r[i] is not r[i]*=-1 if possible[r[i]] and np==1:print("Lie") elif not possible[r[i]]:print("Truth") else:print("Not defined") # print (possible) ```
output
1
98,866
14
197,733
Provide tags and a correct Python 3 solution for this coding contest problem. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
instruction
0
98,867
14
197,734
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` from collections import Counter c = Counter() n, m = map(int, input().split()) sumdc = 0 mark = [] suspect = set() for _ in range(n): k = int(input()) mark.append(k) if k < 0: sumdc += 1 c = Counter(mark) k = 0 for i in range(1, n + 1): if c[i] + sumdc - c[-1 * i] == m: suspect.add(i) k += 1 if k==0:print("Not defined\n"*n) elif k==1: j=suspect.pop() for x in mark: if x==j or(x<0 and abs(x)!=j):print("Truth") else: print("Lie") else: suspect.update({-x for x in suspect}) for x in mark: if x in suspect:print("Not defined") elif x<0:print("Truth") else:print("Lie") ```
output
1
98,867
14
197,735
Provide tags and a correct Python 3 solution for this coding contest problem. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
instruction
0
98,868
14
197,736
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` n, m = map(int, input().split()) t = [int(input()) for i in range(n)] s, p = 0, [0] * (n + 1) for i in t: if i < 0: m -= 1 p[-i] -= 1 else: p[i] += 1 q = {i for i in range(1, n + 1) if p[i] == m} if len(q) == 0: print('Not defined\n' * n) elif len(q) == 1: j = q.pop() print('\n'.join(['Truth' if i == j or (i < 0 and i + j) else 'Lie' for i in t])) else: q.update({-i for i in q}) print('\n'.join(['Not defined' if i in q else ('Truth' if i < 0 else 'Lie') for i in t])) ```
output
1
98,868
14
197,737
Provide tags and a correct Python 3 solution for this coding contest problem. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
instruction
0
98,869
14
197,738
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` n,m=list(map(int,input().split())) a=[[0,0] for i in range(n)] b=[] e=0 f=0 for i in range(n): c=input() d=int(c[1:]) if c[0]=='+': a[d-1][0]+=1 e+=1 else: a[d-1][1]+=1 f+=1 b.append([c[0],d]) g=[a[i][0]+f-a[i][1] for i in range(n)] h=g.count(m) for i in range(n): d=b[i][1] if b[i][0]=='+': if g[d-1]==m: if h>1: print('Not defined') else: print('Truth') else: print('Lie') else: if h>1 or h==1 and g[d-1]!=m: if g[d-1]==m: print('Not defined') else: print('Truth') else: print('Lie') ```
output
1
98,869
14
197,739
Provide tags and a correct Python 3 solution for this coding contest problem. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
instruction
0
98,870
14
197,740
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` from sys import stdin, stdout n, m = map(int, stdin.readline().split()) cntf = [0 for i in range(n + 1)] cnts = [0 for i in range(n + 1)] challengers = [] for i in range(n): s = stdin.readline().strip() challengers.append(s) if s[0] == '+': cntf[int(s[1:])] += 1 else: cnts[int(s[1:])] += 1 first = sum(cntf) second = sum(cnts) ans = set() for i in range(1, n + 1): if cntf[i] + second - cnts[i] == m: ans.add(i) for i in range(n): s = challengers[i] if s[0] == '+': if int(s[1:]) in ans: if len(ans) > 1: stdout.write('Not defined\n') else: stdout.write('Truth\n') else: stdout.write('Lie\n') else: if int(s[1:]) in ans: if len(ans) > 1: stdout.write('Not defined\n') else: stdout.write('Lie\n') else: stdout.write('Truth\n') ```
output
1
98,870
14
197,741
Provide tags and a correct Python 3 solution for this coding contest problem. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
instruction
0
98,871
14
197,742
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` from collections import Counter n, m = map(int, input().split()) pank = 0 buben = [] bober = set() for _ in range(n): k = int(input()) buben.append(k) if k < 0: pank += 1 cnt = Counter(buben) k = 0 for i in range(1, n + 1): if cnt[i] + pank - cnt[-1 * i] == m: bober.add(i) k += 1 if k == 0: print('Not defined\n' * n) elif k == 1: j = bober.pop() for x in buben: if x == j or (x < 0 and abs(x) != j): print('Truth') else: print('Lie') else: bober.update({-x for x in bober}) for x in buben: if x in bober: print('Not defined') elif x < 0: print('Truth') else: print('Lie') ```
output
1
98,871
14
197,743
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one. Submitted Solution: ``` """ Brandt Smith, Lemuel Gorion and Peter Haddad codeforces.com Problem 156B """ import sys n, m = map(int, input().split(' ')) inp = [] guess = [0] * (n + 1) for i in range(n): temp = int(input()) inp.append(temp) if temp < 0: m -= 1 guess[-temp] -= 1 else: guess[temp] += 1 dic = {temp for temp in range(1, n + 1) if guess[temp] == m} if len(dic) == 0: for i in range(n): print('Not defined') elif len(dic) == 1: temp = dic.pop() for i in inp: if i == temp or (i < 0 and i + temp): print('Truth') else: print('Lie') else: temp = dic.update({-i for i in dic}) for i in inp: if i in dic: print('Not defined') else: if i < 0: print('Truth') else: print('Lie') ```
instruction
0
98,872
14
197,744
Yes
output
1
98,872
14
197,745
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one. Submitted Solution: ``` from collections import defaultdict n, m = map(int, input().split()) a = [] for i in range(n): a.append(int(input())) d = defaultdict(int) pos, neg = 0, 0 for x in a: d[x] += 1 if x > 0: pos += 1 else: neg += 1 possible = [False] * n for i in range(1, n + 1): t = d[i] + neg - d[-i] if t == m: possible[i - 1] = True cnt = sum(possible) for i in range(n): if cnt == 0: print('Lie') continue if a[i] > 0: if possible[a[i] - 1]: print('Truth' if cnt == 1 else 'Not defined') else: print('Lie') else: if not possible[-a[i] - 1]: print('Truth') else: print('Lie' if cnt == 1 else 'Not defined') ```
instruction
0
98,873
14
197,746
Yes
output
1
98,873
14
197,747
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one. Submitted Solution: ``` """ Brandt Smith, Lemuel Gorion and Peter Haddad codeforces.com Problem 156B """ import sys n, m = map(int, input().split(' ')) inp = [] guess = [0] * (n + 1) for i in range(n): temp = int(input()) inp.append(temp) if temp < 0: m -= 1 guess[-temp] -= 1 else: guess[temp] += 1 dic = {temp for temp in range(1, n + 1) if guess[temp] == m} if len(dic) == 0: for i in range(n): print('Not defined') elif len(dic) == 1: temp = dic.pop() for i in inp: if i == temp or (i < 0 and i + temp): print('Truth') else: print('Lie') else: temp = dic.update({-i for i in dic}) for i in inp: if i in dic: print('Not defined') else: if i < 0: print('Truth') else: print('Lie') # Made By Mostafa_Khaled ```
instruction
0
98,874
14
197,748
Yes
output
1
98,874
14
197,749
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one. Submitted Solution: ``` n,m=list(map(int,input().split())) a=[[0,0] for i in range(n)] b=[] e=0 f=0 for i in range(n): c=input() d=int(c[1:]) if c[0]=='+': a[d-1][0]+=1 e+=1 else: a[d-1][1]+=1 f+=1 b.append([c[0],d]) g=[a[int(b[i][1])-1][0]+f-a[int(b[i][1])-1][1] for i in range(n)] h=g.count(m) for i in range(n): d=b[i][1] if b[i][0]=='+': if g[i]==m: if h>1: print('Not defined') else: print('Truth') else: print('Lie') else: if h>1 or h==1 and g[i]!=m: if g[i]==m: print('Not defined') else: print('Truth') else: print('Lie') ```
instruction
0
98,875
14
197,750
No
output
1
98,875
14
197,751
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one. Submitted Solution: ``` a,b=map(int,input().split()) r=[0]*a c1=[0]*(a+1) c2=[0]*(a+1) f=0 for i in range(a): n=int(input()) r[i]=n if n>0:c1[n]+=1 else: f+=1 c2[n]+=1 possible=[False]*(a+1) np=0#number of suspects for i in range(1,a+1): if c1[i]+f-c2[i]==b: #f-c2[i] is truth possible[i]=True np+=1 for i in range(a): if r[i]>0:#he said + if possible[r[i]] and np==1:#unique print("Truth") elif not possible[r[i]]:#Lie print("Lie") else: print("Not defined") else:#claims r[i] is not r[i]*=-1 if possible[r[i]] and np==1:print("Lie") elif not possible[r[i]]:print("Truth") else:print("Not defined") # print (possible) ```
instruction
0
98,876
14
197,752
No
output
1
98,876
14
197,753
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one. Submitted Solution: ``` n,m=map(int,input().split()) c=[0]*(n+1) dc=[0]*(n+1) sumdc=0 sus=0 mark=[] suspect=[] for _ in range(n): k=int(input()) mark.append(k) if k>0: c[k]+=1 else: dc[abs(k)]+=1 sumdc+=1 for i in range(1,n+1): #假定每一个I 为罪犯 if c[i]+sumdc-dc[i]==m: suspect.append(i) if len(suspect)==1: for x in mark: print ("Truth") if x==suspect[0] else print("Lie") else: for x in mark: if x>0: print("Not defined") if suspect.count(x)>0 else print("Lie") else: if suspect.count(abs(x))>0: print("Not defined") elif suspect.count(abs(x))==0: print("Truth") ```
instruction
0
98,877
14
197,754
No
output
1
98,877
14
197,755
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i). Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth? Input The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth. Output Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime. Examples Input 1 1 +1 Output Truth Input 3 2 -1 -2 -3 Output Not defined Not defined Not defined Input 4 1 +2 -3 +4 -1 Output Lie Not defined Lie Not defined Note The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth. In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not. In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one. Submitted Solution: ``` n,m=map(int,input().split()) c=[0]*(n+1) dc=[0]*(n+1) sumdc=0 sus=0 mark=[] suspect=[] for _ in range(n): k=int(input()) mark.append(k) if k>0: c[k]+=1 else: dc[abs(k)]+=1 sumdc+=1 for i in range(1,n+1): #假定每一个I 为罪犯 if c[i]+sumdc-dc[i]==m: suspect.append(i) #print(suspect) if len(suspect)==1: for x in mark: if x>0: print ("Truth") if x==suspect[0] else print("Lie") else: print ("Lie") if abs(x)==suspect[0] else print("True") else: for x in mark: if x>0: print("Not defined") if suspect.count(x)>0 else print("Lie") else: if suspect.count(abs(x))>0: print("Not defined") elif suspect.count(abs(x))==0: print("Truth") ```
instruction
0
98,878
14
197,756
No
output
1
98,878
14
197,757
Provide a correct Python 3 solution for this coding contest problem. The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot. The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set. Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value — it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 — to a single marriage resulting in minimal delight for the audience, and so on. Help the Beaver to implement the algorithm for selecting the desired set. Input The first input line contains integers n, k and t (1 ≤ k ≤ min(100, n2), 1 ≤ t ≤ 2·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 ≤ h, w ≤ n; 1 ≤ r ≤ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple. The input limitations for getting 30 points are: * 1 ≤ n ≤ 5 The input limitations for getting 100 points are: * 1 ≤ n ≤ 20 Output Print a single number — the value of the t-th acceptable variant. Examples Input 2 4 3 1 1 1 1 2 2 2 1 3 2 2 7 Output 2 Input 2 4 7 1 1 1 1 2 2 2 1 3 2 2 7 Output 8 Note The figure shows 7 acceptable sets of marriages that exist in the first sample. <image>
instruction
0
98,879
14
197,758
"Correct Solution: ``` n, k, t = map(int, input().split()) l = (1 << (n + 1)) - 1 p = [tuple(map(int, input().split())) for i in range(k)] p.sort(key = lambda i: i[2]) a, b = -1, sum(i[2] for i in p) def g(j, s, u, v): global k, p, t k += 1 if k > t: return for i, (h, w, r) in enumerate(p[j:], j + 1): if r > s: break if ((u >> h) & 1) and ((v >> w) & 1): g(i, s - r, u - (1 << h), v - (1 << w)) def f(s): global k, l k = 0 g(0, s, l, l) return k while b - a > 1: c = (a + b) // 2 if f(c) < t: a = c else: b = c print(b) ```
output
1
98,879
14
197,759
Provide a correct Python 3 solution for this coding contest problem. The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot. The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set. Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value — it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 — to a single marriage resulting in minimal delight for the audience, and so on. Help the Beaver to implement the algorithm for selecting the desired set. Input The first input line contains integers n, k and t (1 ≤ k ≤ min(100, n2), 1 ≤ t ≤ 2·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 ≤ h, w ≤ n; 1 ≤ r ≤ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple. The input limitations for getting 30 points are: * 1 ≤ n ≤ 5 The input limitations for getting 100 points are: * 1 ≤ n ≤ 20 Output Print a single number — the value of the t-th acceptable variant. Examples Input 2 4 3 1 1 1 1 2 2 2 1 3 2 2 7 Output 2 Input 2 4 7 1 1 1 1 2 2 2 1 3 2 2 7 Output 8 Note The figure shows 7 acceptable sets of marriages that exist in the first sample. <image>
instruction
0
98,880
14
197,760
"Correct Solution: ``` def g(t, d): j = len(d) - 1 i = len(t) - 1 while j >= 0: while i >= 0 and t[i] > d[j]: i -= 1 t.insert(i + 1, d[j]) j -= 1 n, k, t = map(int, input().split()) p = [0] * k for i in range(k): h, w, r = map(int, input().split()) p[i] = (r, w, h) p.sort() q, a, b = [0], [0] * (n + 1), [0] * (n + 1) def f(i, s): global p, q, a, b, k, t r = [(p[j][1], p[j][2], s + p[j][0], j + 1) for j in range(i, k) if a[p[j][1]] == b[p[j][2]] == 0] g(q, [s[2] for s in r]) if len(q) > t: q = q[:t] for h, w, s, j in r: if len(q) == t and s >= q[t - 1]: break a[h], b[w] = 1, 1 f(j, s) a[h], b[w] = 0, 0 f(0, 0) print(q[t - 1]) ```
output
1
98,880
14
197,761
Provide a correct Python 3 solution for this coding contest problem. The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot. The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set. Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value — it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 — to a single marriage resulting in minimal delight for the audience, and so on. Help the Beaver to implement the algorithm for selecting the desired set. Input The first input line contains integers n, k and t (1 ≤ k ≤ min(100, n2), 1 ≤ t ≤ 2·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 ≤ h, w ≤ n; 1 ≤ r ≤ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple. The input limitations for getting 30 points are: * 1 ≤ n ≤ 5 The input limitations for getting 100 points are: * 1 ≤ n ≤ 20 Output Print a single number — the value of the t-th acceptable variant. Examples Input 2 4 3 1 1 1 1 2 2 2 1 3 2 2 7 Output 2 Input 2 4 7 1 1 1 1 2 2 2 1 3 2 2 7 Output 8 Note The figure shows 7 acceptable sets of marriages that exist in the first sample. <image>
instruction
0
98,881
14
197,762
"Correct Solution: ``` n, k, t = map(int, input().split()) p = [0] * k for i in range(k): h, w, r = map(int, input().split()) p[i] = (r, w, h) p.sort() q, a, b = [], [0] * (n + 1), [0] * (n + 1) def f(i, s): global p, q, a, b, k q.append(s) while i < k: h, w = p[i][1], p[i][2] if a[h] == b[w] == 0: a[h], b[w] = 1, 1 f(i + 1, s + p[i][0]) a[h], b[w] = 0, 0 i += 1 f(0, 0) q.sort() print(q[t - 1]) ```
output
1
98,881
14
197,763
Provide a correct Python 3 solution for this coding contest problem. The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot. The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set. Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value — it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 — to a single marriage resulting in minimal delight for the audience, and so on. Help the Beaver to implement the algorithm for selecting the desired set. Input The first input line contains integers n, k and t (1 ≤ k ≤ min(100, n2), 1 ≤ t ≤ 2·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 ≤ h, w ≤ n; 1 ≤ r ≤ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple. The input limitations for getting 30 points are: * 1 ≤ n ≤ 5 The input limitations for getting 100 points are: * 1 ≤ n ≤ 20 Output Print a single number — the value of the t-th acceptable variant. Examples Input 2 4 3 1 1 1 1 2 2 2 1 3 2 2 7 Output 2 Input 2 4 7 1 1 1 1 2 2 2 1 3 2 2 7 Output 8 Note The figure shows 7 acceptable sets of marriages that exist in the first sample. <image>
instruction
0
98,882
14
197,764
"Correct Solution: ``` I=lambda:list(map(int,input().split())) n,k,T=I() t=[I()for _ in '0'*k] def b(h,w,r,a): if h>n:a+=[r] else: b(h+1,w,r,a) for f,s,v in t: if f==h and s in w:b(h+1,w-set([s]),r+v,a) return a print(sorted(b(1,set(range(1,n+1)), 0,[]))[T-1]) ```
output
1
98,882
14
197,765
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot. The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set. Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value — it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 — to a single marriage resulting in minimal delight for the audience, and so on. Help the Beaver to implement the algorithm for selecting the desired set. Input The first input line contains integers n, k and t (1 ≤ k ≤ min(100, n2), 1 ≤ t ≤ 2·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 ≤ h, w ≤ n; 1 ≤ r ≤ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple. The input limitations for getting 30 points are: * 1 ≤ n ≤ 5 The input limitations for getting 100 points are: * 1 ≤ n ≤ 20 Output Print a single number — the value of the t-th acceptable variant. Examples Input 2 4 3 1 1 1 1 2 2 2 1 3 2 2 7 Output 2 Input 2 4 7 1 1 1 1 2 2 2 1 3 2 2 7 Output 8 Note The figure shows 7 acceptable sets of marriages that exist in the first sample. <image> Submitted Solution: ``` n, k, t = map(int, input().split()) p = [0] * k for i in range(k): h, w, r = map(int, input().split()) p[i] = (r, w, h) p.sort() q, a, b = [0], [0] * (n + 1), [0] * (n + 1) def f(i, s): global p, q, a, b, k, t r = [(p[j][1], p[j][2], s + p[j][0], j + 1) for j in range(i, k) if a[p[j][1]] == b[p[j][2]] == 0] print(r) q += [s[2] for s in r] if len(q) > t: q.sort() q = q[:t] for h, w, s, j in r: if len(q) == t and s >= q[t - 1]: break a[h], b[w] = 1, 1 f(j, s) a[h], b[w] = 0, 0 f(0, 0) q.sort() print(q[t - 1]) ```
instruction
0
98,883
14
197,766
No
output
1
98,883
14
197,767
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot. The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set. Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value — it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 — to a single marriage resulting in minimal delight for the audience, and so on. Help the Beaver to implement the algorithm for selecting the desired set. Input The first input line contains integers n, k and t (1 ≤ k ≤ min(100, n2), 1 ≤ t ≤ 2·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 ≤ h, w ≤ n; 1 ≤ r ≤ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple. The input limitations for getting 30 points are: * 1 ≤ n ≤ 5 The input limitations for getting 100 points are: * 1 ≤ n ≤ 20 Output Print a single number — the value of the t-th acceptable variant. Examples Input 2 4 3 1 1 1 1 2 2 2 1 3 2 2 7 Output 2 Input 2 4 7 1 1 1 1 2 2 2 1 3 2 2 7 Output 8 Note The figure shows 7 acceptable sets of marriages that exist in the first sample. <image> Submitted Solution: ``` n, k, t = map(int, input().split()) p = [0] * k for i in range(k): h, w, r = map(int, input().split()) p[i] = (r, w, h) p.sort() q, g, a, b = [0], 10 ** 10, [0] * (n + 1), [0] * (n + 1) def f(i, s): global p, q, a, b, k, t, g r = [(p[j][1], p[j][2], s + p[j][0]) for j in range(i, k) if a[p[j][1]] == b[p[j][2]] == 0] q += [s[2] for s in r] i += 1 if len(q) > t: q.sort() g = q[t - 1] - 1 for h, w, s in r: if s > g: break a[h], b[w] = 1, 1 f(i, s) a[h], b[w] = 0, 0 f(0, 0) print(g + 1) ```
instruction
0
98,884
14
197,768
No
output
1
98,884
14
197,769
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers like several couples, and a marriage of any of them will make the audience happy. The Smart Beaver formalized this fact as k triples of numbers (h, w, r), where h is the index of the man, w is the index of the woman, and r is the measure of the audience's delight in case of the marriage of this couple. The same poll showed that the marriage of any other couple will leave the audience indifferent, so the screenwriters decided not to include any such marriages in the plot. The script allows you to arrange several marriages between the heroes or not to arrange marriages at all. A subset of some of the k marriages is considered acceptable if each man and each woman is involved in at most one marriage of the subset (the series won't allow any divorces). The value of the acceptable set of marriages is the total delight the spectators will get from the marriages included in this set. Obviously, there is a finite number of acceptable sets, and they all describe some variants of the script. The screenwriters do not want to choose a set with maximum value — it would make the plot too predictable. So the Smart Beaver offers the following option: sort all the acceptable sets in increasing order of value and choose the t-th set from the sorted list. Thus, t = 1 corresponds to a plot without marriages, t = 2 — to a single marriage resulting in minimal delight for the audience, and so on. Help the Beaver to implement the algorithm for selecting the desired set. Input The first input line contains integers n, k and t (1 ≤ k ≤ min(100, n2), 1 ≤ t ≤ 2·105), separated by single spaces. Next k lines contain triples of integers (h, w, r) (1 ≤ h, w ≤ n; 1 ≤ r ≤ 1000), separated by single spaces, which describe the possible marriages. It is guaranteed that the input data is correct: t doesn't exceed the total number of acceptable sets, and each pair (h, w) is present in at most one triple. The input limitations for getting 30 points are: * 1 ≤ n ≤ 5 The input limitations for getting 100 points are: * 1 ≤ n ≤ 20 Output Print a single number — the value of the t-th acceptable variant. Examples Input 2 4 3 1 1 1 1 2 2 2 1 3 2 2 7 Output 2 Input 2 4 7 1 1 1 1 2 2 2 1 3 2 2 7 Output 8 Note The figure shows 7 acceptable sets of marriages that exist in the first sample. <image> Submitted Solution: ``` n, k, t = map(int, input().split()) l = (1 << (n + 1)) - 1 p = [tuple(map(int, input().split())) for i in range(k)] p.sort(key = lambda i: i[2]) a, b = 0, sum(i[2] for i in p) def g(j, s, u, v): global k, p, t k += 1 if k > t: return for i, (h, w, r) in enumerate(p[j:], j + 1): if r > s: break if ((u >> h) & 1) and ((v >> w) & 1): g(i, s - r, u - (1 << h), v - (1 << w)) def f(s): global k, l k = 0 g(0, s, l, l) return k while b - a > 1: c = (a + b) // 2 if f(c) < t: a = c else: b = c print(b) ```
instruction
0
98,885
14
197,770
No
output
1
98,885
14
197,771
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
instruction
0
99,134
14
198,268
Tags: dfs and similar, math Correct Solution: ``` ###### ### ####### ####### ## # ##### ### ##### # # # # # # # # # # # # # ### # # # # # # # # # # # # # ### ###### ######### # # # # # # ######### # ###### ######### # # # # # # ######### # # # # # # # # # # # #### # # # # # # # # # # ## # # # # # ###### # # ####### ####### # # ##### # # # # from __future__ import print_function # for PyPy2 # from itertools import permutations # from functools import cmp_to_key # for adding custom comparator # from fractions import Fraction from collections import * from sys import stdin # from bisect import * from heapq import * from math import * g = lambda : stdin.readline().strip() gl = lambda : g().split() gil = lambda : [int(var) for var in gl()] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] rr = lambda x : reversed(range(x)) mod = int(1e9)+7 inf = float("inf") n, = gil() nxt = [0] + gil() vis = [0]*(n+1) c = set() def cycle(p): tt = 0 start = p while vis[p] == 0: tt += 1 vis[p] = 1 p = nxt[p] if p != start: print(-1) exit() return tt for p in range(1, n+1): if vis[p] : continue tt = cycle(p) if tt&1 : c.add(tt) else: c.add(tt//2) if 1 in c:c.remove(1) c = list(c) # print(c) if len(c) <= 1: print(c[0] if c else 1) exit() pr = {} def getPrime(x): ppr = {} for i in range(2, int(sqrt(x))+1): po = 0 while x%i == 0: x //= i po += 1 if po:ppr[i] = po if x > 1: ppr[x] = 1 return ppr for v in c: for pi, po in getPrime(v).items(): pr[pi] = max(pr.get(pi, 0), po) ans = 1 for pi, po in pr.items(): ans *= pi**po print(ans) ```
output
1
99,134
14
198,269
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
instruction
0
99,135
14
198,270
Tags: dfs and similar, math Correct Solution: ``` from math import gcd n = int(input()) crush = list(map(int,input().split())) crush = [0] + crush vis,dis,cyc,flag = [0]*(n+1), [-1]*(n+1), [], 1 def dfs(u,d): dis[u] = d vis[u] = 1 global flag v = crush[u] if vis[v]==0: dfs(v,d+1) else: if dis[v]==1: cyc.append(d) else: flag = 0 dis[u] = -1 for i in range(1,n+1): if not flag: break if vis[i]==0: dfs(i,1) if not flag or len(cyc)==0: print(-1) else: def lcm(a,b): return a*b//gcd(a,b) L = cyc[0] if not L%2: L //= 2 for i in range(1,len(cyc)): s = cyc[i] if not s%2: s //= 2 L = lcm(L,s) print(L) # C:\Users\Usuario\HOME2\Programacion\ACM ```
output
1
99,135
14
198,271
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
instruction
0
99,136
14
198,272
Tags: dfs and similar, math Correct Solution: ``` def gcd(a, b): while b: a, b = b, a % b return a def lcm(a, b): return a * b // gcd(a, b) n = int(input()) a = list(map(int, input().split())) if sorted(a) != [i + 1 for i in range(n)]: print(-1) else: ans = 1 used = [0 for i in range(n)] for i in range(n): if used[i] == 0: j = i am = 0 while used[j] == 0: am += 1 used[j] = 1 j = a[j] - 1 if am % 2: ans = lcm(ans, am) else: ans = lcm(ans, am // 2) print(ans) ```
output
1
99,136
14
198,273
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
instruction
0
99,137
14
198,274
Tags: dfs and similar, math Correct Solution: ``` import sys #sys.stdin=open("data.txt") input=sys.stdin.readline n=int(input()) l=list(map(lambda x:int(x)-1,input().split())) use=[] valid=1 for i in range(n): t=i for j in range(n+5): t=l[t] if t==i: if (j+1)%2==0: use.append((j+1)//2) else: use.append(j+1) break else: valid=0 if not valid: print("-1") else: # get lcm ans=1 for i in use: t=ans while ans%i: ans+=t print(ans) ```
output
1
99,137
14
198,275
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
instruction
0
99,138
14
198,276
Tags: dfs and similar, math Correct Solution: ``` from math import gcd n = int(input()) arr = map(int, input().split()) arr = list(map(lambda x: x-1, arr)) res = 1 for i in range(n): p, k = 0, i for j in range(n): k = arr[k] if k == i: p = j break if k != i: print(-1) exit() p += 1 if p % 2 == 0: p //= 2 res = res * p // gcd(res, p) print(res) ```
output
1
99,138
14
198,277
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
instruction
0
99,139
14
198,278
Tags: dfs and similar, math Correct Solution: ``` input() crush = [0] + [int(x) for x in input().split()] visited = set() circle_sizes = [] def gcd(a, b): return a if b == 0 else gcd(b, a%b) def lcm(a, b): return a * b // gcd(a, b) def solve(): for i in range(len(crush)): if i not in visited: start, cur, count = i, i, 0 while cur not in visited: visited.add(cur) count += 1 cur = crush[cur] if cur != start: return -1 circle_sizes.append(count if count % 2 else count // 2) if len(circle_sizes) == 1: return circle_sizes[0] ans = lcm(circle_sizes[0], circle_sizes[1]) for size in circle_sizes[2:]: ans = lcm(ans, size) return ans print(solve()) ```
output
1
99,139
14
198,279
Provide tags and a correct Python 3 solution for this coding contest problem. As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i). Input The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land. The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush. Output If there is no t satisfying the condition, print -1. Otherwise print such smallest t. Examples Input 4 2 3 1 4 Output 3 Input 4 4 4 4 4 Output -1 Input 4 2 1 4 3 Output 1 Note In the first sample suppose t = 3. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
instruction
0
99,140
14
198,280
Tags: dfs and similar, math Correct Solution: ``` from fractions import gcd n = int(input()) crush = list(map(int,input().split())) was = [0] * n ans = 1 for i in range(n): if was[i]: continue num = i j = 0 while not was[num]: was[num] = 1 num = crush[num] - 1 j += 1 if i != num: ans = -1 break else: if j % 2 == 0: j //= 2 ans = ans * j // gcd(ans,j) print(ans) ```
output
1
99,140
14
198,281