message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≤ i ≤ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≤ x ≤ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≤ b_i ≤ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
instruction
0
65,824
12
131,648
Tags: constructive algorithms, data structures, greedy Correct Solution: ``` t = int(input()) while t: n = int(input()) a = [int(x) for x in input().split()] a.sort() b1, b2 = a[-1], a[-2] somatorio = sum(a) if somatorio - b1 == 2 * b2: a = [str(x) for x in a[:n]] print(" ".join(a)) else: for i in range(n+1): if somatorio - a[i] == 2 * b1: b = a[:i] + a[i+1:n+1] b = [str(x) for x in b[:n]] print(" ".join(b)) break else: print("-1") t -= 1 ```
output
1
65,824
12
131,649
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≤ i ≤ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≤ x ≤ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≤ b_i ≤ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
instruction
0
65,825
12
131,650
Tags: constructive algorithms, data structures, greedy Correct Solution: ``` for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr = sorted(arr) n+=2 pref = [0 for _ in range(n)] pref[0] = arr[0] for k in range(1, n): pref[k]+=arr[k]+pref[k-1] if pref[n-3]==arr[n-2]: print(*arr[:(n-2)]) elif pref[n-2] > arr[n-1] and pref[n-2]-arr[n-1] in arr[:(n-1)]: res = arr[:(n-1)] res.remove(pref[n-2]-arr[n-1]) print(*res) else: print("-1") ```
output
1
65,825
12
131,651
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≤ i ≤ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≤ x ≤ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≤ b_i ≤ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
instruction
0
65,826
12
131,652
Tags: constructive algorithms, data structures, greedy Correct Solution: ``` t = int(input()) for i in range(t): n = int(input()) b = [int(x) for x in input().split()] m, p = b[0], 0 sm = sum(b) for j in range(1, n + 2): if b[j] > m: m = b[j] p = j b = b[:p] + b[p + 1:] f = False for x in b: if x + 2 * m == sm: f = True break if f: b.remove(x) print(' '.join(str(y) for y in b)) else: for x in b: if m + 2 * x == sm: b.remove(x) print(' '.join(str(y) for y in b)) break else: print(-1) ```
output
1
65,826
12
131,653
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≤ i ≤ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≤ x ≤ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≤ b_i ≤ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6 Submitted Solution: ``` import bisect def print_nums(nums, i1, i2): for x in range(len(nums)): if x == i1 or x == i2: continue print(nums[x], end=' ') print() if __name__ == '__main__': for _ in range(int(input())): input() nums = sorted(map(int, input().split())) arr_sum = sum(nums) b = nums[-1] k = (arr_sum - b) - b # print(nums, b, k) index = bisect.bisect_left(nums, k) # print(index) if index < len(nums) and nums[index] == k: print_nums(nums, index, len(nums) - 1) continue b = nums[-2] k = arr_sum - b index = bisect.bisect_left(nums, k) if index < len(nums) and nums[index] == k: print_nums(nums, index, len(nums) - 2) continue print(-1) ```
instruction
0
65,831
12
131,662
No
output
1
65,831
12
131,663
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≤ i ≤ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≤ x ≤ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≤ b_i ≤ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6 Submitted Solution: ``` from collections import deque def ints(): return map(int,input().split()) def solve(arr): sett=set(arr) s=sum(arr) for e in arr: if (s-e)%2==0: if (s-e)//2 in sett: arr.remove(e) try: arr.remove((s-e)//2) except: continue return ' '.join(map(str,arr)) return -1 t=int(input()) for _ in range(t): n=int(input()) arr=list(ints()) print(solve(arr)) ```
instruction
0
65,832
12
131,664
No
output
1
65,832
12
131,665
Provide tags and a correct Python 3 solution for this coding contest problem. Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness. Input The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness. Output Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order. Examples Input 5 2 4 7 8 10 Output 3 Input 4 1 2 1 1 Output 2
instruction
0
65,878
12
131,756
Tags: brute force Correct Solution: ``` n = int(input()) arr = [int(x) for x in input().split()] arr_odd = [] arr_even = [] for i in range(n): if arr[i] % 2 == 1: arr_odd.append(i+1) else: arr_even.append(i+1) if len(arr_even) > len(arr_odd): print(arr_odd[0]) else: print(arr_even[0]) ```
output
1
65,878
12
131,757
Provide tags and a correct Python 3 solution for this coding contest problem. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image>
instruction
0
65,887
12
131,774
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` def main(): l, base, res, le, tot = [0] * 200001, [0] * 200001, [], 1, 0 for _ in range(int(input())): s = input() c = s[0] if c == '1': a, x = map(int, s[2:].split()) base[a] += x tot += a * x elif c == '2': l[le] = x = int(s[2:]) tot += x le += 1 else: x = base[le] base[le] = 0 tot -= x le -= 1 base[le] += x tot -= l[le] res.append(tot / le) print('\n'.join(map(str, res))) if __name__ == '__main__': main() ```
output
1
65,887
12
131,775
Provide tags and a correct Python 3 solution for this coding contest problem. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image>
instruction
0
65,892
12
131,784
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` suma = 0.0 items = 0 puros = [0] agreg = [0] cant = 1 queries = [] n = int(input()) for i in range(n): q = [int(x) for x in input().split()] if q[0] == 1: suma += q[1]*q[2] agreg[q[1]-1] += q[2] elif q[0] == 2: puros.append(q[1]) agreg.append(0) suma += q[1] cant += 1 else: #3 cant -= 1 agreg[-2] += agreg[-1] suma -= agreg.pop() + puros.pop() queries.append(suma/cant) print( "\n".join( [ "{0:.6f}".format(q) for q in queries ] ) ) ```
output
1
65,892
12
131,785
Provide tags and a correct Python 3 solution for this coding contest problem. Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 5 2 1 3 2 3 2 1 3 Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes <image>
instruction
0
65,894
12
131,788
Tags: constructive algorithms, data structures, implementation Correct Solution: ``` n=int(input()) s=0 c=1 l=[0] d=[0] ans=[0]*n for i in range(n): x=list(map(int,input().split())) if x[0]==1: d[x[1]-1]+=x[2] s=s+x[1]*x[2] elif x[0]==2: l.append(x[1]) d.append(0) s=s+x[1] c=c+1 else: t=d[-1] s=s-l[-1]-t c=c-1 l.pop() d.pop() d[-1]+=t ans[i]=str(s/c) print('\n'.join(ans)) ```
output
1
65,894
12
131,789
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
66,146
12
132,292
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` n = int(input()) array = list(map(int, input().split())) def gcd(a, b): if b > 0: return gcd(b, a % b) else: return a allGcd = array[0] for elem in array: allGcd = gcd(allGcd, elem) if allGcd > 1: print(-1) else: ones = 0 for elem in array: if elem == 1: ones += 1 if ones > 1: print(n - ones) else: res = 2000000000 for i in range(n): tmp = array[i] for j in range(i, n): tmp = gcd(tmp, array[j]) if tmp == 1: res = min(res, j-i) print(n + res - 1) ```
output
1
66,146
12
132,293
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
66,147
12
132,294
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` from math import gcd n = int(input()) j = [int(x) for x in input().split(" ")] def screen(l): o = [] for i in range(len(l)-1): c = gcd(l[i],l[i+1]) if c == 1: return 1 else: o.append(c) return o if n == 1: if j[0] == 1: print(0) else: print(-1) else: d = j.count(1) if d == 0: t = j for i in range(len(j)-1): r = screen(t) if r == 1: print(i+n) break else: t = r if t != 1: if len(t) ==1: print(-1) else: print(n-d) ```
output
1
66,147
12
132,295
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
66,148
12
132,296
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` import math n = int(input()) a = list(map(int, input().split())) a1 = a.count(1) if a1: print(n - a1) exit() ans = n for i in range(n): x = a[i] for j in range(i + 1, n): x = math.gcd(x, a[j]) if x == 1: ans = min(ans, j - i) break if ans != n: print(n + ans - 1) else: print(-1) ```
output
1
66,148
12
132,297
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
66,149
12
132,298
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` #trying to find the samllest segment whose gcd is 1 #not gonna use segment tree instead we will use a faster method to find gcd which includes prefix and suffix of gcd #refer to seg_gcd2.py in nov17 from math import gcd def check(val): for i in range(0,n,val): l=i r=min(n-1,i+val-1) pre[l]=arr[l] for j in range(l+1,r+1): pre[j]=gcd(pre[j-1],arr[j]) suf[r]=arr[r] for j in range(r-1,l-1,-1): suf[j]=gcd(suf[j+1],arr[j]) i=0 #print(val,n) while True: if i+val-1>=n: break if gcd(suf[i],pre[i+val-1])==1: return True i+=1 return False def binS(): l=0 r=n+1 flag=False while l < r: mid=(l+r)>>1 if mid < 1: break if check(mid): flag=True r=mid else: l=mid+1 if flag: return r else: return -1 n=int(input()) arr=[int(x) for x in input().split()] k=0 for i in arr: if i==1: k+=1 pre=[None]*n suf=[None]*n ans=binS() if ans==1: print(n-k) elif ans ==-1: print(-1) else: print(ans-1+n-1) ```
output
1
66,149
12
132,299
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
66,150
12
132,300
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` import math n=int(input()) a=list(map(int,input().split())) x=[1 for x in a if x==1] x=len(x) if(x>=1): print(n-x) quit() ans=n for i in range(n): g=a[i] for j in range(i,n): g=math.gcd(g,a[j]) if(g==1): ans=min(ans,j-i) if(ans==n): print(-1) else: print(ans+n-1) ```
output
1
66,150
12
132,301
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
66,151
12
132,302
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` def gcd(a, b): if a < b: a,b = b,a while b > 0: r = a % b a,b = b,r return a def solve(): n = int(input()) al = [int(i) for i in input().split()] if 1 in al: print(n - al.count(1)) return ans = -1 for i in range(n): t = al[i] for j in range(i + 1, n): t = gcd(t, al[j]) if t == 1: if ans == -1: ans = (j - i) + (n - 1) else: ans = min(ans, (j - i) + (n - 1)) print(ans) if __name__=="__main__": solve() ```
output
1
66,151
12
132,303
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
66,152
12
132,304
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` import math def slve(n,a): c1=a.count(1);ans=10**20 if c1>0:return n-c1 for i in range(n): g=a[i] for j in range(i+1,n): g=math.gcd(g,a[j]) if g==1:ans=min(ans,j-i) return -1 if ans==10**20 else n-1+ans n=int(input());a=list(map(int,input().split()));print(slve(n,a)) ```
output
1
66,152
12
132,305
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
66,153
12
132,306
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` from math import gcd def solve(nums, n): res = [] for i in range(n-1): res.append(gcd(nums[i], nums[i+1])) if 1 not in res: if n > 1: return solve(res, len(res)) elif n == 1: return -1 elif 1 in res: return n def main(): n = int(input()) nums = list(map(int, input().split(' '))) if 1 not in nums: res = solve(nums, n) if res == -1: print(-1) else: print(2*n-res) else: print(n - nums.count(1)) main() ```
output
1
66,153
12
132,307
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` n = int(input()) l = list(map(int, input().split())) cnt1 = l.count(1) if 0 < cnt1: print(n - cnt1) exit(0) import math m = n + 1 for i in range(n): g = l[i] for j in range(n - i): g = math.gcd(g, l[i + j]) if g == 1: m = min(m, j) if m == n + 1: print(-1) else: print(m + n - 1) # Made By Mostafa_Khaled ```
instruction
0
66,154
12
132,308
Yes
output
1
66,154
12
132,309
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` from math import gcd n = int(input()) a = list(map(int, input().split())) t = False l, r = -1, -1 if 1 in a: print(n - a.count(1)) exit(0) ll = a[0] for i in range(n): ll = a[i] for j in range(i, n): ll = gcd(ll, a[j]) if ll == 1: t = True if abs(i - j) + 1 < r - l + 1 or r == -1: r, l = max(i, j), min(i, j) if not t: print(-1) exit(0) print(n + r - l - 1) ```
instruction
0
66,155
12
132,310
Yes
output
1
66,155
12
132,311
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` n = int(input()) arr = list(map(int, input().split())) def GCD(x, y): if y > 0: return GCD(y, x % y) else: return x allGCD = arr[0] for i in arr: allGCD = GCD(allGCD, i) if allGCD > 1: print(-1) else: ones = 0 for i in arr: if i == 1: ones += 1 if ones > 1: print(n - ones) else: res = 2000000000 for i in range(n): tmp = arr[i] for j in range(i, n): tmp = GCD(tmp, arr[j]) if tmp == 1: res = min(res, j-i) print(n + res - 1) ''' 85% copied. LEL ''' ```
instruction
0
66,156
12
132,312
Yes
output
1
66,156
12
132,313
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` n=int(input('')) s=list(map(int,input('').split())) a=s.count(1) ans=0 def gcd(x,y): x,y=max(x,y),min(x,y) if y==0: return x return gcd(y,x%y) if a!=0: ans=n-a else: dp=[[0 for i in range(n)] for j in range(n)] for i in range(n): dp[i][i]=s[i] mins=n for i in range(n): for j in range(i,n): dp[i][j]=gcd(dp[i][j-1],s[j]) if dp[i][j]==1: mins=min(j-i,mins) if mins==n: ans=-1 else: ans=mins+(n-1) print(ans) ```
instruction
0
66,157
12
132,314
Yes
output
1
66,157
12
132,315
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` from math import gcd n = int(input()) a = list(map(int, input().split())) t = False l, r = -1, -1 if 1 in a: print(n - a.count(1)) exit(0) ll = a[0] for i in range(n): for j in range(i + 1, n): ll = gcd(ll, a[j]) if ll == 1: t = True if abs(i - j) + 1 < r - l + 1 or r == -1: r, l = max(i, j), min(i, j) if not t: print(-1) exit(0) print(n + ll) ```
instruction
0
66,158
12
132,316
No
output
1
66,158
12
132,317
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` a = int(input()) s = list(map(int, input().split())) def g(a, b): a, b = max(a, b), min(a, b) while a % b > 0: a, b = b, a % b return b b = 0 c = 0 for j in range(1, a): for i in range(1, a): v = g(s[i], s[i - 1]) if s[i] != s[i - 1]: if s[i] == 1: s[i - 1] = v else: s[i] = v c += 1 if v == 1: b = 1 break if s[-1] == 1: b += 1 if b: break if b or a == 1 and s[0] == 1: print(c + a - s.count(1)) else: print(-1) ```
instruction
0
66,159
12
132,318
No
output
1
66,159
12
132,319
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` #trying to find the samllest segment whose gcd is 1 #not gonna use segment tree instead we will use a faster method to find gcd which includes prefix and suffix of gcd #refer to seg_gcd2.py in nov17 from math import gcd def check(val): for i in range(0,n,val): l=i r=min(n-1,i+val-1) pre[l]=arr[l] for j in range(l+1,r+1): pre[j]=gcd(pre[j-1],arr[j]) suf[r]=arr[r] for j in range(r-1,l-1,-1): suf[j]=gcd(suf[j+1],arr[j]) i=0 #print(val,n) while True: if i+val-1>=n: break if gcd(suf[i],pre[i+val-1])==1: return True i+=1 return False def binS(): l=0 r=n+1 flag=False while l < r: mid=(l+r)>>1 if mid < 1: break if check(mid): flag=True r=mid else: l=mid+1 if flag: return r else: return -1 n=int(input()) arr=[int(x) for x in input().split()] pre=[None]*n suf=[None]*n ans=binS() if ans==1: print(n-1) elif ans==n: print(n+1) elif ans ==-1: print(-1) else: print(ans-1+n-1) ```
instruction
0
66,160
12
132,320
No
output
1
66,160
12
132,321
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` import math int(input()) a = [int(x) for x in input().split()] c = 0 for i in range(len(a)): if(a[i] == 1): c += 1 if(c > 1): print(len(a) - c) exit() x = 10000000 for i in range(len(a)): temp = a[i] for j in range(i + 1, len(a)): temp = math.gcd(a[j], temp) if(temp == 1): x = min(x, j - i) if(x == 10000000): print('-1') else: print(len(a) + x - 1) ```
instruction
0
66,161
12
132,322
No
output
1
66,161
12
132,323
Provide a correct Python 3 solution for this coding contest problem. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545
instruction
0
66,226
12
132,452
"Correct Solution: ``` n,m=map(int,input().split()) N=3*n d=[[0]*(N+1) for i in range(N+4)] d[0][0]=1 for i in range(N): a,b,c,e=d[i:i+4] for j in range(N): J=j+1 b[j]=(b[j]+a[j])%m c[J]=(c[J]+a[j]*(i+1))%m e[J]=(e[J]+a[j]*(i+1)*(i+2))%m print(sum(d[N][:n+1])%m) ```
output
1
66,226
12
132,453
Provide a correct Python 3 solution for this coding contest problem. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545
instruction
0
66,227
12
132,454
"Correct Solution: ``` n,m=map(int,input().split()) n3=3*n x=lambda a,b:(a+b)%mod d=[[0]*(n+2) for i in range(n3+4)] d[0][0]=1 for i in range(n3): d0,d1,d2,d3=d[i:i+4] for j in range(n+1): d1[j]=(d1[j]+d0[j])%m d2[j+1]=(d2[j+1]+d0[j]*(i+1))%m d3[j+1]=(d3[j+1]+d0[j]*(i+1)*(i+2))%m print(sum(d[n3][:n+1])%m) ```
output
1
66,227
12
132,455
Provide a correct Python 3 solution for this coding contest problem. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545
instruction
0
66,228
12
132,456
"Correct Solution: ``` """ Writer: SPD_9X2 https://atcoder.jp/contests/agc043/tasks/agc043_d 結局は、N個をX個の集合に、3個を最大として分ける通り数? それを集合内降順ソートして、全体で昇順ソートしたものは構成可能 3こ取る集合の数、2個とる集合の数を決めれば、1この集合の数もわかる →2重ループで解けそう 1ことるやつは、 全体C取る数 2ことるやつは、 残りC取るやつ * (Π(i=1~N) 2i C 2) / (N!) 3ことるやつは  ( Π(i=1~N) 3i C 3 ) / (N!) Π(i=1~N) 2i C 2 = TX[i] Π(i=1~N) 3i C 3 = TT[i]を各Nについて前計算しておく必要性あり グループ内の最大が先頭である必要があるけど、残りは自由 →なんか違くね… →2の個数が1の個数を超えたらダメなのか!! もうちょっとちゃんと考察しよう ある順列があっったとする。これが構築可能かの判定 →大小の塊をセットにする。 塊の大きさは3以下、かつ2の塊の数が1より多かったらダメ このような分け方のとき、かならず構築できる? 1,2の大きさを組み合わせ、先頭が昇順になるように合わせればおk """ def inverse(a,mod): #aのmodを法にした逆元を返す return pow(a,mod-2,mod) #modのn!と、n!の逆元を格納したリストを返す(拾いもの) #factorialsには[1, 1!%mod , 2!%mod , 6!%mod… , n!%mod] が入っている #invsには↑の逆元が入っている def modfac(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return factorials, invs def modnCr(n,r,mod,fac,inv): #上で求めたfacとinvsを引数に入れるべし(上の関数で与えたnが計算できる最大のnになる) return fac[n] * inv[n-r] * inv[r] % mod N,mod = map(int,input().split()) fac,inv = modfac(10*N+30,mod) TX = [1] for i in range(1,2 * N+10): TX.append(TX[-1] * modnCr(2*i,2,mod,fac,inv) % mod) TT = [1] for i in range(1,2 * N+10): TT.append(TT[-1] * modnCr(3*i,3,mod,fac,inv) % mod) ans = 0 for tri in range(N+1): for sec in range(2*N): one = 3*N - 3*tri - 2*sec if one < 0 or one < sec: continue now = modnCr(3*N,one,mod,fac,inv) now *= modnCr(3*N-one,2*sec,mod,fac,inv) * TX[sec] * inv[sec] now *= TT[tri] * inv[tri] * pow(2,tri,mod) now %= mod ans += now #print (tri,sec,one,now) ans %= mod print (ans) ```
output
1
66,228
12
132,457
Provide a correct Python 3 solution for this coding contest problem. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545
instruction
0
66,229
12
132,458
"Correct Solution: ``` N,mod=map(int,input().split()) DP=[[0]*(N+5) for i in range(3*N+5)] DP[0][0]=1 for i in range(3*N): for j in range(N+1): DP[i][j]%=mod DP[i+1][j]+=DP[i][j] DP[i+2][j+1]+=((3*N-i)-1)*DP[i][j] DP[i+3][j+1]+=((3*N-i)-1)*((3*N-i)-2)*DP[i][j] ANS=0 for i in range(N+1): ANS+=DP[3*N][i] print(ANS%mod) ```
output
1
66,229
12
132,459
Provide a correct Python 3 solution for this coding contest problem. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545
instruction
0
66,230
12
132,460
"Correct Solution: ``` import sys readline = sys.stdin.readline N, MOD = map(int, readline().split()) dp = [[0]*(N+5) for _ in range(3*N+4)] dp[0][0] = 1 for i in range(3*N): for j in range(N+1): d = dp[i][j] dp[i+1][j] = (dp[i+1][j]+d)%MOD dp[i+2][j+1] = (dp[i+2][j+1]+(i+1)*d)%MOD dp[i+3][j+1] = (dp[i+3][j+1]+(i+2)*(i+1)*d)%MOD res = 0 for j in range(N+1): res = (res+dp[3*N][j])%MOD print(res) ```
output
1
66,230
12
132,461
Provide a correct Python 3 solution for this coding contest problem. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545
instruction
0
66,231
12
132,462
"Correct Solution: ``` n,m=map(int,input().split()) N=3*n d=[[0]*(N+1) for i in range(N+4)] d[0][0]=1 for i in range(N): a,b,c,e=d[i:i+4] for j in range(N):J=j+1;b[j]=(b[j]+a[j])%m;c[J]=(c[J]+a[j]*(i+1))%m;e[J]=(e[J]+a[j]*(i+1)*(i+2))%m print(sum(d[N][:n+1])%m) ```
output
1
66,231
12
132,463
Provide a correct Python 3 solution for this coding contest problem. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545
instruction
0
66,232
12
132,464
"Correct Solution: ``` n, MOD = map(int, input().split()) n3 = n * 3 dp = [[0] * int(i * 1.5 + 5) for i in range(n3 + 1)] dp[0][0] = dp[1][1] = dp[2][2] = dp[2][-1] = 1 for i in range(3, n3 + 1): i12 = (i - 1) * (i - 2) dpi3, dpi2, dpi1, dpi = dp[i - 3:i + 1] for j in range(-(i // 2), i + 1): tmp = dpi3[j] * i12 tmp += dpi2[j + 1] * (i - 1) tmp += dpi1[j - 1] dpi[j] = tmp % MOD print(sum(dp[-1][:n3 + 1]) % MOD) ```
output
1
66,232
12
132,465
Provide a correct Python 3 solution for this coding contest problem. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545
instruction
0
66,233
12
132,466
"Correct Solution: ``` N,M=map(int,input().split()) dp=[[0 for i in range(2*N+1)] for j in range(3*N+1)] for j in range(N+1): dp[0][j]=1 for i in range(1,3*N+1): for j in range(2*N+1): if 2*N-1>=j>=1: dp[i][j]=(dp[i-1][j-1]+dp[i-2][j+1]*(i-1)+dp[i-3][j]*(i-1)*(i-2))%M elif j==0: dp[i][j]=(dp[i-1][j]+dp[i-2][j+1]*(i-1)+dp[i-3][j]*(i-1)*(i-2))%M else: dp[i][j]=(dp[i-1][j-1]+dp[i-3][j]*(i-1)*(i-2))%M print(dp[3*N][N]%M) ```
output
1
66,233
12
132,467
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545 Submitted Solution: ``` from functools import lru_cache N, M = map(int, input().split()) @lru_cache(maxsize=None) def mod_inv(x): x1, y1, z1 = 1, 0, x x2, y2, z2 = 0, 1, M while z1 != 1: d, m = divmod(z2, z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1%M def gen_Y(i): # sC2/1, (s-2)C2/2, (s-4)C2/3 ... s = 3*(N-i) r = s*(s-1)>>1 d_r = (s<<1)-3 for j in range(1, N-i+1): yield r * mod_inv(j) r -= d_r d_r -= 4 def gen_X(): # sC3*2/1, (s-3)C3*2/2, (s-6)C3*2/3 ... a = N b = 3*N - 1 for i in range(1, N+1): yield a * b * (b-1) * mod_inv(i) a -= 1 b -= 3 def acc_mulmod(it, init=1): x = init yield x for y in it: x = x*y % M yield x ans = sum(sum(acc_mulmod(gen_Y(i), A)) for i, A in enumerate(acc_mulmod(gen_X())))%M print(ans) ```
instruction
0
66,234
12
132,468
Yes
output
1
66,234
12
132,469
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545 Submitted Solution: ``` N, P = map(int, input().split()) K = 96 m = int(("1" * 32 + "0" * 64) * 8192, 2) pa = (1 << 64) - ((1 << 64) % P) modP = lambda x: x - ((x & m) >> 64) * pa def getsum(x): for i in range(13): x += x >> (K << i) return x & ((1 << K) - 1) a, b, c = 1 << K * N, 0, 0 fa = 1 for i in range(1, 3 * N + 1): a, b, c = modP(((a << K) + (b >> K) + c) * pow(i, P-2, P)), a, b fa = fa * i % P print(getsum(a >> K * N) * fa % P) ```
instruction
0
66,235
12
132,470
Yes
output
1
66,235
12
132,471
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545 Submitted Solution: ``` from functools import lru_cache, reduce from itertools import accumulate N, M = map(int, input().split()) @lru_cache(maxsize=None) def mod_inv(x): x1, y1, z1 = 1, 0, x x2, y2, z2 = 0, 1, M while z1 != 1: d, m = divmod(z2, z1) x1, x2 = x2-d*x1, x1 y1, y2 = y2-d*y1, y1 z1, z2 = m, z1 return x1%M def gen_Y(i, A): yield A # sC2/1, (s-2)C2/2, (s-4)C2/3 ... s = 3*(N-i) r = s*(s-1)>>1 d_r = (s<<1)-3 for j in range(1, N-i+1): yield r * mod_inv(j) r -= d_r d_r -= 4 def gen_X(): # sC3*2/1, (s-3)C3*2/2, (s-6)C3*2/3 ... yield 1 a = N b = 3*N - 1 for i in range(1, N+1): yield a * b * (b-1) * mod_inv(i) a -= 1 b -= 3 def mul_mod(x, y): return x * y % M def acc_mod(it): return accumulate(it, mul_mod) ans = sum(sum(acc_mod(gen_Y(i, A))) for i, A in enumerate(acc_mod(gen_X())))%M print(ans) ```
instruction
0
66,236
12
132,472
Yes
output
1
66,236
12
132,473
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545 Submitted Solution: ``` n,m=map(int,input().split()) N=3*n d=[[0]*(N+1) for i in range(N+5)] d[0][0]=1 for i in range(N): a,b,c,e,*_=d[i:] for j in range(N): J=j+1 b[j]=(b[j]+a[j])%m c[J]=(c[J]+a[j]*(i+1))%m e[J]=(e[J]+a[j]*(i+1)*(i+2))%m print(sum(d[N][:n+1])%m) ```
instruction
0
66,237
12
132,474
Yes
output
1
66,237
12
132,475
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545 Submitted Solution: ``` n, m = map(int, input()) score = 1 for i in range(n): score *= 3+2i for i in range(1, 2n+1): score *= i print(score%m) ```
instruction
0
66,238
12
132,476
No
output
1
66,238
12
132,477
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545 Submitted Solution: ``` import numpy as p n,M=map(int,input().split()) l=n*3+1 r=p.roll d=p.zeros((l,n*5),p.int64) d[0][0]=1 for i in range(l):j,k=i-1,i-2;d[i]=(d[i-3]*k*j+r(d[k],-1)*j+r(d[j],1))%M print(d[-1][:l].sum()%M) ```
instruction
0
66,239
12
132,478
No
output
1
66,239
12
132,479
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545 Submitted Solution: ``` N,mod=map(int,input().split()) DP=[[0]*(N+5) for i in range(3*N+5)] DP[0][0]=1 for i in range(3*N): for j in range(N+1): DP[i+1][j]+=DP[i][j] DP[i+2][j+1]+=((3*N-i)-1)*DP[i][j] DP[i+3][j+1]+=((3*N-i)-1)*((3*N-i)-2)*DP[i][j] ANS=0 for i in range(N+1): ANS+=DP[3*N][i] print(ANS%mod) ```
instruction
0
66,240
12
132,480
No
output
1
66,240
12
132,481
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive integer N. Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M. * Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. * Let P be an empty sequence, and do the following operation 3N times. * Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x. * Remove x from the sequence, and add x at the end of P. Constraints * 1 \leq N \leq 2000 * 10^8 \leq M \leq 10^9+7 * M is a prime number. * All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M. Examples Input 1 998244353 Output 6 Input 2 998244353 Output 261 Input 314 1000000007 Output 182908545 Submitted Solution: ``` n,m=map(int,input().split()) N=3*n d=[[0]*(N+1) for i in range(N+4)] d[0][0]=1 for i in range(N): a,b,c,d=d[i:i+4] for j in range(N): b[j]=(b[j]+a[j])%m c[j+1]=(c[j+1]+a[j]*(i+1))%m d[j+1]=(d[j+1]+a[j]*(i+1)*(i+2))%m print(sum(d[n3][:n+1])%m) ```
instruction
0
66,241
12
132,482
No
output
1
66,241
12
132,483
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist.
instruction
0
66,532
12
133,064
Tags: greedy Correct Solution: ``` def is_valid(mtx): for i in range(len(mtx)): for j in range(len(mtx[i])): if mtx[i][j] != 0: if i - 1 >= 0 and (mtx[i][j] <= mtx[i - 1][j] != 0): return False if j - 1 >= 0 and (mtx[i][j] <= mtx[i][j - 1] != 0): return False return True def get_sum(mtx): res = 0 for i in range(len(mtx)): for j in range(len(mtx[i])): res += mtx[i][j] return res def solve(mtx): if not is_valid(mtx): return -1 update = 1 for i in range(len(mtx) - 2, 0, -1): for j in range(len(mtx[i]) - 2, 0, -1): if mtx[i][j] == 0: updated = min(mtx[i][j + 1], mtx[i + 1][j]) - update if updated <= mtx[i][j - 1] or updated <= mtx[i - 1][j]: return -1 mtx[i][j] = updated # Check that values are valid elif mtx[i][j] <= mtx[i][j - 1] or mtx[i][j] <= mtx[i - 1][j]: return -1 return get_sum(mtx) n, m = [int(x) for x in input().split()] mat = [] for i in range(n): mat.append([int(x) for x in input().split()]) print(solve(mat)) ```
output
1
66,532
12
133,065
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist.
instruction
0
66,533
12
133,066
Tags: greedy Correct Solution: ``` n,m=map(int,input().split()) mat=[] for i in range(n): mat.append(list(map(int,input().split()))) for i in range(n-1,-1,-1): for j in range(m-1,-1,-1): if(mat[i][j]==0): mat[i][j]=min(mat[i+1][j],mat[i][j+1])-1 ans=0 for i in range(n): for j in range(m): if(i!=0 and j!=0 and (mat[i][j]<=mat[i-1][j] or mat[i][j]<=mat[i][j-1])): print(-1) exit() ans+=mat[i][j] print(ans) ```
output
1
66,533
12
133,067
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist.
instruction
0
66,534
12
133,068
Tags: greedy Correct Solution: ``` n , m =[int(i) for i in input().split()] a = [[int(j) for j in input().split()] for i in range(n)] wa = 0 for y in range(n): if y != 0 and (a[y][0] <= a[y - 1][0] or a[y][m - 1] <= a[y - 1][m - 1]): wa = 1 for y in range(m): if y != 0 and (a[0][y] <= a[0][y - 1] or a[n - 1][y] <= a[n - 1][y - 1]): wa = 1 for x in range(n - 2): for y in range(m - 2): if a[n - x - 2][m - y - 2] == 0: a[n - x - 2][m - y - 2] = min(a[n - x - 2][m - y - 1],a[n - x - 1][m - y - 2]) - 1 if a[n - x - 2][m - y - 2] <= max(a[n - x - 2][m - y - 3],a[n - x - 3][m - y - 2]): wa = 1 break if wa == 1: break if wa == 1: print( -1 ) else: ans = 0 for x in a: for y in x: ans += y print(ans) ```
output
1
66,534
12
133,069
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist.
instruction
0
66,535
12
133,070
Tags: greedy Correct Solution: ``` n, m = list(map(int, input().split())) matrix = [] for _ in range(n): matrix.append(list(map(int, input().split()))) ans = 'yes' total = 0 a = matrix.copy() for i in range(n - 1, -1, -1): for j in range(m - 1, -1, -1): if i + 1 < n and j + 1 < m: possible_max = min(a[i+1][j], a[i][j+1]) elif i + 1 < n: possible_max = a[i+1][j] elif j + 1 < m: possible_max = a[i][j+1] else: # n-1, m - 1 total += a[i][j] continue if a[i][j] == 0: a[i][j] = possible_max - 1 if a[i][j] <= 0: ans = 'no' break else: if a[i][j] >= possible_max: ans = 'no' break total += a[i][j] #print(i, j, a[i][j], possible_max) if ans == 'no': break if ans == 'yes': print(total) else: print(-1) ```
output
1
66,535
12
133,071
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist.
instruction
0
66,536
12
133,072
Tags: greedy Correct Solution: ``` import sys import math #to read string get_string = lambda: sys.stdin.readline().strip() #to read list of integers get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) ) #to read integers get_int = lambda: int(sys.stdin.readline()) #to print fast pt = lambda x: sys.stdout.write(str(x)+'\n') #--------------------------------WhiteHat010--------------------------------------# n,m = get_int_list() matrix = [] for i in range(n): matrix.append(get_int_list()) for i in range(n-1,-1,-1): for j in range(m-1,-1,-1): if matrix[i][j] == 0: matrix[i][j] = min( matrix[i+1][j]-1, matrix[i][j+1]-1 ) flag = True for i in range(1,n): for j in range(1,m): if matrix[i][j] <= matrix[i-1][j] or matrix[i][j] <= matrix[i][j-1]: flag = False break if not flag: break if flag: s = 0 for i in range(n): s += sum(matrix[i]) print(s) else: print(-1) ```
output
1
66,536
12
133,073
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist.
instruction
0
66,537
12
133,074
Tags: greedy Correct Solution: ``` if __name__ == '__main__': n, m = list(map(int, input().split())) matrix = [] for i in range(n): matrix.append(list(map(int, input().split()))) res = 0 for i in range(n - 2, 0, -1): for j in range(m - 2, 0, -1): if matrix[i][j] == 0: new = min(matrix[i + 1][j], matrix[i][j + 1]) - 1 if (matrix[i - 1][j] == 0 or new > matrix[i - 1][j]) and (matrix[i][j - 1] == 0 or new > matrix[i][j - 1]): matrix[i][j] = new else: res = -1 break if res == -1: break if res != -1: for i in range(n): for j in range(1, m): if matrix[i][j] <= matrix[i][j - 1]: res = -1 if res == -1: print(-1) else: for i in range(m): for j in range(1, n): if matrix[j][i] <= matrix[j-1][i]: res = -1 if res == -1: print(-1) else: for i in range(n): res += sum(matrix[i]) print(res) else: print(-1) ```
output
1
66,537
12
133,075
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist.
instruction
0
66,538
12
133,076
Tags: greedy Correct Solution: ``` I = lambda: map(int, input().split()) n, m = I() A = [list(I()) for _ in range(n)] for i in range(n-2, 0, -1): for j in range(m-2, 0, -1): if not A[i][j]: A[i][j] = min(A[i+1][j],A[i][j+1]) - 1 if ( all(A[i][j]<A[i][j+1] for i in range(n) for j in range(m-1)) and all(A[i][j]<A[i+1][j] for i in range(n-1) for j in range(m)) ): print(sum(A[i][j] for i in range(n) for j in range(m))) else: print(-1) ```
output
1
66,538
12
133,077
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist.
instruction
0
66,539
12
133,078
Tags: greedy Correct Solution: ``` n,m=map(int,input().split()) l=[list(map(int,input().split())) for _ in range(n)] ans=0 for i in range(n-1,-1,-1): for j in range(m-1, -1, -1): if l[i][j]==0: mr=l[i][j+1]-1 mc=l[i+1][j]-1 l[i][j]=min(mr,mc) ans+=l[i][j] else: ans+=l[i][j] ch=0 for i in range(n): dic={} for j in range(m): if l[i][j] not in dic: dic[l[i][j]]=1 if j>0: if l[i][j]<=l[i][j-1]: ch=1 break else: ch=1 break if ch: break if not ch: for j in range(m): dic={} for i in range(n): if l[i][j] not in dic: dic[l[i][j]]=1 if i >0: if l[i][j]<=l[i-1][j]: ch=1 break else: ch=1 break if ch: break if ch: print(-1) else: print(ans) ```
output
1
66,539
12
133,079
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this problem, a n × m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}). In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible. It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column). Input The first line contains integers n and m (3 ≤ n, m ≤ 500) — the number of rows and columns in the given matrix a. The following lines contain m each of non-negative integers — the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 ≤ a_{i,j} ≤ 8000). It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true. Output If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1. Examples Input 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 Output 144 Input 3 3 1 2 3 2 0 4 4 5 6 Output 30 Input 3 3 1 2 3 3 0 4 4 5 6 Output -1 Input 3 3 1 2 3 2 3 4 3 4 2 Output -1 Note In the first example, the resulting matrix is as follows: 1 3 5 6 7 3 6 7 8 9 5 7 8 9 10 8 9 10 11 12 In the second example, the value 3 must be put in the middle cell. In the third example, the desired resultant matrix does not exist. Submitted Solution: ``` def comp(A): for i in range(n-2, -1, -1): for j in range(m-2, -1, -1): if (A[i+1][j+1] <= A[i][j+1] or A[i+1][j+1] <= A[i+1][j]): return -1 if (A[i][j] == 0): A[i][j] = min (A[i+1][j]-1, A[i][j+1]-1) if (A[i][j] <= A[i-1][j] or A[i][j] <= A[i][j-1] or A[i][j] == 0): return -1 return sum([sum(A[i]) for i in range(n)]) n, m = map(int, input().rstrip().split()) A = [] for i in range(n): A.append(list(map(int, input().rstrip().split()))) res = comp (A) print(res) ```
instruction
0
66,540
12
133,080
Yes
output
1
66,540
12
133,081