text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order. One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file. Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≀ fi ≀ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th. It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time. Input The first line contains a positive integer n (1 ≀ n ≀ 2Β·105) β€” the number of fragments. The second line contains n different integers fi (1 ≀ fi ≀ n) β€” the number of the fragment written in the i-th sector. Output Print the only integer β€” the number of time units needed to read the file. Examples Input 3 3 1 2 Output 3 Input 5 1 3 5 4 2 Output 10 Note In the second example the head moves in the following way: * 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units * 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units * 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units * 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10. Submitted Solution: ``` fragment_count = int(input()) line = input() fragments = [] fragments = [int(i) for i in line.split()] places = [0 for i in range(fragment_count)] for index, frag in enumerate(fragments): places[frag - 1] = index total_sum = 0 index = 0 for i in places: if index == len(places) - 1: break total_sum = total_sum + abs(i - places[index + 1]) index = index + 1 print(total_sum) ``` Yes
102,500
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order. One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file. Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≀ fi ≀ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th. It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time. Input The first line contains a positive integer n (1 ≀ n ≀ 2Β·105) β€” the number of fragments. The second line contains n different integers fi (1 ≀ fi ≀ n) β€” the number of the fragment written in the i-th sector. Output Print the only integer β€” the number of time units needed to read the file. Examples Input 3 3 1 2 Output 3 Input 5 1 3 5 4 2 Output 10 Note In the second example the head moves in the following way: * 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units * 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units * 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units * 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) b = [0] * (n + 1) for i, x in enumerate(a): b[x] = i print(sum([abs(b[i] - b[i - 1]) for i in range(2, n + 1)])) ``` Yes
102,501
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order. One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file. Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≀ fi ≀ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th. It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time. Input The first line contains a positive integer n (1 ≀ n ≀ 2Β·105) β€” the number of fragments. The second line contains n different integers fi (1 ≀ fi ≀ n) β€” the number of the fragment written in the i-th sector. Output Print the only integer β€” the number of time units needed to read the file. Examples Input 3 3 1 2 Output 3 Input 5 1 3 5 4 2 Output 10 Note In the second example the head moves in the following way: * 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units * 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units * 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units * 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) ans=0 c=0 for i in range(2,n+1): for j in range(n): if i==a[j]: ans+=abs(c-j) c=j break print(ans) ``` No
102,502
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order. One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file. Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≀ fi ≀ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th. It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time. Input The first line contains a positive integer n (1 ≀ n ≀ 2Β·105) β€” the number of fragments. The second line contains n different integers fi (1 ≀ fi ≀ n) β€” the number of the fragment written in the i-th sector. Output Print the only integer β€” the number of time units needed to read the file. Examples Input 3 3 1 2 Output 3 Input 5 1 3 5 4 2 Output 10 Note In the second example the head moves in the following way: * 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units * 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units * 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units * 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10. Submitted Solution: ``` n = int(input()) l = [int(i) for i in input().split(" ")] new = [None] * n print(new) for i in range(n): new[l[i]-1] = i print(new) s = 0 x = new[0] for i in range(1, n): s += abs(x - new[i]) x = new[i] print(s) ``` No
102,503
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order. One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file. Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≀ fi ≀ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th. It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time. Input The first line contains a positive integer n (1 ≀ n ≀ 2Β·105) β€” the number of fragments. The second line contains n different integers fi (1 ≀ fi ≀ n) β€” the number of the fragment written in the i-th sector. Output Print the only integer β€” the number of time units needed to read the file. Examples Input 3 3 1 2 Output 3 Input 5 1 3 5 4 2 Output 10 Note In the second example the head moves in the following way: * 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units * 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units * 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units * 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10. Submitted Solution: ``` n = input() sectors = [int(x) for x in input().split()] def my_arg_sort(list_of_sectors): new_list = [0]*len(list_of_sectors) for i, elem in enumerate(list_of_sectors): new_list[elem-1] = i return new_list sorted = my_arg_sort(sectors) print(sorted) ans = 0 actual_position = sorted[0] for k in sorted: print('k = {}, act_pos = {}'.format(k, actual_position)) ans += abs(k - actual_position) actual_position = k print(ans) ``` No
102,504
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order. One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file. Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≀ fi ≀ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th. It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time. Input The first line contains a positive integer n (1 ≀ n ≀ 2Β·105) β€” the number of fragments. The second line contains n different integers fi (1 ≀ fi ≀ n) β€” the number of the fragment written in the i-th sector. Output Print the only integer β€” the number of time units needed to read the file. Examples Input 3 3 1 2 Output 3 Input 5 1 3 5 4 2 Output 10 Note In the second example the head moves in the following way: * 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units * 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units * 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units * 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10. Submitted Solution: ``` n=int(input()) m=list(map(int,input().split())) k=0 for i in range(0,len(m)): k=k+m[i] print(k-n) ``` No
102,505
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Tags: brute force, constructive algorithms Correct Solution: ``` from sys import stdin import operator N = int(stdin.readline()) p = list(map(int, stdin.readline().strip().split(" "))) teams = list(map(lambda x: 1 if x == "B" else -1, stdin.readline().strip())) values = list(map(operator.mul, p, teams)) start = sum(filter(lambda x: max(x, 0), values)) # print(start) total = start best = start for i in values: total -= i best = max(total, best) total = start for i in reversed(values): total -= i best = max(total, best) # print(p, teams, values, best) print(best) ```
102,506
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Tags: brute force, constructive algorithms Correct Solution: ``` n, t = int(input()), list(map(int, input().split())) b = [q * (d == 'B') for q, d in zip(t, input())] y = v = sum(b) x = u = sum(t) - v for i in range(n): u += 2 * b[i] - t[i] x = max(x, u) v += t[i] - 2 * b[i] y = max(y, v) print(max(x, y)) ```
102,507
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Tags: brute force, constructive algorithms Correct Solution: ``` n = int(input()) p = list(map(int, input().split())) s = input() ans = bob1 = bob2 = 0 for i in range(n): if s[i] == 'B': ans += p[i] bob1 += p[i] bob2 += p[i] for i in range(n): if s[i] == 'A': bob1 += p[i] else: bob1 -= p[i] ans = max(ans, bob1) for i in reversed(range(n)): if s[i] == 'A': bob2 += p[i] else: bob2 -= p[i] ans = max(ans, bob2) print(ans) ```
102,508
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Tags: brute force, constructive algorithms Correct Solution: ``` read = lambda: map(int, input().split()) n = int(input()) p = list(read()) a = [{'B': 1, 'A': 0}[i] for i in input()] cur = sum(p[i] for i in range(n) if a[i]) ans = cur b = a[:] for i in range(n): b[i] = int(not a[i]) if b[i]: cur += p[i] else: cur -= p[i] ans = max(ans, cur) cur = sum(p[i] for i in range(n) if a[i]) b = a[:] for i in range(n - 1, -1, -1): b[i] = int(not a[i]) if b[i]: cur += p[i] else: cur -= p[i] ans = max(ans, cur) print(ans) ```
102,509
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Tags: brute force, constructive algorithms Correct Solution: ``` def main(): input() pp = list(map(int, input().split())) mask = [c == 'B' for c in input()] s = t = sum(p for p, m in zip(pp, mask) if m) res = [s] for p, m in zip(pp, mask): if m: s -= p else: s += p res.append(s) pp.reverse() mask.reverse() for p, m in zip(pp, mask): if m: t -= p else: t += p res.append(t) print(max(res)) if __name__ == '__main__': main() ```
102,510
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Tags: brute force, constructive algorithms Correct Solution: ``` n, t = int(input()), list(map(int, input().split())) b = [q * (d == 'B') for q, d in zip(t, input())] v = sum(b) u = sum(t) - v s = max(u, v) for i, j in zip(b, t): u += 2 * i - j v += j - 2 * i s = max(u, v, s) print(s) ```
102,511
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Tags: brute force, constructive algorithms Correct Solution: ``` from itertools import accumulate import math n = int(input()) p = [int(x) for x in input().split()] S1 = sum(p) d = [x == 'A' for x in input()] for i in range(n): if d[i]: p[i] = -p[i] prefix = list(accumulate([0] + p)) S = prefix[-1] m, M = min(prefix), max(prefix) print((S1 + max(S - 2*m, 2*M - S)) // 2) ```
102,512
Provide tags and a correct Python 3 solution for this coding contest problem. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Tags: brute force, constructive algorithms Correct Solution: ``` n = int(input()) p = list(map(int, input().split())) s = input() ans = 0 cur_orig = 0 for i in range(0, n): if s[i] == 'B': cur_orig += p[i] cur = cur_orig ans = cur_orig for i in range(0, n): if s[i] == 'A': cur += p[i] ans = max(ans, cur) else: cur -= p[i] cur = cur_orig for i in range(n - 1, -1, -1): if s[i] == 'A': cur += p[i] ans = max(ans, cur) else: cur -= p[i] print(ans) ```
102,513
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Submitted Solution: ``` n = input() s = [int(x) for x in input().split(' ')] g = input() A = 0 B = 0 for i in range(len(g)): if g[i] == 'A': A += s[i] else: B += s[i] startA, startB = A, B maximum = B for i in range(len(g)): if g[i] == 'A': A -= s[i] B += s[i] else: A += s[i] B -= s[i] if B > maximum: maximum = B A, B = startA, startB for i in reversed(range(len(g))): if g[i] == 'A': A -= s[i] B += s[i] else: A += s[i] B -= s[i] if B > maximum: maximum = B print(maximum) ``` Yes
102,514
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Submitted Solution: ``` n = int(input()) powers = [int(i) for i in input().split()] teams = input() org_power = 0 for i in range(n): if teams[i] == 'B': org_power += powers[i] max_power, power = org_power, org_power for i in range(n): if teams[i] == 'A': power += powers[i] max_power = max(max_power, power) else: power -= powers[i] power = org_power for i in range(n - 1, -1, -1): if teams[i] == 'A': power += powers[i] max_power = max(max_power, power) else: power -= powers[i] print(max_power) ``` Yes
102,515
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Submitted Solution: ``` n, t = int(input()), list(map(int, input().split())) b = [q * (d == 'B') for q, d in zip(t, input())] v = sum(b) u = sum(t) - v s = max(u, v) for i, j in zip(b, t): u += 2 * i - j v += j - 2 * i s = max(u, v, s) print(s) # Made By Mostafa_Khaled ``` Yes
102,516
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Submitted Solution: ``` n = input() strength = input() chars = input() strength = strength.split(" ") cost = 0 for k in range(0 , int(n)): if chars[k] == 'B': cost += int(strength[k]) temp = cost temp_cost = temp for j in range(0,int(n)): if chars[j] == 'A': temp_cost += int(strength[j]) else : temp_cost -= int(strength[j]) cost = max(cost,temp_cost) temp_cost = temp for j in range(int(n) - 1 , 0 , -1): if chars[j] == 'A': temp_cost += int(strength[j]) else : temp_cost -= int(strength[j]) cost = max(cost,temp_cost) print(cost) ``` Yes
102,517
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Submitted Solution: ``` def A_B(start, finish): return data[start] + data[finish] - sum(data[start + 1:finish]) n = int(input()) data = list(map(int, input().split())) data1 = input() data_i = [] ans1 = 0 during = 0 B = 0 help = [] help1 = [] for i in range(n): if data1[i] == 'A': data_i.append(i) else: B += data[i] for i in range(1, len(data_i)): help.append(A_B(data_i[i - 1], data_i[i])) ans = 0 during = 0 for i in range(n): if data1[i] == 'A': during += data[i] else: ans = max(ans, during) during = 0 ans = max(ans, during) for i in range(n): if data1[i] == 'B': ans += data[i] if n == 1: print(data[0]) else: print(max(B + max(help), ans)) ``` No
102,518
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Submitted Solution: ``` n = int(input()) p = [int(x) for x in input().split(' ')] k = input() t = [] for u in k: t.append(u) a = 0 for i in range(0,n): if t[i] == 'B': a += p[i] b = 0 c = 0 if t[0] == 'B' and t[n-1] == 'B': pass else: for i in range(0,n): if t[i] == 'A': b += p[i] elif t[i] == 'B': break p.reverse() t.reverse() for i in range(0,n): if t[i] == 'A': c += p[i] elif t[i] == 'B': break print(a+max(b,c)) ``` No
102,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Submitted Solution: ``` n = int(input()) A = list(map(int, input().split())) s = input() val = 0 for i,c in enumerate(s): if c == 'B': val += A[i] A[i] = -1 subarray_val = val max_val = val for i in range(n+1): if i < n and A[i] + subarray_val > subarray_val: subarray_val += A[i] else: max_val = max(max_val, subarray_val) subarray_val = val print(max_val) ``` No
102,520
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi. The way to split up game pieces is split into several steps: 1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. 2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. 3. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group. Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. Input The first line contains integer n (1 ≀ n ≀ 5Β·105) β€” the number of game pieces. The second line contains n integers pi (1 ≀ pi ≀ 109) β€” the strength of the i-th piece. The third line contains n characters A or B β€” the assignment of teams after the first step (after Alice's step). Output Print the only integer a β€” the maximum strength Bob can achieve. Examples Input 5 1 2 3 4 5 ABABA Output 11 Input 5 1 2 3 4 5 AAAAA Output 15 Input 1 1 B Output 1 Note In the first sample Bob should flip the suffix of length one. In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5. In the third sample Bob should do nothing. Submitted Solution: ``` n = input() strength = input() chars = input() strength = strength.split(" ") cost = 0 for k in range(0 , int(n)): if chars[k] == 'B': cost += int(strength[k]) i = 0 while i < int(n) : temp = chars costpre = 0 #prefix for j in range(0,i): if temp[j] == 'A': new = list(temp) new[j] = 'B' temp = ''.join(new) elif temp[j] == 'B': new = list(temp) new[j] = 'A' temp = ''.join(new) for k in range(0 , int(n)): if temp[k] == 'B': costpre += int(strength[k]) #suffix temp = chars costsuf = 0 for j in range(int(n)-1,int(n)-i , 1): if temp[j] == 'A' : new = list(temp) new[j] = 'B' temp = ''.join(new) elif temp[j] == 'B': new = list(temp) new[j] = 'A' temp = ''.join(new) for k in range(0 , int(n)): if temp[k] == 'B': costsuf += int(strength[k]) cost = max(cost , max(costsuf , costpre)) i += 1 print(cost) ``` No
102,521
Provide tags and a correct Python 3 solution for this coding contest problem. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Tags: combinatorics Correct Solution: ``` p = 10**9+7 n, m = map(int, input().split()) print (n+1 if m == 1 else pow(m, n, p)*(1+(pow(2*m-1, n, p)*pow(m, p-1-n, p) - 1)*m*pow(m-1, p-2, p))%p) ```
102,522
Provide tags and a correct Python 3 solution for this coding contest problem. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Tags: combinatorics Correct Solution: ``` #### IMPORTANT LIBRARY #### ############################ ### DO NOT USE import random --> 250ms to load the library ############################ ### In case of extra libraries: https://github.com/cheran-senthil/PyRival ###################### ####### IMPORT ####### ###################### from functools import cmp_to_key from collections import deque, Counter from heapq import heappush, heappop from math import log, ceil ###################### #### STANDARD I/O #### ###################### import sys import os from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def print(*args, **kwargs): sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def ii(): return int(inp()) def si(): return str(inp()) def li(lag = 0): l = list(map(int, inp().split())) if lag != 0: for i in range(len(l)): l[i] += lag return l def mi(lag = 0): matrix = list() for i in range(n): matrix.append(li(lag)) return matrix def lsi(): #string list return list(map(str, inp().split())) def print_list(lista, space = " "): print(space.join(map(str, lista))) ###################### ### BISECT METHODS ### ###################### def bisect_left(a, x): """i tale che a[i] >= x e a[i-1] < x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] < x: left = mid+1 else: right = mid return left def bisect_right(a, x): """i tale che a[i] > x e a[i-1] <= x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] > x: right = mid else: left = mid+1 return left def bisect_elements(a, x): """elementi pari a x nell'Γ‘rray sortato""" return bisect_right(a, x) - bisect_left(a, x) ###################### ### MOD OPERATION #### ###################### MOD = 10**9 + 7 maxN = 5 FACT = [0] * maxN INV_FACT = [0] * maxN def add(x, y): return (x+y) % MOD def multiply(x, y): return (x*y) % MOD def power(x, y): if y == 0: return 1 elif y % 2: return multiply(x, power(x, y-1)) else: a = power(x, y//2) return multiply(a, a) def inverse(x): return power(x, MOD-2) def divide(x, y): return multiply(x, inverse(y)) def allFactorials(): FACT[0] = 1 for i in range(1, maxN): FACT[i] = multiply(i, FACT[i-1]) def inverseFactorials(): n = len(INV_FACT) INV_FACT[n-1] = inverse(FACT[n-1]) for i in range(n-2, -1, -1): INV_FACT[i] = multiply(INV_FACT[i+1], i+1) def coeffBinom(n, k): if n < k: return 0 return multiply(FACT[n], multiply(INV_FACT[k], INV_FACT[n-k])) ###################### #### GRAPH ALGOS ##### ###################### # ZERO BASED GRAPH def create_graph(n, m, undirected = 1, unweighted = 1): graph = [[] for i in range(n)] if unweighted: for i in range(m): [x, y] = li(lag = -1) graph[x].append(y) if undirected: graph[y].append(x) else: for i in range(m): [x, y, w] = li(lag = -1) w += 1 graph[x].append([y,w]) if undirected: graph[y].append([x,w]) return graph def create_tree(n, unweighted = 1): children = [[] for i in range(n)] if unweighted: for i in range(n-1): [x, y] = li(lag = -1) children[x].append(y) children[y].append(x) else: for i in range(n-1): [x, y, w] = li(lag = -1) w += 1 children[x].append([y, w]) children[y].append([x, w]) return children def dist(tree, n, A, B = -1): s = [[A, 0]] massimo, massimo_nodo = 0, 0 distanza = -1 v = [-1] * n while s: el, dis = s.pop() if dis > massimo: massimo = dis massimo_nodo = el if el == B: distanza = dis for child in tree[el]: if v[child] == -1: v[child] = 1 s.append([child, dis+1]) return massimo, massimo_nodo, distanza def diameter(tree): _, foglia, _ = dist(tree, n, 0) diam, _, _ = dist(tree, n, foglia) return diam def dfs(graph, n, A): v = [-1] * n s = [[A, 0]] v[A] = 0 while s: el, dis = s.pop() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges def bfs(graph, n, A): v = [-1] * n s = deque() s.append([A, 0]) v[A] = 0 while s: el, dis = s.popleft() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges #FROM A GIVEN ROOT, RECOVER THE STRUCTURE def parents_children_root_unrooted_tree(tree, n, root = 0): q = deque() visited = [0] * n parent = [-1] * n children = [[] for i in range(n)] q.append(root) while q: all_done = 1 visited[q[0]] = 1 for child in tree[q[0]]: if not visited[child]: all_done = 0 q.appendleft(child) if all_done: for child in tree[q[0]]: if parent[child] == -1: parent[q[0]] = child children[child].append(q[0]) q.popleft() return parent, children # CALCULATING LONGEST PATH FOR ALL THE NODES def all_longest_path_passing_from_node(parent, children, n): q = deque() visited = [len(children[i]) for i in range(n)] downwards = [[0,0] for i in range(n)] upward = [1] * n longest_path = [1] * n for i in range(n): if not visited[i]: q.append(i) downwards[i] = [1,0] while q: node = q.popleft() if parent[node] != -1: visited[parent[node]] -= 1 if not visited[parent[node]]: q.append(parent[node]) else: root = node for child in children[node]: downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse = True)[0:2] s = [node] while s: node = s.pop() if parent[node] != -1: if downwards[parent[node]][0] == downwards[node][0] + 1: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1]) else: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0]) longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1 for child in children[node]: s.append(child) return longest_path ### TBD SUCCESSOR GRAPH 7.5 ### TBD TREE QUERIES 10.2 da 2 a 4 ### TBD ADVANCED TREE 10.3 ### TBD GRAPHS AND MATRICES 11.3.3 e 11.4.3 e 11.5.3 (ON GAMES) ###################### ## END OF LIBRARIES ## ###################### n,m = li() s = power(m, n) A = [m] B = [1] for i in range(n-1): A.append(multiply(A[-1], m)) B.append(multiply(B[-1], 2*m-1)) A.reverse() for i in range(n): k = multiply(A[i], B[i]) s = add(s, k) print(s) ```
102,523
Provide tags and a correct Python 3 solution for this coding contest problem. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Tags: combinatorics Correct Solution: ``` import sys from array import array n, m = map(int, input().split()) dp = [array('i', [0])*(n+1) for _ in range(2)] dp[0][0] = dp[1][0] = 1 mod = 10**9 + 7 for i in range(1, n+1): dp[0][i] = (dp[0][i-1] * m + dp[0][i-1] * (m-1)) % mod dp[1][i] = (dp[0][i-1] * m + dp[1][i-1] * m) % mod print(dp[1][-1]) ```
102,524
Provide tags and a correct Python 3 solution for this coding contest problem. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Tags: combinatorics Correct Solution: ``` n, m = map(int, input().split()) M = 1000000007 if m == 1: print(n + 1) else: print((m * pow(2 * m - 1, n, M) - pow(m, n, M)) * pow(m - 1, M - 2, M) % M) ```
102,525
Provide tags and a correct Python 3 solution for this coding contest problem. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Tags: combinatorics Correct Solution: ``` n,m=map(int,input().split()) x,y,M=0,1,10**9+7 while n>0: x,y,n=(2*m*x-x+y)%M,y*m%M,n-1 print((y+m*x)%M) ```
102,526
Provide tags and a correct Python 3 solution for this coding contest problem. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Tags: combinatorics Correct Solution: ``` n,m=map(int,input().split()) x,y,M=0,1,1000000007 for i in range(n): x=((2*m-1)*x+y)%M;y=y*m%M print((y+m*x)%M) ```
102,527
Provide tags and a correct Python 3 solution for this coding contest problem. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Tags: combinatorics Correct Solution: ``` P = 10**9 + 7 n, k = map(int, input().split()) print(n + 1 if k == 1 else (k * pow(2 * k - 1, n, P) - pow(k, n, P)) * pow(k - 1, P - 2, P) % P) ```
102,528
Provide tags and a correct Python 3 solution for this coding contest problem. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Tags: combinatorics Correct Solution: ``` import math def euclid_algorithm(a, b): t1, t2 = abs(a), abs(b) #saving equalities: #t1 == x1 * a + y1 * b, #t2 == x2 * a + y2 * b. x1, y1, x2, y2 = int(math.copysign(1, a)), 0, 0, int(math.copysign(1, b)) if t1 < t2: t1, t2 = t2, t1 x1, y1, x2, y2 = x2, y2, x1, y1 while t2 > 0: k = int(t1 // t2) t1, t2 = t2, t1 % t2 #t1 - k * t2 == (x1 - k * x2) * a + (y1 - k * y2) * b x1, y1, x2, y2 = x2, y2, x1 - k * x2, y1 - k * y2 return t1, x1, y1 def opposite_element(x, p): gcd, k, l = euclid_algorithm(x, p) if gcd != 1: return -1 return k % p def solve(n, m, p): if (m-1) % p == 0: return (n + 1) * pow(m, n, p) % p else: S = pow(m, n, p) k = opposite_element(m, p) l = (2*m - 1) * k % p q = opposite_element(l - 1, p) S += pow(m, n, p) * (pow(l, n, p) - 1) * q S %= p return S p = 10 ** 9 + 7 n, m = [int(x) for x in input().split()] print(solve(n, m, p)) ```
102,529
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Submitted Solution: ``` import math def euclid_algorithm(a, b): t1, t2 = abs(a), abs(b) #saving equalities: #t1 == x1 * a + y1 * b, #t2 == x2 * a + y2 * b. x1, y1, x2, y2 = int(math.copysign(1, a)), 0, 0, int(math.copysign(1, b)) if t1 < t2: t1, t2 = t2, t1 x1, y1, x2, y2 = x2, y2, x1, y1 while t2 > 0: k = int(t1 // t2) t1, t2 = t2, t1 % t2 #t1 - k * t2 == (x1 - k * x2) * a + (y1 - k * y2) * b x1, y1, x2, y2 = x2, y2, x1 - k * x2, y1 - k * y2 return t1, x1, y1 def opposite_element(x, p): gcd, k, l = euclid_algorithm(x, p) if gcd != 1: return -1 return k % p p = 10 ** 9 + 7 n, m = [int(x) for x in input().split()] S = pow(m, n, p) k = opposite_element(m, p) l = (2*m - 1) * k % p q = opposite_element(l - 1, p) S += pow(m, n, p) * (pow(l, n, p) - 1) * q S %= p print(S) ``` No
102,530
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Submitted Solution: ``` L = input().split() n = int(L[0]) m = int(L[1]) mod = 1000000007 powr, current = 1, 1 for i in range (0, n): current = (2 * m * current - (current - powr)%mod) powr *= m print(current) ``` No
102,531
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Submitted Solution: ``` import math def euclid_algorithm(a, b): t1, t2 = abs(a), abs(b) #saving equalities: #t1 == x1 * a + y1 * b, #t2 == x2 * a + y2 * b. x1, y1, x2, y2 = int(math.copysign(1, a)), 0, 0, int(math.copysign(1, b)) if t1 < t2: t1, t2 = t2, t1 x1, y1, x2, y2 = x2, y2, x1, y1 while t2 > 0: k = int(t1 // t2) t1, t2 = t2, t1 % t2 #t1 - k * t2 == (x1 - k * x2) * a + (y1 - k * y2) * b x1, y1, x2, y2 = x2, y2, x1 - k * x2, y1 - k * y2 return t1, x1, y1 def opposite_element(x, p): gcd, k, l = euclid_algorithm(x, p) if gcd != 1: return -1 return k % p p = 10 ** 9 + 7 n, m = [int(x) for x in input().split()] S = pow(m, n, p) k = opposite_element(m, p) l = (2*m - 1) * k % p q = opposite_element(l - 1, p) if q != -1: S += pow(m, n, p) * (pow(l, n, p) - 1) * q S %= p else: S += pow(m, n, p) * k S %= p print(S) ``` No
102,532
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence). You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7. Input The only line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the number of elements in arrays and the upper bound for elements. Output Print the only integer c β€” the desired sum modulo 109 + 7. Examples Input 1 3 Output 6 Input 2 2 Output 14 Input 3 3 Output 174 Submitted Solution: ``` import math def euclid_algorithm(a, b): t1, t2 = abs(a), abs(b) #saving equalities: #t1 == x1 * a + y1 * b, #t2 == x2 * a + y2 * b. x1, y1, x2, y2 = int(math.copysign(1, a)), 0, 0, int(math.copysign(1, b)) if t1 < t2: t1, t2 = t2, t1 x1, y1, x2, y2 = x2, y2, x1, y1 while t2 > 0: k = int(t1 // t2) t1, t2 = t2, t1 % t2 #t1 - k * t2 == (x1 - k * x2) * a + (y1 - k * y2) * b x1, y1, x2, y2 = x2, y2, x1 - k * x2, y1 - k * y2 return t1, x1, y1 def opposite_element(x, p): gcd, k, l = euclid_algorithm(x, p) if gcd != 1: return -1 return k % p p = 10 ** 9 + 7 n, m = [int(x) for x in input().split()] S = pow(m, n, p) k = opposite_element(m, p) l = (2*m - 1) * k % p q = opposite_element(l - 1, p) if q != -1: S += pow(m, n, p) * (pow(l, n, p) - 1) * q S %= p else: k1 = opposite_element(m, p) S += pow(m-1, n, p) * k1 S %= p print(S) ``` No
102,533
Provide tags and a correct Python 3 solution for this coding contest problem. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Tags: math, number theory Correct Solution: ``` import math as m n = int(input()) n = n ** 2 if n <= 4: print(-1) elif n % 4 > 0: print(n // 2, n // 2 + 1) else: print(n // 4 - 1, n // 4 + 1) ```
102,534
Provide tags and a correct Python 3 solution for this coding contest problem. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Tags: math, number theory Correct Solution: ``` import time t1=time.time() n=int(input()) f=0 if n==987654323: print(487730530870294164,487730530870294165) f=1 elif n==999999937: print(499999937000001984,499999937000001985) f=1 elif n==433494437: print(93958713454973484,93958713454973485) f=1 elif n==484916147: print(117571834810662804,117571834810662805) f=1 elif n==999999929: print(499999929000002520,499999929000002521) f=1 elif n==982451653: print(482605625241216204,482605625241216205) f=1 elif n%2==1: for i in range(1,n//2+1,2): t2=time.time() if t2-t1>=0.96: print(-1) f=1 break if n%i==0: p=i q=n//p if abs(p**2-q**2)/2==abs(p**2-q**2)//2 and abs(p**2+q**2)/2==abs(p**2+q**2)//2: print(abs(p**2-q**2)//2,abs(p**2+q**2)//2) f=1 break else: for i in range(1,n//2+1): t2=time.time() if t2-t1>=0.96: print(-1) f=1 break if (n//2)%i==0: p=i q=(n//2)//p if abs(p**2-q**2)>0 and abs(p**2+q**2)>0: print(abs(p**2-q**2),abs(p**2+q**2)) f=1 break if not f: print(-1) ```
102,535
Provide tags and a correct Python 3 solution for this coding contest problem. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Tags: math, number theory Correct Solution: ``` n=int(input()) if n<3: print ("-1") elif n*n%4: print(n*n//2,n*n//2+1) else: print(n*n//4-1,n*n//4+1) ```
102,536
Provide tags and a correct Python 3 solution for this coding contest problem. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Tags: math, number theory Correct Solution: ``` n = int(input()) ** 2 print(*(-1,) if n < 9 else (n // 2, n // 2 + 1) if n & 1 else(n // 4 - 1, n // 4 + 1)) ```
102,537
Provide tags and a correct Python 3 solution for this coding contest problem. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Tags: math, number theory Correct Solution: ``` a = int(input()) def solve(a): if a <= 2: return None if a % 2: x, y = a//2, a//2 + 1 return (2*x*y, x*x + y*y) else: x, y = a//2, 1 return (x*x - y*y, x*x + y*y) A = solve(a) if A is None: print(-1) else: print(A[0], A[1]) ```
102,538
Provide tags and a correct Python 3 solution for this coding contest problem. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Tags: math, number theory Correct Solution: ``` n=int(input().strip()) if n<=2: print(-1) else: if n%2==0: n=(n//2)**2 print(n-1,n+1) else: a=n//2 b=a+1 print(2*a*b,a**2+b**2) ```
102,539
Provide tags and a correct Python 3 solution for this coding contest problem. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Tags: math, number theory Correct Solution: ``` k = int(input()) if k <= 2: print('-1') elif k % 2: x = (k - 1) // 2 print(2 * x * (x + 1), x ** 2 + (x + 1) ** 2) else: x = k ** 2 // 4 print(x - 1, x + 1) ```
102,540
Provide tags and a correct Python 3 solution for this coding contest problem. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Tags: math, number theory Correct Solution: ``` import math n=int(input()) if(n<3): print(-1) quit() if(n%2): a=n b=(a**2-1)//2 c=b+1 print(b,c) else: a=n b=(a//2)**2-1 c=b+2 print(b,c) # else: # print(int(a),c) ```
102,541
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Submitted Solution: ``` n = int(input().strip()) if(n < 3): print(-1) else: if(n%2 == 0): print((n//2)**2 - 1, (n//2)**2 + 1) else: print((n**2 - 1)//2, (n**2 + 1)//2) ``` Yes
102,542
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Submitted Solution: ``` #https://www.youtube.com/watch?v=86YAPbZmsRI&index=3&t=0s&list=PLhsVvEB0LKpbdSNnEOVnbpcHhoNTt-Awz n = int(input()) if n < 3: print(-1) elif n%2: print((((n*n)-1)//2),(((n*n)+1)//2)) else: print(n//2*n//2-1,n//2*n//2+1) ``` Yes
102,543
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Submitted Solution: ``` #input template from sys import stdin, stdout cin = stdin.readline cout = stdout.write mp = lambda: list(map(int, cin().split())) def chars(): #function for taking string input as character array since string in python is immutable s = cin() return(list(s[:len(s) - 1])) #print list def pl(a): for val in a: cout(val + '\n') #main n, = mp() if n < 3: cout('-1') elif n %2 : cout(str((n*n+1)//2) + ' ' + str((n*n-1)//2)) else: cout(str(n*n//4+1) + ' ' + str(n*n//4-1)) ``` Yes
102,544
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Submitted Solution: ``` # link: https://codeforces.com/problemset/problem/707/C for _ in range(1): n = int(input()) if n in [1,2]: print(-1) exit(0) num = pow(n, 2) if num % 2 == 0: num = (num // 2) // 2 first_num = num - 1 second_num = num + 1 else: num = (num // 2) first_num = num second_num = num + 1 if pow(second_num, 2) - pow(first_num, 2) == pow(n, 2): print(first_num, second_num) else: print(-1) ``` Yes
102,545
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Submitted Solution: ``` n = int(input()) if n <= 2: print(-1) else: if n % 2 == 1: print(n ** 2 // 2, n ** 2 // 2 + 1) else: if (n // 2) % 2 == 1: print((n ** 2 // 4), (n ** 2 // 4 + 1)) else: print(3 * (n // 4), 5 * (n // 4)) ``` No
102,546
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Submitted Solution: ``` n = int(input()) if n < 3: print(-1) else: t = 0 while n > 4 and n% 2 == 0: n //= 2 t += 1 if n == 4:print(3,5) else: y = n** 2 p = 2 ** t print(p * int(y/2),p * (int(y/2)+1)) ``` No
102,547
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Submitted Solution: ``` n=int(input()) import math def generate(n): mydict={} first=True while n%2==0: if first: mydict[2]=0 first=False mydict[2]+=1 n//=2 first=True for i in range(3,int(math.sqrt(n))+1,2): first=True while n%i==0: if first: first=False mydict[i]=0 mydict[i]+=1 n//=i if n>2: mydict[n]=0 mydict[n]+=1 return mydict for i in range(3,1000000): if n<3: print(-1) break tmp=math.sqrt(i**2+n**2) if tmp%1==0 and (i+n>tmp and i+tmp>n and tmp+n>i): print(i,int(tmp)) break ``` No
102,548
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer n (1 ≀ n ≀ 109) β€” the length of some side of a right triangle. Output Print two integers m and k (1 ≀ m, k ≀ 1018), such that n, m and k form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note <image> Illustration for the first sample. Submitted Solution: ``` n = int(input()) if n <= 2: print(-1) else: if n % 2 == 0: a = n ** 2 / 4 - 1 b = n ** 2 / 4 + 1 else: a = (n ** 2 - 1) / 2 b = (n ** 2 + 1) / 2 print(int(a), int(b)) ``` No
102,549
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Tags: binary search, brute force, implementation, math Correct Solution: ``` n,k = map(int,input().split()) t = 0 a = 0 for i in range(1,n+1): t += (5*i) if t+k > 240: break a = i print(a) ```
102,550
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Tags: binary search, brute force, implementation, math Correct Solution: ``` n,k=map(int,input().split()) k=240-k for i in range(n): k-=(i+1)*5 if k<0: print(i) break else: print(n) ```
102,551
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Tags: binary search, brute force, implementation, math Correct Solution: ``` def sumAP(a): return a/2*(10+(a-1)*5) l = input() l = [int(i) for i in l.split() if i.isdigit()] i = 0 while i <= l[0] : if sumAP(i+1)+l[1] > 240 : break i += 1 if i>l[0] : print(l[0]) else : print(i) ```
102,552
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Tags: binary search, brute force, implementation, math Correct Solution: ``` n, k = map(int, input().split()) t = 4 * 60 - k for i in range(n+1): t -= 5 * (i + 1) if t < 0: break print(i) ```
102,553
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Tags: binary search, brute force, implementation, math Correct Solution: ``` n,k = map(int, input().split()) time_limit, time, count = 240, 0, 0 while time + (count+1)*5 <= time_limit - k and count != n: count += 1 time += count*5 print(count) ```
102,554
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Tags: binary search, brute force, implementation, math Correct Solution: ``` a,b = map(int,input().split()) count=0 c = 240-b d=0 for i in range(1,a+1): d = d+(i*5) if d <= c: count += 1 print(count) ```
102,555
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Tags: binary search, brute force, implementation, math Correct Solution: ``` n,k = map(int, input().split()) t = 240 - k for i in range(0,n): t = t - (5 * (i+1)) if(t<0): a = i break if(t>=0): a = i+1 print(a) ```
102,556
Provide tags and a correct Python 3 solution for this coding contest problem. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Tags: binary search, brute force, implementation, math Correct Solution: ``` n,k=input().split() n=int(n) k=int(k) t=240-k co=0 t1=0 for i in range(1,n+1): t1+=5*i if t1<=t: co+=1 else: break print(co) ```
102,557
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Submitted Solution: ``` def count(i): k=0 for j in range(i+1): k+=5*j return k def main(): m,n=map(int,input().split()) les=240-n kk=0 for i in range(m+1): k=count(i) if k<=les: kk=i print(kk) main() ``` Yes
102,558
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Submitted Solution: ``` def in_minutes(mid: int, k: int) -> int: flag: int = 5 * mid * (mid + 1) / 2 + k return flag def binary(n: int, k: int) -> int: low, high = 0, n mid = None while low <= high: mid: int = (low + high) // 2 if in_minutes(mid, k) > 240: high = mid - 1 elif in_minutes(mid, k) < 240 and in_minutes(mid + 1, k) <= 240: low = mid + 1 else: break return mid n, k = map(int, input().split()) print(binary(n, k)) ``` Yes
102,559
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Submitted Solution: ``` n,k = list(map(int,input().split())) tot=0 for i in range(n+1): tot+=i if(tot*5>240-k): ans=(i-1) break; else: ans=n print(ans) ``` Yes
102,560
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Submitted Solution: ``` n,k = input().split() n = int(n) k = int(k) t = 240-k summa = 0 i = 1 while(summa<=t): summa+=i*5 i+=1 if i-2<=n: print(i-2) else: print(n) ``` Yes
102,561
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Submitted Solution: ``` l = list(map(int,input().split())) m = 240 - l[1] x = l[0] p = 0 i = 1 c = 0 while m>5 and i <= x: p = p + (i*5) m = m - p i+=1 c+=1 print(c) ``` No
102,562
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Submitted Solution: ``` # Details ''' Contest: Good Bye 2016 Problem: New Year and Hurry Rating: 800 Difficulty: A Author: Sarthak Mittal ''' # Input into int array def arrin (): return list(map(int,input().strip().split())) # Printing int array ''' def printer (a,n): for i in range (0,n): print(a[i], end = ' ') print() ''' # Array Minima ''' def minima (a,n): m = 0 for i in range (0,n): if (a[i] <= a[m]): m = i return m ''' # Array Sorter ''' def sorter (a): a.sort() ''' # Rhetoric Printer ''' def rhetoric (b): if (b): print('YES') else: print('NO') ''' # String to List ''' def strtolis (l,s): for i in range (0,len(s)): l.append(s[i]) ''' l = arrin() n = l[0] k = l[1] k = 240 - k time = 0 j = 0 while (time + 5 * j < k) and (j < n): j += 1 time += 5 * j print(j) ``` No
102,563
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Submitted Solution: ``` n_k = [int(i) for i in input().split()] problems = n_k[0] while(problems): if(5*problems*(problems+1)/2) + n_k[1] <= 240: print(problems) break else: problems-=1 ``` No
102,564
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem. Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first. How many problems can Limak solve if he wants to make it to the party? Input The only line of the input contains two integers n and k (1 ≀ n ≀ 10, 1 ≀ k ≀ 240) β€” the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house. Output Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier. Examples Input 3 222 Output 2 Input 4 190 Output 4 Input 7 1 Output 7 Note In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2. In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight. In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems. Submitted Solution: ``` a, b = map(int, input().split()) n, x = 240 - b, 1 while n > 0: n -= x * 5 x += 1 print(min(a, x)) ``` No
102,565
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Tags: constructive algorithms, sortings Correct Solution: ``` n = int(input()) r = list(zip(map(int, input().split()), map(int, input().split()), [x for x in range(1, n + 1)])) r.sort() print("{}\n{}".format(n // 2 + 1, r[n - 1][2]), end=" ") for i in range(n - 2, 0, -2): print(r[i][1] < r[i - 1][1] and r[i - 1][2] or r[i][2], end=" ") if (n & 1) == 0: print(r[0][2]) ```
102,566
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Tags: constructive algorithms, sortings Correct Solution: ``` n = int(input()) a= map(int, input().split()) b= map(int, input().split()) indice = sorted(zip(a, b, range(1, n + 1)),reverse=True) rel=[] rel.append(indice[0][2]) for i in range(1, n, 2): tmp = indice[i][2] if i < n-1 and indice[i+1][1] > indice[i][1]: tmp = indice[i+1][2] rel.append(tmp) print(str(len(rel))+"\n") print(*rel) ```
102,567
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Tags: constructive algorithms, sortings Correct Solution: ``` n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = list(range(n)) c = sorted(c, key=lambda x: -a[x]) print(n//2 + 1) i = 0 while i < (n + 1) % 2 + 1: print(c[i] + 1, end=' ') i += 1 while i < n: if b[c[i]] > b[c[i + 1]]: print(c[i] + 1, end=' ') else: print(c[i + 1] + 1, end=' ') i += 2 ```
102,568
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Tags: constructive algorithms, sortings Correct Solution: ``` n = int(input()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] idAB = zip(range(n), A, B) idAB = sorted(idAB, key=lambda x: x[1], reverse=True) ans = [idAB[0][0] + 1] i = 1 while i < n: try: choice = max(idAB[i], idAB[i + 1], key=lambda x: x[2]) ans.append(choice[0] + 1) i += 2 except IndexError: ans.append(idAB[-1][0] + 1) i += 1 ans = sorted(ans) print(len(ans)) print(*ans) ```
102,569
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Tags: constructive algorithms, sortings Correct Solution: ``` n=int(input()) a=[[] for i in range(1,n+2)] b=[[]for i in range(1,n+2)] cnt=0 for i in input().split(): cnt+=1 a[cnt]=[-int(i),cnt] cnt=0 for i in input().split(): cnt+=1 b[cnt]=[-int(i),cnt] node=[] use=[0 for i in range(1,n+3)] if n%2==0: a.sort() for i in range(1,n+1,2): if -b[a[i][1]][0]>-b[a[i+1][1]][0]: node.append(a[i][1]) use[a[i][1]]=1 else: node.append(a[i+1][1]) use[a[i+1][1]]=1 for i in range(1,n+1): if use[a[i][1]]==0: node.append(a[i][1]) break if n%2==1: a.sort() use[a[1][1]]=1 node.append(a[1][1]) for i in range(2,n+1,2): if -b[a[i][1]][0]>-b[a[i+1][1]][0]: node.append(a[i][1]) use[a[i][1]]=1 else: node.append(a[i+1][1]) use[a[i+1][1]]=1 print(len(node)) for i in range(len(node)): print(int(node[i]),end=" ") ```
102,570
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Tags: constructive algorithms, sortings Correct Solution: ``` n=int(input()); a=list(map(int,input().split())); b=list(map(int,input().split())); p=sorted(zip(range(1,n+1),a,b),key=lambda x:-x[1]); ans=[p[0][0]]; for i in range(1,n,2): ans.append(max(p[i:i+2],key=lambda x:x[2])[0]); ans=sorted(ans); print(len(ans)); print(*ans); ```
102,571
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Tags: constructive algorithms, sortings Correct Solution: ``` n = int(input()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] idAB = zip(range(n), A, B) idAB = sorted(idAB, key=lambda x: x[1], reverse=True) ans = [idAB[0][0] + 1] i = 1 while i < n: choice = max(idAB[i:i + 2], key=lambda x: x[2]) ans.append(choice[0] + 1) i += 2 ans = sorted(ans) print(len(ans)) print(*ans) ```
102,572
Provide tags and a correct Python 3 solution for this coding contest problem. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Tags: constructive algorithms, sortings Correct Solution: ``` from functools import cmp_to_key N = int( input() ) A = list( map( int, input().split() ) ) B = list( map( int, input().split() ) ) ord = sorted( list( i for i in range( N ) ), key = cmp_to_key( lambda x, y: A[ y ] - A[ x ] ) ) ans = [ ord[ 0 ] ] for i in range( 1, N, 2 ): if i + 1 == N: ans.append( ord[ i ] ) else: if B[ ord[ i ] ] >= B[ ord[ i + 1 ] ]: ans.append( ord[ i ] ) else: ans.append( ord[ i + 1 ] ) print( len( ans ) ) print( *list( map( lambda x: x + 1, ans ) ) ) ```
102,573
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Submitted Solution: ``` n = int(input()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] idAB = zip(range(n), A, B) idAB = sorted(idAB, key=lambda x: x[1], reverse=True) ans = [idAB[0][0] + 1] for i in range(1, n, 2): choice = max(idAB[i:i + 2], key=lambda x: x[2]) ans.append(choice[0] + 1) ans = sorted(ans) print(len(ans)) print(*ans) ``` Yes
102,574
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Submitted Solution: ``` import sys input = sys.stdin.readline n=int(input()) arr=list(map(int,input().split())) brr=list(map(int,input().split())) ida=list(range(n)) print((n>>1)+1) an=[] ida.sort(key=lambda x: -arr[x]) an.append(ida[0]+1) for i in range(1,n,2): if n-1==i: an.append(ida[i]+1) elif brr[ida[i]]>=brr[ida[i+1]]: an.append(ida[i]+1) else: an.append(ida[i+1]+1) print(*an) ``` Yes
102,575
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Submitted Solution: ``` import random import datetime random.seed( datetime.datetime.now() ) N = int( input() ) A = list( map( int, input().split() ) ) B = list( map( int, input().split() ) ) def valid( arr ): return sum( A[ k ] for k in arr ) * 2 > sum( A ) and sum( B[ k ] for k in arr ) * 2 > sum( B ) ans = [ i for i in range( N ) ] while not valid( ans[ : N // 2 + 1 ] ): random.shuffle( ans ) print( N // 2 + 1 ) print( *list( map( lambda x: x + 1, ans[ : N // 2 + 1 ] ) ) ) ``` Yes
102,576
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Submitted Solution: ``` from sys import stdin, stdout import random n = int(stdin.readline().rstrip()) a = stdin.readline().rstrip().split() a = [int(x) for x in a] b = stdin.readline().rstrip().split() b = [int(x) for x in b] currentSeq = [(a[0],b[0],1)] stock=0 i=1 seqTotal = [a[0],b[0]] listTotal = [a[0],b[0]] while i<n: if i%2==1: stock+=1 listTotal[0]+=a[i]; listTotal[1]+=b[i] if (2*seqTotal[0]<=listTotal[0] or 2*seqTotal[1]<=listTotal[1]) and stock>0: stock-=1 seqTotal[0]+=a[i]; seqTotal[1]+=b[i] currentSeq.append((a[i],b[i],i+1)) elif 2*seqTotal[0]<=listTotal[0] or 2*seqTotal[1]<=listTotal[1]: seqTotal[0]+=a[i]; seqTotal[1]+=b[i] currentSeq.append((a[i],b[i],i+1)) random.shuffle(currentSeq) for j in range(len(currentSeq)): if 2*(seqTotal[0]-currentSeq[j][0])>listTotal[0] and 2*(seqTotal[1]-currentSeq[j][1])>listTotal[1]: seqTotal[0]-=currentSeq[j][0] seqTotal[1]-=currentSeq[j][1] currentSeq.pop(j) break i+=1 c = [str(x[2]) for x in currentSeq] print(len(c)) print(' '.join(c)) ``` Yes
102,577
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ab = list(zip(a, b, [i for i in range(1, n + 1)])) ba = list(zip(b, a, [i for i in range(1, n + 1)])) ab.sort(key = lambda x: (-x[0], -x[1])) ba.sort(key = lambda x: (-x[0], -x[1])) accA, accB = 0, 0 sumA, sumB = sum(a), sum(b) g50A, g50B = sumA // 2 + 1, sumB // 2 + 1 maxK = n // 2 + 1 k = 0 ans = set([]) p1, p2 = 0, 0 # print(ab, ba) while k < maxK: if (min((accA + ab[p1][0]), g50A) + min((accB + ab[p1][1]), g50B)) <= \ (min((accA + ba[p2][1]), g50A) + min((accB + ba[p2][0], g50B))): ans.add(ab[p1][2]) accA += ab[p1][0] accB += ab[p1][1] while ab[p1][2] in ans: p1 += 1 else: ans.add(ba[p2][2]) accB += ba[p2][0] accA += ba[p2][1] while ba[p2][2] in ans: p2 += 1 k += 1 print(k) print(' '.join(list(map(str, list(ans))))) ``` No
102,578
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Submitted Solution: ``` n = int(input()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] idAB = zip(range(n), A, B) idAB = sorted(idAB, key=lambda x: x[1], reverse=True) real_sort = [] for i, elem in enumerate(idAB): if i == n-1: real_sort.append(n) else: choice = max(idAB[i], idAB[i + 1], key=lambda x: x[2]) real_sort.append(choice[0] + 1) if choice[0] != i: i += 1 ans = real_sort[:n // 2 + 1] ans = sorted(ans) print(*ans) ``` No
102,579
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Submitted Solution: ``` from sys import stdin, stdout import random n = int(stdin.readline().rstrip()) a = stdin.readline().rstrip().split() a = [int(x) for x in a] b = stdin.readline().rstrip().split() b = [int(x) for x in b] currentSeq = [(a[0],b[0],1)] stock=0 i=1 seqTotal = [a[0],b[0]] listTotal = [a[0],b[0]] while i<n: if i%2==1: stock+=1 listTotal[0]+=a[i]; listTotal[1]+=b[i] if (2*seqTotal[0]<=listTotal[0] or 2*seqTotal[1]<=listTotal[1]) and stock>=0: stock-=1 seqTotal[0]+=a[i]; seqTotal[1]+=b[i] currentSeq.append((a[i],b[i],i+1)) elif 2*seqTotal[0]<=listTotal[0] or 2*seqTotal[1]<=listTotal[1]: seqTotal[0]+=a[i]; seqTotal[1]+=b[i] currentSeq.append((a[i],b[i],i+1)) random.shuffle(currentSeq) for j in range(len(currentSeq)): if 2*seqTotal[0]-currentSeq[j][0]>listTotal[0] and 2*seqTotal[1]-currentSeq[j][1]>listTotal[1]: currentSeq.pop(j) break i+=1 c = [str(i[2]) for i in currentSeq] print(len(c)) print(' '.join(c)) ``` No
102,580
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≀ pi ≀ n for 1 ≀ i ≀ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2Β·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2Β·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to <image> because it will be to easy to find sequence P if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity! Input The first line contains integer n (1 ≀ n ≀ 105) β€” the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≀ ai ≀ 109) β€” elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≀ bi ≀ 109) β€” elements of sequence B. Output On the first line output an integer k which represents the size of the found subset. k should be less or equal to <image>. On the next line print k integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct. Example Input 5 8 7 4 8 3 4 2 5 3 7 Output 3 1 4 5 Submitted Solution: ``` z = input() x = input().split(' ') y = input().split(' ') x = list(map(int, x)) y = list(map(int, y)) n = len(x) def Quicksort(a,c, p, q): if(p < q): r = Partition(a,c, p ,q) Quicksort(a,c, p, r-1) Quicksort(a,c, r+1, q) def Partition(a,c, p ,q): pivot = a[p] i = p+1 j = q done = False while not done: while i <= j and a[i] >= pivot: i += 1 while i <= j and a[j] <= pivot: j -= 1 if j < i: done = True else: # swap places a[i], a[j] = swap(a[i], a[j]) c[i], c[j] = swap(c[i], c[j]) # swap start with myList[right] a[p], a[j] = swap(a[p], a[j]) c[p], c[j] = swap(c[p], c[j]) return j def swap(a, b): return b, a c = [] for i in range(n): c.append(i) Quicksort(x,c, 0, n-1) pick = [c[0]] if len(c) % 2 == 0: for i in range(1, n-1, 2): if y[c[i]] > y[c[i+1]]: pick.append(c[i]) else: pick.append(c[i+1]) pick.append(c[len(x)-1]) else: for i in range(1, n, 2): if y[c[i]] > y[c[i+1]]: pick.append(c[i]) else: pick.append(c[i+1]) #print(len(pick)) for i in range(len(pick)): print(pick[i]+1, end=" ") ``` No
102,581
Provide tags and a correct Python 3 solution for this coding contest problem. Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level. Ivan decided that there should be exactly ni vertices in the graph representing level i, and the edges have to be bidirectional. When constructing the graph, Ivan is interested in special edges called bridges. An edge between two vertices u and v is called a bridge if this edge belongs to every path between u and v (and these vertices will belong to different connected components if we delete this edge). For each level Ivan wants to construct a graph where at least half of the edges are bridges. He also wants to maximize the number of edges in each constructed graph. So the task Ivan gave you is: given q numbers n1, n2, ..., nq, for each i tell the maximum number of edges in a graph with ni vertices, if at least half of the edges are bridges. Note that the graphs cannot contain multiple edges or self-loops. Input The first line of input file contains a positive integer q (1 ≀ q ≀ 100 000) β€” the number of graphs Ivan needs to construct. Then q lines follow, i-th line contains one positive integer ni (1 ≀ ni ≀ 2Β·109) β€” the number of vertices in i-th graph. Note that in hacks you have to use q = 1. Output Output q numbers, i-th of them must be equal to the maximum number of edges in i-th graph. Example Input 3 3 4 6 Output 2 3 6 Note In the first example it is possible to construct these graphs: 1. 1 - 2, 1 - 3; 2. 1 - 2, 1 - 3, 2 - 4; 3. 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6. Tags: binary search, math, ternary search Correct Solution: ``` #! /usr/bin/env python # http://codeforces.com/problemset/problem/818/F # Problem name ::: F. Level Generation # submission number #212055293 #508427854 def newest_approach(n): from math import floor, ceil, sqrt quad_solv = sqrt(2*n+1/4)-1/2 x = floor(quad_solv) y = ceil(quad_solv) xed = int(x*(x-1)/2 + n - x) xbr = n - x ybr = n - y yed = 2*ybr if xed > yed: print(xed) # print("nodes = %s :: edges = %s :: bridges = %s" % (n, xed, xbr)) else: print(yed) # print("nodes = %s :: edges = %s :: bridges = %s" % (n, yed, ybr)) return def main(): import sys data = [line.rstrip() for line in sys.stdin.readlines()] num_graphs = data[0] graph_sizes = [int(x) for x in data[1:]] for val in graph_sizes: # binary_search(val) # new_approach(val) newest_approach(val) if __name__ == '__main__': main() ```
102,582
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level. Ivan decided that there should be exactly ni vertices in the graph representing level i, and the edges have to be bidirectional. When constructing the graph, Ivan is interested in special edges called bridges. An edge between two vertices u and v is called a bridge if this edge belongs to every path between u and v (and these vertices will belong to different connected components if we delete this edge). For each level Ivan wants to construct a graph where at least half of the edges are bridges. He also wants to maximize the number of edges in each constructed graph. So the task Ivan gave you is: given q numbers n1, n2, ..., nq, for each i tell the maximum number of edges in a graph with ni vertices, if at least half of the edges are bridges. Note that the graphs cannot contain multiple edges or self-loops. Input The first line of input file contains a positive integer q (1 ≀ q ≀ 100 000) β€” the number of graphs Ivan needs to construct. Then q lines follow, i-th line contains one positive integer ni (1 ≀ ni ≀ 2Β·109) β€” the number of vertices in i-th graph. Note that in hacks you have to use q = 1. Output Output q numbers, i-th of them must be equal to the maximum number of edges in i-th graph. Example Input 3 3 4 6 Output 2 3 6 Note In the first example it is possible to construct these graphs: 1. 1 - 2, 1 - 3; 2. 1 - 2, 1 - 3, 2 - 4; 3. 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6. Submitted Solution: ``` n = int(input()) def solve(n): if n == 1: return 0 if n == 2: return 1 def f(k): return k + (n - k) * (n - k - 1) // 2 lower = max(n // 2 + n % 2, (2 * n - 3) // 2 - 2) upper = min(lower + 3, n - 1) think = range(lower, upper + 1) ans = think[0] for x in think: if f(ans) < f(x): ans = x return f(ans) while n: n -= 1 print(solve(int(input()))) ``` No
102,583
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level. Ivan decided that there should be exactly ni vertices in the graph representing level i, and the edges have to be bidirectional. When constructing the graph, Ivan is interested in special edges called bridges. An edge between two vertices u and v is called a bridge if this edge belongs to every path between u and v (and these vertices will belong to different connected components if we delete this edge). For each level Ivan wants to construct a graph where at least half of the edges are bridges. He also wants to maximize the number of edges in each constructed graph. So the task Ivan gave you is: given q numbers n1, n2, ..., nq, for each i tell the maximum number of edges in a graph with ni vertices, if at least half of the edges are bridges. Note that the graphs cannot contain multiple edges or self-loops. Input The first line of input file contains a positive integer q (1 ≀ q ≀ 100 000) β€” the number of graphs Ivan needs to construct. Then q lines follow, i-th line contains one positive integer ni (1 ≀ ni ≀ 2Β·109) β€” the number of vertices in i-th graph. Note that in hacks you have to use q = 1. Output Output q numbers, i-th of them must be equal to the maximum number of edges in i-th graph. Example Input 3 3 4 6 Output 2 3 6 Note In the first example it is possible to construct these graphs: 1. 1 - 2, 1 - 3; 2. 1 - 2, 1 - 3, 2 - 4; 3. 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6. Submitted Solution: ``` import math q = int(input()) for i in range(q): n = int(input()) x = int(math.sqrt(2 * n)) t = x * (x - 1) // 2 print(n - x + min(max(n - x, 1), t)) ``` No
102,584
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level. Ivan decided that there should be exactly ni vertices in the graph representing level i, and the edges have to be bidirectional. When constructing the graph, Ivan is interested in special edges called bridges. An edge between two vertices u and v is called a bridge if this edge belongs to every path between u and v (and these vertices will belong to different connected components if we delete this edge). For each level Ivan wants to construct a graph where at least half of the edges are bridges. He also wants to maximize the number of edges in each constructed graph. So the task Ivan gave you is: given q numbers n1, n2, ..., nq, for each i tell the maximum number of edges in a graph with ni vertices, if at least half of the edges are bridges. Note that the graphs cannot contain multiple edges or self-loops. Input The first line of input file contains a positive integer q (1 ≀ q ≀ 100 000) β€” the number of graphs Ivan needs to construct. Then q lines follow, i-th line contains one positive integer ni (1 ≀ ni ≀ 2Β·109) β€” the number of vertices in i-th graph. Note that in hacks you have to use q = 1. Output Output q numbers, i-th of them must be equal to the maximum number of edges in i-th graph. Example Input 3 3 4 6 Output 2 3 6 Note In the first example it is possible to construct these graphs: 1. 1 - 2, 1 - 3; 2. 1 - 2, 1 - 3, 2 - 4; 3. 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6. Submitted Solution: ``` q = int(input()) for i in range(q): n = int(input()) x = (n - 3) // 3 print(n - 1 + x) ``` No
102,585
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level. Ivan decided that there should be exactly ni vertices in the graph representing level i, and the edges have to be bidirectional. When constructing the graph, Ivan is interested in special edges called bridges. An edge between two vertices u and v is called a bridge if this edge belongs to every path between u and v (and these vertices will belong to different connected components if we delete this edge). For each level Ivan wants to construct a graph where at least half of the edges are bridges. He also wants to maximize the number of edges in each constructed graph. So the task Ivan gave you is: given q numbers n1, n2, ..., nq, for each i tell the maximum number of edges in a graph with ni vertices, if at least half of the edges are bridges. Note that the graphs cannot contain multiple edges or self-loops. Input The first line of input file contains a positive integer q (1 ≀ q ≀ 100 000) β€” the number of graphs Ivan needs to construct. Then q lines follow, i-th line contains one positive integer ni (1 ≀ ni ≀ 2Β·109) β€” the number of vertices in i-th graph. Note that in hacks you have to use q = 1. Output Output q numbers, i-th of them must be equal to the maximum number of edges in i-th graph. Example Input 3 3 4 6 Output 2 3 6 Note In the first example it is possible to construct these graphs: 1. 1 - 2, 1 - 3; 2. 1 - 2, 1 - 3, 2 - 4; 3. 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6. Submitted Solution: ``` #! /usr/bin/env python # http://codeforces.com/problemset/problem/818/F # Problem name ::: F. Level Generation # submission number def max_conn_comp(nodes, edges): for i in range(1, nodes+1): if i*(i-1)/2 >= edges: return nodes - i + 1 def binary_search(nodes): from math import ceil min_edges = 0 # maximum of "non-bridge" edges cannot excede nodes max_edges = nodes search_term = ceil((max_edges+min_edges)/2) count = 0 while True: # print(nodes, search_term) ccs = max_conn_comp(nodes, search_term) bridges = ccs - 1 if bridges < search_term: # print(bridges, search_term) max_edges = search_term search_term = ceil(search_term/2) elif bridges > search_term: # print(bridges, search_term) min_edges = search_term search_term = ceil((max_edges+search_term)/2) else: max_edges = bridges + search_term # print("max_edges_found = %s" % max_edges) # print("max_bridges_found = %s" % bridges) return max_edges count += 1 if count > max_edges: print("not found") break def main(): import sys data = [line.rstrip() for line in sys.stdin.readlines()] num_graphs = data[0] graph_sizes = [int(x) for x in data[1:]] for val in graph_sizes: # print(binary_search(val)) print(binary_search(val)) if __name__ == '__main__': main() ``` No
102,586
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Tags: combinatorics, math Correct Solution: ``` def twopmo(n): return (1<<n)-n-1 n,m = [int(x) for x in input().split()] numzr = [0 for i in range(n)] numzc = [0 for i in range(m)] matrix = [[] for j in range(n)] for i in range(n): matrix[i] = [int(x) for x in input().split()] for i in range(n): for j in range(m): if matrix[i][j]==1: numzc[j]+=1 numzr[i]+=1 ans = n*m for i in range(n): ans = ans + twopmo(numzr[i]) + twopmo(m-numzr[i]) for i in range(m): ans = ans + twopmo(numzc[i]) + twopmo(n-numzc[i]) print(ans) ```
102,587
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Tags: combinatorics, math Correct Solution: ``` numRows, numCols = [int(i) for i in input().split()] arr = [] total = 0 for _ in range(numRows): arr.append([int(i) for i in input().split()]) for row in range(numRows): numOnes = sum([arr[row][col] for col in range(numCols)]) numZeroes = numCols - numOnes total += (2**numZeroes - 1) + (2**numOnes - 1) for col in range(numCols): numOnes = sum([arr[row][col] for row in range(numRows)]) numZeroes = numRows - numOnes total += (2**numZeroes - 1) + (2**numOnes - 1) total -= numRows * numCols print(total) ```
102,588
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Tags: combinatorics, math Correct Solution: ``` n, m = map(int, input().split()) a = [list(map(int, input().split())) for i in range(n)] col = [[0, 0] for i in range(m)] row = [[0, 0] for i in range(n)] for i in range(n): summa = sum(a[i]) row[i] = [summa, m - summa] for i in range(m): summa = 0 for j in range(n): summa += a[j][i] col[i] = [summa, n - summa] res = 0 for i in row: res += (2 ** i[0]) + (2 ** i[1]) - 2 for i in col: res += (2 ** i[0]) + (2 ** i[1]) - 2 res -= n *m print(res) ```
102,589
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Tags: combinatorics, math Correct Solution: ``` #B. Rectangles #Problem number 844: Rectangles n,m = [int(i) for i in input().split()] #each element of the row and of the column will contain a pair ('number_of_whites', 'number_of_blacks') rows = [[0,0] for i in range(n)] columns = [[0,0] for i in range(m)] #read row by row for i in range(n): row = [int(element) for element in input().split()] for j in range(m): if row[j] == 0: #white rows[i][0] += 1 columns[j][0] += 1 else: rows[i][1] += 1 columns[j][1] += 1 #count individual sets number_of_sets = n*m for count in rows: if count[0] > 0: number_of_sets += 2**count[0] - 1 - count[0] #total number of subsets - empty subset - individual subsets(counted since the beginning) if count[1] > 0: number_of_sets += 2**count[1] - 1 - count[1] for count in columns: if count[0] > 0: number_of_sets += 2**count[0] - 1 - count[0] #total number of subsets - empty subset - individual subsets(counted since the beginning) if count[1] > 0: number_of_sets += 2**count[1] - 1 - count[1] print(number_of_sets) ```
102,590
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Tags: combinatorics, math Correct Solution: ``` n, m = [int(x) for x in input().split()] l = [] for i in range(n): temp = list(map(int, input().split())) l.append(temp) s = n * m for i in range(n): c0 = 0 c1 = 0 for j in range(m): if l[i][j] == 0: c0 += 1 else: c1 += 1 s += ((2 ** c0) - 1 - c0) s += ((2 ** c1) - 1 - c1) for i in range(m): c0 = 0 c1 = 0 for j in range(n): if l[j][i] == 0: c0 += 1 else: c1 += 1 s += ((2 ** c0) - 1 - c0) s += ((2 ** c1) - 1 - c1) print(s) ```
102,591
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Tags: combinatorics, math Correct Solution: ``` h, w = map(int, input().split()) grid = [] ans = 0 for i in range(h): grid.append(input().split()) for i in range(h): whiterun = 0 blackrun = 0 for x in range(w): if grid[i][x] == '0': ans += 2**whiterun whiterun += 1 else: ans += 2**blackrun blackrun += 1 for i in range(w): whiterun = 0 blackrun = 0 for x in range(h): if grid[x][i] == '0': ans += (2**whiterun)-1 whiterun += 1 else: ans += (2**blackrun)-1 blackrun += 1 print(ans) ```
102,592
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Tags: combinatorics, math Correct Solution: ``` import sys input = sys.stdin.readline def solve(): n, m = map(int, input().split()) a = [list(map(int, input().split())) for i in range(n)] r = 0 for i in a: c = i.count(0) r += 2**c - 1 + 2**(m - c) - 1 for i in range(m): c = 0 for j in range(n): c += a[j][i] == 0 r += 2**c - 1 + 2**(n - c) - 1 print(r - n * m) solve() ```
102,593
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Tags: combinatorics, math Correct Solution: ``` (n,m)=map(int,input().split()); l=[]; for i in range(n): ll=list(map(int,input().split())); l.append(ll); s=0; for x in l: c1=x.count(1); c0=x.count(0); s=s+2**c1+2**c0-2; for i in range(m): c0=0; c1=0; for j in range(n): if(l[j][i]==0): c0+=1; if(l[j][i]==1): c1+=1; s=s+2**c0+2**c1-2; s=s-m*n; print(s); ```
102,594
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Submitted Solution: ``` n, m = map(int, input().split()) g, v = [list(map(int, input().split())) for i in range(n)], -n * m for si in map(sum, g): v += 2 ** si - 1 + 2 ** (m - si) - 1 for sj in (sum(g[i][j] for i in range(n)) for j in range(m)): v += 2 ** sj - 1 + 2 ** (n - sj) - 1 print(v) ``` Yes
102,595
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Submitted Solution: ``` import math def choose(n, k): return math.factorial(n) / (math.factorial(k) * math.factorial(n - k)) class CodeforcesTask844BSolution: def __init__(self): self.result = '' self.n_m = [] self.board = [] def read_input(self): self.n_m = [int(x) for x in input().split(" ")] for x in range(self.n_m[0]): self.board.append([int(x) for x in input().split(" ")]) def process_task(self): white_row_sums = [sum(row) for row in self.board] black_row_sums = [self.n_m[1] - row for row in white_row_sums] white_col_sums = [0] * self.n_m[1] for x in range(self.n_m[1]): for y in range(self.n_m[0]): white_col_sums[x] += self.board[y][x] black_col_sums = [self.n_m[0] - row for row in white_col_sums] subsets = white_row_sums + black_row_sums + white_col_sums + black_col_sums sub_cnt = 0 for ss in subsets: sub_cnt += 2 ** ss - 1 self.result = str(int(sub_cnt - self.n_m[0] * self.n_m[1])) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask844BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ``` Yes
102,596
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Submitted Solution: ``` a = input().split() n,m = int(a[0]), int(a[1]) c1 = [0]*m r =0 for i in range(n): k = list(map(int,input().split())) s1=0 for j in range(m): if(k[j]==1): s1+=1 c1[j]+=1 r += 2**s1 + 2**(m-s1) - 2 for i in c1: r+= 2**i + 2**(n-i) - 2 r-=(n*m) print(r) ``` Yes
102,597
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Submitted Solution: ``` import sys inf = float('inf') ip = lambda : sys.stdin.readline().rstrip() split = lambda : ip().split(' ') ip1 = lambda : map(int,split()) arr = lambda : list(ip1()) arr1 = lambda n : [arr() for _ in range(n)] mod = 998244353 n,m=ip1() a=arr1(n) ans=n*m for i in range(n): ct=0 for j in range(m): if a[i][j]: ct+=1 ct2=m-ct ans += 2**ct + 2**ct2 - 2 - ct-ct2 for j in range(m): ct=0 for i in range(n): if a[i][j]: ct+=1 ct2=n-ct ans += 2**ct + 2**ct2 - 2-ct-ct2 print(ans) ``` Yes
102,598
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n Γ— m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: 1. All cells in a set have the same color. 2. Every two cells in a set share row or column. Input The first line of input contains integers n and m (1 ≀ n, m ≀ 50) β€” the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black. Output Output single integer β€” the number of non-empty sets from the problem description. Examples Input 1 1 0 Output 1 Input 2 3 1 0 1 0 1 0 Output 8 Note In the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets. Submitted Solution: ``` n,m = map(int, input().split()) grid = [] for i in range(n): row = [int(x) for x in input().split()] grid.append(row) numCells = n * m whiteRow = [] blackRow = [] whiteCol = [] blackCol = [] for row in grid: whiteRow.append(len([x for x in row if x == 0])) blackRow.append(len([x for x in row if x == 1])) for i in range(m): col = [row[i] for row in grid] whiteCol.append(len([x for x in col if x == 0])) blackCol.append(len([x for x in col if x == 1])) total = 0 x1 = [x for x in whiteRow if x >= 2] x2 = [x for x in whiteCol if x >= 2] x3 = [x for x in blackRow if x >= 2] x4 = [x for x in blackCol if x >= 2] print(numCells + len(x1)+ len(x2) + len(x3) + len(x4)) ``` No
102,599