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. Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to m. Input The first line contains two positive integers n and m (2 ≀ n ≀ 2Β·105, 2 ≀ m ≀ 10) β€” the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≀ ai ≀ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer β€” the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2Β·109. Examples Input 4 3 2 1 3 1 Output 5 Input 7 4 4 2 3 1 2 4 3 Output 18 Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. Submitted Solution: ``` n, m = map(int, input().strip().split()) g = [0]*m for a in map(int, input().strip().split()): g[a-1] += 1 total = 0 for i in range(m): for j in range(i+1, m): total += g[i]*g[j] print(total) ```
instruction
0
77,881
14
155,762
Yes
output
1
77,881
14
155,763
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to m. Input The first line contains two positive integers n and m (2 ≀ n ≀ 2Β·105, 2 ≀ m ≀ 10) β€” the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≀ ai ≀ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer β€” the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2Β·109. Examples Input 4 3 2 1 3 1 Output 5 Input 7 4 4 2 3 1 2 4 3 Output 18 Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. Submitted Solution: ``` import sys import string from collections import Counter, defaultdict from math import fsum, sqrt, gcd, ceil, factorial from operator import * from itertools import accumulate inf = float("inf") # input = sys.stdin.readline flush = lambda: sys.stdout.flush comb = lambda x, y: (factorial(x) // factorial(y)) // factorial(x - y) # inputs # ip = lambda : input().rstrip() ip = lambda: input() ii = lambda: int(input()) r = lambda: map(int, input().split()) rr = lambda: list(r()) n, k = r() arr = rr() arr = Counter(arr) c = 0 for i in arr.values(): c += i * (n - i) print(c // 2) ```
instruction
0
77,882
14
155,764
Yes
output
1
77,882
14
155,765
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to m. Input The first line contains two positive integers n and m (2 ≀ n ≀ 2Β·105, 2 ≀ m ≀ 10) β€” the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≀ ai ≀ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer β€” the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2Β·109. Examples Input 4 3 2 1 3 1 Output 5 Input 7 4 4 2 3 1 2 4 3 Output 18 Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. Submitted Solution: ``` def main(): nm=input().split(' ') book=input().split(' ') n=int(nm[0]) m=int(nm[1]) result=0 array=[0 for i in range(m)] for i in book: array[int(i)-1]+=1 for i in range(m): s=0 for j in range(i+1,m): s+=array[j] result+=array[i]*s print(result) if __name__ == "__main__": main() ```
instruction
0
77,883
14
155,766
Yes
output
1
77,883
14
155,767
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to m. Input The first line contains two positive integers n and m (2 ≀ n ≀ 2Β·105, 2 ≀ m ≀ 10) β€” the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≀ ai ≀ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer β€” the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2Β·109. Examples Input 4 3 2 1 3 1 Output 5 Input 7 4 4 2 3 1 2 4 3 Output 18 Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. Submitted Solution: ``` #!/usr/bin/python3 def read_ints(): return list(map(int, input().split())) bookCount, topicCount = read_ints() books = {} for book in read_ints(): books[book] = books.get(book,0) + 1 result = 0 i = 0 for curBooks in books.values(): result += curBooks * (bookCount - curBooks) bookCount -= curBooks i += 1 print(result) ```
instruction
0
77,884
14
155,768
Yes
output
1
77,884
14
155,769
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to m. Input The first line contains two positive integers n and m (2 ≀ n ≀ 2Β·105, 2 ≀ m ≀ 10) β€” the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≀ ai ≀ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer β€” the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2Β·109. Examples Input 4 3 2 1 3 1 Output 5 Input 7 4 4 2 3 1 2 4 3 Output 18 Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. Submitted Solution: ``` n,m=list(map(int,input().split())) a=list(map(int,input().split())) s=0 d=n-m f=d h=0 for i in range(1,n): if d>0: h+=i d=d-1 s+=i if f==1: print(s-h) else: print(s-h+f) ```
instruction
0
77,885
14
155,770
No
output
1
77,885
14
155,771
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to m. Input The first line contains two positive integers n and m (2 ≀ n ≀ 2Β·105, 2 ≀ m ≀ 10) β€” the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≀ ai ≀ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer β€” the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2Β·109. Examples Input 4 3 2 1 3 1 Output 5 Input 7 4 4 2 3 1 2 4 3 Output 18 Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. Submitted Solution: ``` n , m = map(int, input().split()) a = list(map(int, input().split())) d = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for i in range(n): d[a[i] - 1] += 1 res = 0 for i in range(n): res += n - d[a[i] - 1] res /= 2 print(res) ```
instruction
0
77,886
14
155,772
No
output
1
77,886
14
155,773
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to m. Input The first line contains two positive integers n and m (2 ≀ n ≀ 2Β·105, 2 ≀ m ≀ 10) β€” the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≀ ai ≀ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer β€” the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2Β·109. Examples Input 4 3 2 1 3 1 Output 5 Input 7 4 4 2 3 1 2 4 3 Output 18 Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. Submitted Solution: ``` n,m=map(int,input().split()) a=list(map(int,input().split())) x=(n*(n-1))//2 z=n-len(set(a)) y=(z*(z+1))//2 print(x-y) ```
instruction
0
77,887
14
155,774
No
output
1
77,887
14
155,775
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to m. Input The first line contains two positive integers n and m (2 ≀ n ≀ 2Β·105, 2 ≀ m ≀ 10) β€” the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≀ ai ≀ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer β€” the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2Β·109. Examples Input 4 3 2 1 3 1 Output 5 Input 7 4 4 2 3 1 2 4 3 Output 18 Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. Submitted Solution: ``` n, m = list(map(int, input().split())) a = list(map(int, input().split())) c = {} for i in a: if i in c: c[i] += 1 else: c[i] = 0 t = 0 for i in range(n): c[a[i]] -= 1 t += len(a[i+1:]) - c[a[i]] print(t) ```
instruction
0
77,888
14
155,776
No
output
1
77,888
14
155,777
Provide tags and a correct Python 3 solution for this coding contest problem. Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third β€” by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer a (1 ≀ a ≀ 1000) β€” the initial position of the first friend. The second line contains a single integer b (1 ≀ b ≀ 1000) β€” the initial position of the second friend. It is guaranteed that a β‰  b. Output Print the minimum possible total tiredness if the friends meet in the same point. Examples Input 3 4 Output 1 Input 101 99 Output 2 Input 5 10 Output 9 Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β€” two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
instruction
0
77,967
14
155,934
Tags: brute force, greedy, implementation, math Correct Solution: ``` x=int(input()) y=int(input()) dis=abs(x-y) a=dis//2 b=dis-a print(a*(a+1)//2+b*(b+1)//2) if dis>1 else print(1) ```
output
1
77,967
14
155,935
Provide tags and a correct Python 3 solution for this coding contest problem. Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third β€” by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer a (1 ≀ a ≀ 1000) β€” the initial position of the first friend. The second line contains a single integer b (1 ≀ b ≀ 1000) β€” the initial position of the second friend. It is guaranteed that a β‰  b. Output Print the minimum possible total tiredness if the friends meet in the same point. Examples Input 3 4 Output 1 Input 101 99 Output 2 Input 5 10 Output 9 Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β€” two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
instruction
0
77,968
14
155,936
Tags: brute force, greedy, implementation, math Correct Solution: ``` import math def f(n): return sum(list(range(n+1))) a = int(input()) b = int(input()) middle = (a+b)//2 dist1 = int(math.fabs(a-middle)) dist2 = int(math.fabs(b-middle)) print(f(dist1) + f(dist2)) ```
output
1
77,968
14
155,937
Provide tags and a correct Python 3 solution for this coding contest problem. Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third β€” by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer a (1 ≀ a ≀ 1000) β€” the initial position of the first friend. The second line contains a single integer b (1 ≀ b ≀ 1000) β€” the initial position of the second friend. It is guaranteed that a β‰  b. Output Print the minimum possible total tiredness if the friends meet in the same point. Examples Input 3 4 Output 1 Input 101 99 Output 2 Input 5 10 Output 9 Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β€” two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
instruction
0
77,969
14
155,938
Tags: brute force, greedy, implementation, math Correct Solution: ``` from math import ceil def solve(a, b): x = ceil(abs(a - b)/2) y = abs(a - b)//2 if x != y: return ((1+y)*y)//2 + ((x+1)*x)//2 else: return ((1+y)*y) def main(): a = int(input()) b = int(input()) print(solve(a, b)) main() ```
output
1
77,969
14
155,939
Provide tags and a correct Python 3 solution for this coding contest problem. Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third β€” by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer a (1 ≀ a ≀ 1000) β€” the initial position of the first friend. The second line contains a single integer b (1 ≀ b ≀ 1000) β€” the initial position of the second friend. It is guaranteed that a β‰  b. Output Print the minimum possible total tiredness if the friends meet in the same point. Examples Input 3 4 Output 1 Input 101 99 Output 2 Input 5 10 Output 9 Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β€” two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
instruction
0
77,970
14
155,940
Tags: brute force, greedy, implementation, math Correct Solution: ``` import math;a=int(input());b=int(input());print((math.ceil(abs(a-b)/2))*((math.ceil(abs(a-b)/2)) + 1)//1 - (((a-b)%2))*(math.ceil(abs(a-b)/2)) ) ```
output
1
77,970
14
155,941
Provide tags and a correct Python 3 solution for this coding contest problem. Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third β€” by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer a (1 ≀ a ≀ 1000) β€” the initial position of the first friend. The second line contains a single integer b (1 ≀ b ≀ 1000) β€” the initial position of the second friend. It is guaranteed that a β‰  b. Output Print the minimum possible total tiredness if the friends meet in the same point. Examples Input 3 4 Output 1 Input 101 99 Output 2 Input 5 10 Output 9 Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β€” two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
instruction
0
77,971
14
155,942
Tags: brute force, greedy, implementation, math Correct Solution: ``` a = int(input()) b = int(input()) result = 0 contA = 0 contB = 0 while(a != b): if(contA > contB): if(b > a): b -= 1 else: b += 1 contB += 1 result += contB else: if(a < b): a += 1 else: a -= 1 contA += 1 result += contA print(result) ```
output
1
77,971
14
155,943
Provide tags and a correct Python 3 solution for this coding contest problem. Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third β€” by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer a (1 ≀ a ≀ 1000) β€” the initial position of the first friend. The second line contains a single integer b (1 ≀ b ≀ 1000) β€” the initial position of the second friend. It is guaranteed that a β‰  b. Output Print the minimum possible total tiredness if the friends meet in the same point. Examples Input 3 4 Output 1 Input 101 99 Output 2 Input 5 10 Output 9 Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β€” two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
instruction
0
77,972
14
155,944
Tags: brute force, greedy, implementation, math Correct Solution: ``` a=int(input()) b=int(input()) c=(a+b)//2 d=0 e=1 if(a<c): for i in range(a,c): d=d+e e=e+1 else: for i in range(c,a): d=d+e e=e+1 e=1 if(b<c): for i in range(b,c): d=d+e e=e+1 else: for i in range(c,b): d=d+e e=e+1 print(d) ```
output
1
77,972
14
155,945
Provide tags and a correct Python 3 solution for this coding contest problem. Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third β€” by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer a (1 ≀ a ≀ 1000) β€” the initial position of the first friend. The second line contains a single integer b (1 ≀ b ≀ 1000) β€” the initial position of the second friend. It is guaranteed that a β‰  b. Output Print the minimum possible total tiredness if the friends meet in the same point. Examples Input 3 4 Output 1 Input 101 99 Output 2 Input 5 10 Output 9 Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β€” two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
instruction
0
77,973
14
155,946
Tags: brute force, greedy, implementation, math Correct Solution: ``` a = int(input()) b = int(input()) x = abs(a - b) // 2 s = (x * (1 + x)) // 2 s *= 2 if (abs(a - b) % 2 == 1): s += x + 1 print(s) ```
output
1
77,973
14
155,947
Provide tags and a correct Python 3 solution for this coding contest problem. Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third β€” by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer a (1 ≀ a ≀ 1000) β€” the initial position of the first friend. The second line contains a single integer b (1 ≀ b ≀ 1000) β€” the initial position of the second friend. It is guaranteed that a β‰  b. Output Print the minimum possible total tiredness if the friends meet in the same point. Examples Input 3 4 Output 1 Input 101 99 Output 2 Input 5 10 Output 9 Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β€” two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.
instruction
0
77,974
14
155,948
Tags: brute force, greedy, implementation, math Correct Solution: ``` a=int(input()) b=int(input()) c=abs(a-b) d=(c//2)*((c//2)+1)+(c%2)*((c//2)+1) print(d) ```
output
1
77,974
14
155,949
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k. For example, if n=8 and k=2, then: * strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; * strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing two integers n and k (1 ≀ k ≀ n ≀ 2β‹… 10^5) β€” the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string s of length n consisting of "0" and "1" β€” a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant β€” the difference between indices of any two "1" is more than k. The sum of n for all test cases in one test does not exceed 2β‹… 10^5. Output For each test case output one integer β€” the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0. Example Input 6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0 Output 1 2 0 1 1 1 Note The first test case is explained in the statement. In the second test case, the answer is 2, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant.
instruction
0
78,431
14
156,862
Tags: constructive algorithms, greedy, math Correct Solution: ``` import sys INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br def solve(): n, k = I() s = input() ans = 0 last = -INF for i in range(n): if s[i] == '1': if i - last <= k: ans -= 1 last = i count = 0 continue if i - last > k: ans += 1 last = i print(ans) t, = I() while t: t -= 1 solve() ```
output
1
78,431
14
156,863
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k. For example, if n=8 and k=2, then: * strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; * strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing two integers n and k (1 ≀ k ≀ n ≀ 2β‹… 10^5) β€” the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string s of length n consisting of "0" and "1" β€” a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant β€” the difference between indices of any two "1" is more than k. The sum of n for all test cases in one test does not exceed 2β‹… 10^5. Output For each test case output one integer β€” the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0. Example Input 6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0 Output 1 2 0 1 1 1 Note The first test case is explained in the statement. In the second test case, the answer is 2, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant.
instruction
0
78,432
14
156,864
Tags: constructive algorithms, greedy, math Correct Solution: ``` for i in range(int(input())): n, k = [int(x) for x in input().split()] n += 2*k + 2 s = '1' + '0'*k + input() + '0'*k + '1' one = [0] for j in range(k+1, n - (k+1)): if s[j] == '1': one.append(j) one.append(n-1) ans = 0 for j in range(1, len(one)): gap = one[j] - one[j-1] if gap >= 2*k + 2: ans += gap // (k + 1) - 1 print(ans) ```
output
1
78,432
14
156,865
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k. For example, if n=8 and k=2, then: * strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; * strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing two integers n and k (1 ≀ k ≀ n ≀ 2β‹… 10^5) β€” the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string s of length n consisting of "0" and "1" β€” a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant β€” the difference between indices of any two "1" is more than k. The sum of n for all test cases in one test does not exceed 2β‹… 10^5. Output For each test case output one integer β€” the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0. Example Input 6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0 Output 1 2 0 1 1 1 Note The first test case is explained in the statement. In the second test case, the answer is 2, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant.
instruction
0
78,433
14
156,866
Tags: constructive algorithms, greedy, math Correct Solution: ``` import math t=int(input()) for i in range(t): n,k = map(int,input().split()) s=input() z=0 noz=0 if not('1' in s): print(math.ceil(n/(k+1))) else: while z<len(s): if s[z]=='1': z+=k+1 else: nz=z while z<len(s): if s[z]=='0': z+=1 else: break if z==len(s): noz+=math.ceil((z-nz)/(k+1)) elif (z-nz-k)>0: noz+=math.ceil((z-nz-k)/(k+1)) z=z+k+1 print(noz) ```
output
1
78,433
14
156,867
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k. For example, if n=8 and k=2, then: * strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; * strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing two integers n and k (1 ≀ k ≀ n ≀ 2β‹… 10^5) β€” the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string s of length n consisting of "0" and "1" β€” a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant β€” the difference between indices of any two "1" is more than k. The sum of n for all test cases in one test does not exceed 2β‹… 10^5. Output For each test case output one integer β€” the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0. Example Input 6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0 Output 1 2 0 1 1 1 Note The first test case is explained in the statement. In the second test case, the answer is 2, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant.
instruction
0
78,434
14
156,868
Tags: constructive algorithms, greedy, math Correct Solution: ``` from sys import stdin as lector input = lector.readlines() n = lambda l,k: int((l-k)/(1+k)) for i in range(1,2*int(input[0])+1,2): L,k = list(map(int,input[i].split(' '))) x = input[i+1][0:-1] linea = list(map(int,list(x))) suma = sum(linea) if L < (k+1): if (suma == 0): print(1) else: print(0) else: r = 0 ks = [] for o in range(0,k): ks.append('0') xx = ''.join(ks)+x+''.join(ks) y = xx.split('1') for j in range(0,len(y)): if y[j]!='': r += n(len(y[j]),k) print(r) ```
output
1
78,434
14
156,869
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k. For example, if n=8 and k=2, then: * strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; * strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing two integers n and k (1 ≀ k ≀ n ≀ 2β‹… 10^5) β€” the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string s of length n consisting of "0" and "1" β€” a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant β€” the difference between indices of any two "1" is more than k. The sum of n for all test cases in one test does not exceed 2β‹… 10^5. Output For each test case output one integer β€” the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0. Example Input 6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0 Output 1 2 0 1 1 1 Note The first test case is explained in the statement. In the second test case, the answer is 2, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant.
instruction
0
78,435
14
156,870
Tags: constructive algorithms, greedy, math Correct Solution: ``` a = int(input()) for _ in range(a): n, k = map(int,input().split()) s = input() a = 0 z = -100000000 lis = [int(i) for i in s] for i in range(len(lis)): if lis[i] == 1: if i <= k + z: a -= 1 z = i continue if i > k + z: a += 1 z = i print(a) ```
output
1
78,435
14
156,871
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k. For example, if n=8 and k=2, then: * strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; * strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing two integers n and k (1 ≀ k ≀ n ≀ 2β‹… 10^5) β€” the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string s of length n consisting of "0" and "1" β€” a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant β€” the difference between indices of any two "1" is more than k. The sum of n for all test cases in one test does not exceed 2β‹… 10^5. Output For each test case output one integer β€” the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0. Example Input 6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0 Output 1 2 0 1 1 1 Note The first test case is explained in the statement. In the second test case, the answer is 2, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant.
instruction
0
78,436
14
156,872
Tags: constructive algorithms, greedy, math Correct Solution: ``` from collections import * from bisect import * from math import * mod = 10 ** 9 + 7 for _ in range(int(input())): n,k = map(int,input().split()) l = list(input()) ans = 0 val = k for i in range(n): if(l[i] == '1'): if(val < k): ans -= 1 val = 0 else: val += 1 if(val == k + 1): ans += 1 val = 0 print(ans) ```
output
1
78,436
14
156,873
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k. For example, if n=8 and k=2, then: * strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; * strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing two integers n and k (1 ≀ k ≀ n ≀ 2β‹… 10^5) β€” the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string s of length n consisting of "0" and "1" β€” a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant β€” the difference between indices of any two "1" is more than k. The sum of n for all test cases in one test does not exceed 2β‹… 10^5. Output For each test case output one integer β€” the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0. Example Input 6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0 Output 1 2 0 1 1 1 Note The first test case is explained in the statement. In the second test case, the answer is 2, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant.
instruction
0
78,437
14
156,874
Tags: constructive algorithms, greedy, math Correct Solution: ``` ncases = int(input()) a = [] for i in range(ncases): take = input().split(" ") n = int(take[0]) k = int(take[1]) state = list(input()) if ('1' not in state): for j in range(0,len(state),k+1): state[j] = '1' a.append(state.count('1')) else: first = state.index('1') ini = state.count('1') while (first - (k+1) >= 0): state[first - (k+1)] = '1' first = first - (k+1) temp = 99999999999 for l in range(first,len(state)): if (k!=0): if (state[l] == '0'): k -= 1 elif (state[l] == '1'): k = int(take[1]) if (temp < len(state)): state[temp] = '0' elif (k == 0): if (state[l] == '0'): state[l] = '1' temp = l k = int(take[1]) elif (state[l] == '1'): k = int(take[1]) fin = state.count('1') a.append(fin-ini) for ele in a: print (ele) ```
output
1
78,437
14
156,875
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i-k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k. For example, if n=8 and k=2, then: * strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; * strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases in the test. Then t test cases follow. Each test case starts with a line containing two integers n and k (1 ≀ k ≀ n ≀ 2β‹… 10^5) β€” the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string s of length n consisting of "0" and "1" β€” a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant β€” the difference between indices of any two "1" is more than k. The sum of n for all test cases in one test does not exceed 2β‹… 10^5. Output For each test case output one integer β€” the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0. Example Input 6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0 Output 1 2 0 1 1 1 Note The first test case is explained in the statement. In the second test case, the answer is 2, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant.
instruction
0
78,438
14
156,876
Tags: constructive algorithms, greedy, math Correct Solution: ``` for _ in range(int(input())): n,k=map(int,input().split()) s=input() ar=[] for i in range(n): if s[i]=='1': ar.append(i) al=len(ar) ans=0 if al==0: ans=1+(n-1)//(k+1) else: ans=ar[0]//(k+1)+(max(0,n-ar[-1]-1))//(k+1) if al>1: for i in range(al-1): ans+=(max(ar[i+1]-ar[i]-1-k,0))//(k+1) print(ans) ```
output
1
78,438
14
156,877
Provide tags and a correct Python 3 solution for this coding contest problem. One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. Input The first line contains two integers a and m (1 ≀ a, m ≀ 105). Output Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". Examples Input 1 5 Output No Input 3 6 Output Yes
instruction
0
78,653
14
157,306
Tags: implementation, math, matrices Correct Solution: ``` # mukulchandel a,m=map(int,input().split()) for i in range(20): if (a*(2**i))%m==0: print("Yes") quit() print("No") ```
output
1
78,653
14
157,307
Provide tags and a correct Python 3 solution for this coding contest problem. One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. Input The first line contains two integers a and m (1 ≀ a, m ≀ 105). Output Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". Examples Input 1 5 Output No Input 3 6 Output Yes
instruction
0
78,654
14
157,308
Tags: implementation, math, matrices Correct Solution: ``` import sys input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) q = inlt() a = q[0] m = q[1] while(m%2 == 0): m = m // 2 if(a%m == 0): print("Yes") else: print("No") ```
output
1
78,654
14
157,309
Provide tags and a correct Python 3 solution for this coding contest problem. One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. Input The first line contains two integers a and m (1 ≀ a, m ≀ 105). Output Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". Examples Input 1 5 Output No Input 3 6 Output Yes
instruction
0
78,655
14
157,310
Tags: implementation, math, matrices Correct Solution: ``` a, m = map(int, input().split()) print('No' if (a << 17) % m else 'Yes') ```
output
1
78,655
14
157,311
Provide tags and a correct Python 3 solution for this coding contest problem. One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. Input The first line contains two integers a and m (1 ≀ a, m ≀ 105). Output Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". Examples Input 1 5 Output No Input 3 6 Output Yes
instruction
0
78,656
14
157,312
Tags: implementation, math, matrices Correct Solution: ``` a, m = map(int, input().split()) r = a ans = 0 for i in range(10**6): r += r % m if r % m == 0: ans = 1 if ans: print("Yes") else: print("No") ```
output
1
78,656
14
157,313
Provide tags and a correct Python 3 solution for this coding contest problem. One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. Input The first line contains two integers a and m (1 ≀ a, m ≀ 105). Output Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". Examples Input 1 5 Output No Input 3 6 Output Yes
instruction
0
78,657
14
157,314
Tags: implementation, math, matrices Correct Solution: ``` import sys input = lambda:sys.stdin.readline() MOD = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: list(map(int, input().split())) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) a,m=f() c=0 while a%m!=0: a+=a%m c+=1 if c>10**6: break print("YNeos"[a%m!=0::2]) ```
output
1
78,657
14
157,315
Provide tags and a correct Python 3 solution for this coding contest problem. One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. Input The first line contains two integers a and m (1 ≀ a, m ≀ 105). Output Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". Examples Input 1 5 Output No Input 3 6 Output Yes
instruction
0
78,658
14
157,316
Tags: implementation, math, matrices Correct Solution: ``` a,m = map(int,input().split()) for i in range(17): if a%m==0: print("Yes") quit() else: a+=a%m print("No") ```
output
1
78,658
14
157,317
Provide tags and a correct Python 3 solution for this coding contest problem. One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. Input The first line contains two integers a and m (1 ≀ a, m ≀ 105). Output Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". Examples Input 1 5 Output No Input 3 6 Output Yes
instruction
0
78,659
14
157,318
Tags: implementation, math, matrices Correct Solution: ``` a, m = map(int, input().split()) was = [False for i in range(m)] was[a % m] = True a += a % m while a % m != 0 and not was[a % m]: was[a % m] = True a += a % m print('Yes' if was[0] or a % m == 0 else 'No') ```
output
1
78,659
14
157,319
Provide tags and a correct Python 3 solution for this coding contest problem. One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory. The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m). Given the number of details a on the first day and number m check if the production stops at some moment. Input The first line contains two integers a and m (1 ≀ a, m ≀ 105). Output Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". Examples Input 1 5 Output No Input 3 6 Output Yes
instruction
0
78,660
14
157,320
Tags: implementation, math, matrices Correct Solution: ``` import math def res(): n,m=map(int,input().split()) k=math.floor(math.log(m,2))+2 i=1 t=n f=1 while i<=k: if t%m==0: f=0 break t=t*2 i=i+1 if f==0: print("Yes") else: print("No") res() ```
output
1
78,660
14
157,321
Provide tags and a correct Python 3 solution for this coding contest problem. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible
instruction
0
78,765
14
157,530
Tags: dfs and similar, dp, graphs, trees Correct Solution: ``` #!/usr/bin/env python3 import sys import threading from math import * def ri(): return map(int, sys.stdin.readline().split()) def dfsv(u): global ans h1 = -inf h2 = -inf for i in adj[u]: if v[i] == 0: v[i] = 1 dfsv(i) s[u]+=s[i] if h1 < h[i]: h2 = h1 h1 = h[i] elif h2 < h[i]: h2 = h[i] ans = max(ans, h1+h2) h[u] = max(h1, s[u]) return n = int(input()) a = list(ri()) adj = [[] for i in range(n)] v = [0 for i in range(n)] s = [a[i] for i in range(n)] h = [0 for i in range(n)] for i in range(n-1): aa, bb = ri() aa -= 1 bb -= 1 adj[aa].append(bb) adj[bb].append(aa) ans = -inf def solve(): v[0] = 1 dfsv(0) if ans == -inf: print("Impossible") else: print(ans) max_recur_size = 10**5*2 + 1000 max_stack_size = max_recur_size*500 sys.setrecursionlimit(max_recur_size) threading.stack_size(max_stack_size) thread = threading.Thread(target=solve) thread.start() ```
output
1
78,765
14
157,531
Provide tags and a correct Python 3 solution for this coding contest problem. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible
instruction
0
78,766
14
157,532
Tags: dfs and similar, dp, graphs, trees Correct Solution: ``` import sys from math import inf import threading def dfs(g, i, v, n, p): global exist, ans summ = v[i-1] first = -inf second = -inf maxm = -inf for j in g[i]: if(j==p): continue sum_child, max_child = dfs(g, j, v, n, i) summ += sum_child if max_child>= first: second = first first = max_child elif max_child> second: second = max_child ans = max(ans, first+second) maxm = max(first, summ) return summ, maxm def solve(): n = int(input()) v = list(map(int, sys.stdin.readline().split())) g = [[] for _ in range(n+1)] for _ in range(n-1): x, y = map(int, input().split()) g[x].append(y) g[y].append(x) _ , a = dfs(g, 1, v, n, 0) if(not ans == -inf): print(ans) else: print('Impossible') ans = -inf max_recur_size = 10**5*2 + 1000 max_stack_size = max_recur_size*500 sys.setrecursionlimit(max_recur_size) threading.stack_size(max_stack_size) thread = threading.Thread(target=solve) thread.start() ```
output
1
78,766
14
157,533
Provide tags and a correct Python 3 solution for this coding contest problem. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible
instruction
0
78,767
14
157,534
Tags: dfs and similar, dp, graphs, trees Correct Solution: ``` import sys input = sys.stdin.readline oo = 10**20 n = int(input()) a = list(map(int, input().split())) adj = [[] for _ in range(n)] for _ in range(n-1): u, v = [int(i) - 1 for i in input().split()] adj[u].append(v) adj[v].append(u) sm = [0] * n mx = [-oo] * n best = [-oo] * n def dfs(start): stack = [(start, -1)] visit = [False] * n while stack: u, p = stack[-1] if not visit[u]: for v in adj[u]: if v != p: stack.append((v, u)) visit[u] = True else: x = [-oo] * 3 for v in adj[u]: if v != p: sm[u] += sm[v] mx[u] = max(mx[u], mx[v]) best[u] = max(best[u], best[v]) x[0] = mx[v] x.sort() sm[u] += a[u] mx[u] = max(mx[u], sm[u]) if x[1] > -oo and x[2] > -oo: cur = x[1] + x[2] best[u] = max(best[u], cur) stack.pop() dfs(0) ans = max(best) if ans <= -oo: print('Impossible') else: print(ans) ```
output
1
78,767
14
157,535
Provide tags and a correct Python 3 solution for this coding contest problem. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible
instruction
0
78,768
14
157,536
Tags: dfs and similar, dp, graphs, trees Correct Solution: ``` import os import sys from io import BytesIO, IOBase from types import GeneratorType from collections import defaultdict 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") sys.setrecursionlimit(2*10**5) def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc @bootstrap def dfs(u,p): for j in adj[u]: if j!=p: yield dfs(j, u) f[u]+=f[j] res[u]=max(res[u],res[j]) f[u]+=b[u-1] res[u]=max(res[u],f[u]) yield @bootstrap def dfs2(u,p,val): global ans if val!=-float("inf"): ans=max(ans,f[u]+val) req=[] for j in adj[u]: if j !=p: req.append([res[j],j]) req.sort(reverse=True) req.append([-float("inf"),-1]) for j in adj[u]: if j !=p: if req[0][1]==j: yield dfs2(j,u,max(val,req[1][0])) else: dfs2(j, u, max(val, req[0][0])) yield n=int(input()) b=list(map(int,input().split())) adj=[[] for i in range(n+1)] for j in range(n-1): u,v=map(int,input().split()) adj[u].append(v) adj[v].append(u) f=[0]*(n+1) res=[-float("inf")]*(n+1) dfs(1,0) ans=-float("inf") dfs2(1,0,-float("inf")) if ans==-float("inf"): print("Impossible") else: print(ans) ```
output
1
78,768
14
157,537
Provide tags and a correct Python 3 solution for this coding contest problem. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible
instruction
0
78,769
14
157,538
Tags: dfs and similar, dp, graphs, trees Correct Solution: ``` import sys input = sys.stdin.readline oo = 10**20 n = int(input()) a = list(map(int, input().split())) adj = [[] for _ in range(n)] for _ in range(n-1): u, v = [int(i) - 1 for i in input().split()] adj[u].append(v) adj[v].append(u) sm = [0] * n mx = [-oo] * n best = [-oo] * n stack = [(0, -1)] visit = [False] * n while stack: u, p = stack[-1] if not visit[u]: for v in adj[u]: if v != p: stack.append((v, u)) visit[u] = True else: x = [-oo] * 3 for v in adj[u]: if v != p: sm[u] += sm[v] mx[u] = max(mx[u], mx[v]) best[u] = max(best[u], best[v]) x[0] = mx[v] x.sort() sm[u] += a[u] mx[u] = max(mx[u], sm[u]) if x[1] > -oo and x[2] > -oo: cur = x[1] + x[2] best[u] = max(best[u], cur) stack.pop() ans = max(best) if ans <= -oo: print('Impossible') else: print(ans) ```
output
1
78,769
14
157,539
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible Submitted Solution: ``` n = int(input()) a = [int(s) for s in input().split()] root = 1 children = {x:[] for x in range(1, n+1)} for _ in range(n-1): f, t = [int(s) for s in input().split()] children[f].append(t) value = {} def cal_val(node): val = a[node-1] for n in children[node]: val += cal_val(n) value[node] = val return val cal_val(1) ans = [max(0, max(value.values()))] def dfs(root): children_vals = [] for n in children[root]: children_vals.append(dfs(n)) maxes = [max(s) for s in children_vals] if len(maxes) >= 2: maxes.sort() ans[0] = max(ans[0], maxes[-1] + maxes[-2]) vals = set([value[root]]) for v in children_vals: vals |= v return vals dfs(1) print(ans[0] if ans[0] > 0 else 'Impossible') ```
instruction
0
78,770
14
157,540
No
output
1
78,770
14
157,541
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible Submitted Solution: ``` '''input 1 ''' from sys import stdin input = stdin.readline import math import sys, threading from collections import defaultdict sys.setrecursionlimit(10 ** 4) def mp(f, s): return str(f) + ' ' + str(s) def dfs(tree, node, visited, cut, pre, dp): visited[node] = True if mp(node, cut) in dp: return dp[mp(node, cut)] if node != 0: mx1 = pre[node] else: mx1 = -float('inf') for i in tree[node]: if visited[i] is False: mx1 = max(mx1, dfs(tree, i, visited, cut, pre, dp)) mx2 = -float('inf') mx3 = -float('inf') for i in tree[node]: if visited[i] is False and cut > 1: t = dfs(tree, i, visited, cut - 1, pre, dp) if t > mx2: mx3 = mx2 mx2 = t elif t > mx3: mx3 = t # print('temp', temp, mx2, mx3) visited[node] = False # print(node, mx1, mx2, mx3, cut) dp[mp(node, cut)] = max(mx1, mx2 + mx3) return max(mx1, mx2 + mx3) def get_pre(tree, visited, node, pre, parr): visited[node] = True s = 0 for i in tree[node]: if i not in visited: s += get_pre(tree, visited, i, pre, parr) pre[node] = s + parr[node] return s + parr[node] # mains def main(): n = int(input().strip()) parr = list(map(int, input().split())) tree = defaultdict(list) for _ in range(n - 1): a, b = list(map(int, input().split())) a -= 1 b -= 1 tree[a].append(b) tree[b].append(a) if n <= 2: print("Impossible") exit() visited = dict() pre = dict() get_pre(tree, visited, 0, pre, parr) # print(dp) visited = dict() for i in range(n): visited[i] = False dp = dict() print(dfs(tree, 0, visited, 2, pre, dp)) if __name__ == "__main__": sys.setrecursionlimit(200005) threading.stack_size(1<<27) thread = threading.Thread(target = main) thread.start() ```
instruction
0
78,771
14
157,542
No
output
1
78,771
14
157,543
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible Submitted Solution: ``` #!/usr/bin/env python3 import sys import threading from math import * def ri(): return map(int, input().split()) def dfsv(u): global ans print("3") for i in adj[u]: adj[i].remove(u) dfsv(i) adj[u].sort(key=lambda e: node[e][1]) num_chd = len(adj[u]) node[u][2] = a[u] + sum([node[adj[u][i]][2] for i in range(num_chd)]) if num_chd == 0: node[u][0] = -inf node[u][1] = a[u] elif num_chd == 1: node[u][0] = -inf node[u][1] = max(node[adj[u][-1]][1], node[u][2]) else: node[u][0] = node[adj[u][-1]][1] + node[adj[u][-2]][1] node[u][1] = max(node[adj[u][-1]][1], node[u][2]) ans = max(node[u][0], ans) return n = int(input()) a = list(ri()) adj = [[] for i in range(n)] node = [[-inf,-inf, -inf] for i in range(n)] for i in range(n-1): aa, bb = ri() aa-=1 bb-=1 adj[aa].append(bb) adj[bb].append(aa) ans = -inf def solve(): print("1") dfsv(0) print("2") if ans == -inf: print("Impossible") else: print(ans) max_recur_size = 10**5*2 + 1000 max_stack_size = max_recur_size*50 print("0") sys.setrecursionlimit(max_recur_size) threading.stack_size(max_stack_size) thread = threading.Thread(target=solve) thread.start() ```
instruction
0
78,772
14
157,544
No
output
1
78,772
14
157,545
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β€” pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer n (1 ≀ n ≀ 2Β·105) β€” the number of gifts. The next line contains n integers a1, a2, ..., an ( - 109 ≀ ai ≀ 109) β€” the pleasantness of the gifts. The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β€” the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. Examples Input 8 0 5 -1 4 3 2 6 5 1 2 2 4 2 5 1 3 3 6 6 7 6 8 Output 25 Input 4 1 -5 1 1 1 2 1 4 2 3 Output 2 Input 1 -1 Output Impossible Submitted Solution: ``` from sys import stdin from collections import namedtuple N = int(stdin.readline()) graph = { n: set() for n in range(N + 1) } pleasantness = [0] + list(map(int, stdin.readline().split())) INF = float("inf") possible = False for n in range(N - 1): u, v = stdin.readline().split() graph[int(u)].add(int(v)) graph[int(v)].add(int(u)) class Node: def __init__(self, num, left, right): self.num = num self.left = left self.right = right self.pleasantness = -INF # self.bestleft = -INF # self.bestright = -INF # self.bestsum = -INF def __str__(self, level=0): return " " * level + "{}: {}".format(self.num, self.pleasantness) + "\n" + (self.left.__str__(level + 1) if self.left else "") + (self.right.__str__(level + 1) if self.right else "") def maketree(num): global possible if num == None: return None if len(graph[num]) == 2: l, r = graph[num] graph[l].remove(num) graph[r].remove(num) possible = True elif len(graph[num]) == 1: (l,), r = graph[num], None graph[l].remove(num) else: l, r = None, None return Node(num, maketree(l), maketree(r)) def fillpleasantness(node): if node == None: return 0 node.pleasantness = pleasantness[node.num] + fillpleasantness(node.left) + fillpleasantness(node.right) return node.pleasantness def solve(tree): if tree == None: return -INF, -INF bl, bls = solve(tree.left) br, brs = solve(tree.right) return max(tree.pleasantness, bl, br), max(bl + br, bls, brs) tree = maketree(1) fillpleasantness(tree) # solve(tree) # print(tree) if possible: print(solve(tree)[1]) else: print("Impossible") ```
instruction
0
78,773
14
157,546
No
output
1
78,773
14
157,547
Provide tags and a correct Python 3 solution for this coding contest problem. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200
instruction
0
79,334
14
158,668
Tags: combinatorics, math Correct Solution: ``` import math n = int(input()) ans = int(((2*math.factorial(n))/pow(n,2))) print(ans) ```
output
1
79,334
14
158,669
Provide tags and a correct Python 3 solution for this coding contest problem. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200
instruction
0
79,335
14
158,670
Tags: combinatorics, math Correct Solution: ``` n = int(input()) k = n//2 num = 1 for i in range(n,k,-1): num = num*i den = 1 for i in range(k,0,-1): den = den*i ans = num//den for i in range(k-1,1,-1): ans = ans * i*i print (ans//2) ```
output
1
79,335
14
158,671
Provide tags and a correct Python 3 solution for this coding contest problem. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200
instruction
0
79,336
14
158,672
Tags: combinatorics, math Correct Solution: ``` def factorial(m, n): if m == 0: return 1 if m == 1: return n return factorial(m - 1, n * m) n = int(input()) if n > 2: half = n // 2 res = factorial(n, 1) // factorial(half, 1) res = res // factorial(half, 1) res = res // 2 res = res * factorial((half - 1), 1) * factorial((half - 1), 1) else: res = 1 print(res) ```
output
1
79,336
14
158,673
Provide tags and a correct Python 3 solution for this coding contest problem. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200
instruction
0
79,337
14
158,674
Tags: combinatorics, math Correct Solution: ``` n = int(input()) if n == 2: print(1) else: if n==4: print(3) else: rs = 1 for i in range(3,n//2): rs*=i*i rs*=2 for i in range(n//2+1,n+1): rs*=i for i in range(1,n//2+1): rs//=i print(rs) ```
output
1
79,337
14
158,675
Provide tags and a correct Python 3 solution for this coding contest problem. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200
instruction
0
79,338
14
158,676
Tags: combinatorics, math Correct Solution: ``` import math n = int(input()) print(math.factorial(n) // (2 * math.factorial(n // 2) ** 2) * (math.factorial(n // 2) // (n // 2)) ** 2) ```
output
1
79,338
14
158,677
Provide tags and a correct Python 3 solution for this coding contest problem. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200
instruction
0
79,339
14
158,678
Tags: combinatorics, math Correct Solution: ``` def fact(n): res = 1 for i in range(2, n+1): res = res * i return res def nCr(n, r): return (fact(n) / (fact(r) * fact(n - r))) n = int(input()) if(n == 2): print(1) else: x = fact((n//2)-1) ans = int(nCr(n,n//2) * x * x * (0.5)) print(ans) ```
output
1
79,339
14
158,679
Provide tags and a correct Python 3 solution for this coding contest problem. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200
instruction
0
79,340
14
158,680
Tags: combinatorics, math Correct Solution: ``` n = input() res = {'2' : 1, '4' : 3, '6' : 40, '8' : 1260, '10' : 72576, '12' : 6652800, '14' : 889574400, '16' : 163459296000, '18' : 39520825344000, '20' : 12164510040883200 } print(res[n]) ```
output
1
79,340
14
158,681
Provide tags and a correct Python 3 solution for this coding contest problem. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200
instruction
0
79,341
14
158,682
Tags: combinatorics, math Correct Solution: ``` def nCr(n, r): num = fact(n) denom=fact(r)*fact(n-r) return num/denom def fact(n): res = 1 for i in range(1,n+1): res = res * i return int(res) t=1 while t>0: n=int(input()) x=int(n/2) ans=int(nCr(n,x)) ans/=2 y=fact(x-1) ans = ans*y*y print(int(ans)) t-=1 ```
output
1
79,341
14
158,683
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n/2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Each person should belong to exactly one of these two round dances. Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1, 3, 4, 2], [4, 2, 1, 3] and [2, 1, 3, 4] are indistinguishable. For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person. For example, if n=4 then the number of ways is 3. Possible options: * one round dance β€” [1,2], another β€” [3,4]; * one round dance β€” [2,4], another β€” [3,1]; * one round dance β€” [4,1], another β€” [3,2]. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n/2 people. Input The input contains one integer n (2 ≀ n ≀ 20), n is an even number. Output Print one integer β€” the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type. Examples Input 2 Output 1 Input 4 Output 3 Input 8 Output 1260 Input 20 Output 12164510040883200 Submitted Solution: ``` import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = 'x' in file.mode or 'w' in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b'\n') + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: 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') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' def get_input(a=str): return a(input()) def get_int_input(): return get_input(int) def get_input_arr(a): return list(map(a, input().split())) def get_int_input_arr(): return get_input_arr(int) import math import operator as op from functools import reduce def solve(): def ncr(n, r): r = min(r, n-r) numer = reduce(op.mul, range(n, n-r, -1), 1) denom = reduce(op.mul, range(1, r+1), 1) return numer // denom hv = get_int_input() hf = hv // 2 res = (ncr(hv, hf) * math.factorial(hf - 1) * math.factorial(hf - 1)) // 2 cout<<res<<endl def main(): solve() if __name__ == "__main__": main() ```
instruction
0
79,342
14
158,684
Yes
output
1
79,342
14
158,685