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
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two sequences a_1, a_2, ..., a_n and b_1, b_2, ..., b_n. Each element of both sequences is either 0, 1 or 2. The number of elements 0, 1, 2 in the sequence a is x_1, y_1, z_1 respectively, and the number of elements 0, 1, 2 in the sequence b is x_2, y_2, z_2 respectively. You can rearrange the elements in both sequences a and b however you like. After that, let's define a sequence c as follows: c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\\ 0 & \mbox{if }a_i = b_i \\\ -a_i b_i & \mbox{if }a_i < b_i \end{cases} You'd like to make βˆ‘_{i=1}^n c_i (the sum of all elements of the sequence c) as large as possible. What is the maximum possible sum? Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers x_1, y_1, z_1 (0 ≀ x_1, y_1, z_1 ≀ 10^8) β€” the number of 0-s, 1-s and 2-s in the sequence a. The second line of each test case also contains three integers x_2, y_2, z_2 (0 ≀ x_2, y_2, z_2 ≀ 10^8; x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0) β€” the number of 0-s, 1-s and 2-s in the sequence b. Output For each test case, print the maximum possible sum of the sequence c. Example Input 3 2 3 2 3 3 1 4 0 1 2 3 0 0 0 1 0 0 1 Output 4 2 0 Note In the first sample, one of the optimal solutions is: a = \{2, 0, 1, 1, 0, 2, 1\} b = \{1, 0, 1, 0, 2, 1, 0\} c = \{2, 0, 0, 0, 0, 2, 0\} In the second sample, one of the optimal solutions is: a = \{0, 2, 0, 0, 0\} b = \{1, 1, 0, 1, 0\} c = \{0, 2, 0, 0, 0\} In the third sample, the only possible solution is: a = \{2\} b = \{2\} c = \{0\} Submitted Solution: ``` #code if __name__ == "__main__": t = int(input()) for _ in range(t): ans = 0 x1, y1, z1 = map(int,input().split()) x2, y2, z2 = map(int,input().split()) z2 = z2 - min(z2,x1) x1 = x1 - min(z2,x1) z2 = z2 - min(z2,z1) z1 = z1 - min(z2,z1) ans = -(z2*2) z2 = 0 y1 = y1 - z2 ans = ans + (min(y2,z1)*2) print(ans) ```
instruction
0
105,465
12
210,930
No
output
1
105,465
12
210,931
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two sequences a_1, a_2, ..., a_n and b_1, b_2, ..., b_n. Each element of both sequences is either 0, 1 or 2. The number of elements 0, 1, 2 in the sequence a is x_1, y_1, z_1 respectively, and the number of elements 0, 1, 2 in the sequence b is x_2, y_2, z_2 respectively. You can rearrange the elements in both sequences a and b however you like. After that, let's define a sequence c as follows: c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\\ 0 & \mbox{if }a_i = b_i \\\ -a_i b_i & \mbox{if }a_i < b_i \end{cases} You'd like to make βˆ‘_{i=1}^n c_i (the sum of all elements of the sequence c) as large as possible. What is the maximum possible sum? Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers x_1, y_1, z_1 (0 ≀ x_1, y_1, z_1 ≀ 10^8) β€” the number of 0-s, 1-s and 2-s in the sequence a. The second line of each test case also contains three integers x_2, y_2, z_2 (0 ≀ x_2, y_2, z_2 ≀ 10^8; x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0) β€” the number of 0-s, 1-s and 2-s in the sequence b. Output For each test case, print the maximum possible sum of the sequence c. Example Input 3 2 3 2 3 3 1 4 0 1 2 3 0 0 0 1 0 0 1 Output 4 2 0 Note In the first sample, one of the optimal solutions is: a = \{2, 0, 1, 1, 0, 2, 1\} b = \{1, 0, 1, 0, 2, 1, 0\} c = \{2, 0, 0, 0, 0, 2, 0\} In the second sample, one of the optimal solutions is: a = \{0, 2, 0, 0, 0\} b = \{1, 1, 0, 1, 0\} c = \{0, 2, 0, 0, 0\} In the third sample, the only possible solution is: a = \{2\} b = \{2\} c = \{0\} Submitted Solution: ``` import math from collections import deque from sys import stdin, stdout from string import ascii_letters input = stdin.readline #print = stdout.write def calc(a, b): first = a[:] second = b[:] res = 0 for i in range(2, -1, -1): for g in range(i - 1, 0, -1): bf = min(first[i], second[g]) res += bf * (i) * (g) first[i] -= bf second[g] -= bf for i in range(2, -1, -1): bf = min(first[i], second[i]) first[i] -= bf second[i] -= bf for i in range(0, 3): for g in range(i + 1, 3): bf = min(first[i], second[g]) res -= bf * (i) * (g) first[i] -= bf second[g] -= bf return res for _ in range(int(input())): first = list(map(int, input().split())) second = list(map(int, input().split())) print(calc(first, second)) ```
instruction
0
105,466
12
210,932
No
output
1
105,466
12
210,933
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two sequences a_1, a_2, ..., a_n and b_1, b_2, ..., b_n. Each element of both sequences is either 0, 1 or 2. The number of elements 0, 1, 2 in the sequence a is x_1, y_1, z_1 respectively, and the number of elements 0, 1, 2 in the sequence b is x_2, y_2, z_2 respectively. You can rearrange the elements in both sequences a and b however you like. After that, let's define a sequence c as follows: c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\\ 0 & \mbox{if }a_i = b_i \\\ -a_i b_i & \mbox{if }a_i < b_i \end{cases} You'd like to make βˆ‘_{i=1}^n c_i (the sum of all elements of the sequence c) as large as possible. What is the maximum possible sum? Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers x_1, y_1, z_1 (0 ≀ x_1, y_1, z_1 ≀ 10^8) β€” the number of 0-s, 1-s and 2-s in the sequence a. The second line of each test case also contains three integers x_2, y_2, z_2 (0 ≀ x_2, y_2, z_2 ≀ 10^8; x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0) β€” the number of 0-s, 1-s and 2-s in the sequence b. Output For each test case, print the maximum possible sum of the sequence c. Example Input 3 2 3 2 3 3 1 4 0 1 2 3 0 0 0 1 0 0 1 Output 4 2 0 Note In the first sample, one of the optimal solutions is: a = \{2, 0, 1, 1, 0, 2, 1\} b = \{1, 0, 1, 0, 2, 1, 0\} c = \{2, 0, 0, 0, 0, 2, 0\} In the second sample, one of the optimal solutions is: a = \{0, 2, 0, 0, 0\} b = \{1, 1, 0, 1, 0\} c = \{0, 2, 0, 0, 0\} In the third sample, the only possible solution is: a = \{2\} b = \{2\} c = \{0\} Submitted Solution: ``` n = int(input()) a = [] b = [] c = [] for i in range(n): a.append([int(j) for j in input().split()]) b.append([int(j) for j in input().split()]) for i in range(n): if (a[i][2] == 0 or b[i][1] == 0) and b[i][2] > a[i][0] + a[i][2]: print(-(b[i][2] - a[i][0] - a[i][2]) * 2) elif b[i][1] > a[i][2]: print(2 * a[i][2]) else: print(b[i][1] * 2) ```
instruction
0
105,467
12
210,934
No
output
1
105,467
12
210,935
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two sequences a_1, a_2, ..., a_n and b_1, b_2, ..., b_n. Each element of both sequences is either 0, 1 or 2. The number of elements 0, 1, 2 in the sequence a is x_1, y_1, z_1 respectively, and the number of elements 0, 1, 2 in the sequence b is x_2, y_2, z_2 respectively. You can rearrange the elements in both sequences a and b however you like. After that, let's define a sequence c as follows: c_i = \begin{cases} a_i b_i & \mbox{if }a_i > b_i \\\ 0 & \mbox{if }a_i = b_i \\\ -a_i b_i & \mbox{if }a_i < b_i \end{cases} You'd like to make βˆ‘_{i=1}^n c_i (the sum of all elements of the sequence c) as large as possible. What is the maximum possible sum? Input The first line contains one integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers x_1, y_1, z_1 (0 ≀ x_1, y_1, z_1 ≀ 10^8) β€” the number of 0-s, 1-s and 2-s in the sequence a. The second line of each test case also contains three integers x_2, y_2, z_2 (0 ≀ x_2, y_2, z_2 ≀ 10^8; x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0) β€” the number of 0-s, 1-s and 2-s in the sequence b. Output For each test case, print the maximum possible sum of the sequence c. Example Input 3 2 3 2 3 3 1 4 0 1 2 3 0 0 0 1 0 0 1 Output 4 2 0 Note In the first sample, one of the optimal solutions is: a = \{2, 0, 1, 1, 0, 2, 1\} b = \{1, 0, 1, 0, 2, 1, 0\} c = \{2, 0, 0, 0, 0, 2, 0\} In the second sample, one of the optimal solutions is: a = \{0, 2, 0, 0, 0\} b = \{1, 1, 0, 1, 0\} c = \{0, 2, 0, 0, 0\} In the third sample, the only possible solution is: a = \{2\} b = \{2\} c = \{0\} Submitted Solution: ``` import math from decimal import * def pf(n): ans = [1] if(n%2==0): if(n!= 2): ans.append(2) while(n%2==0): n//=2 p = 3 while(n>1): if(n%p==0): ans.append(p) while(n%p==0): n//=p p+=1 return ans for _ in range(int(input())): a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 o= [1, 2, 0] pnt = 0 p1 = 2 p2 = 0 while(a[2]>0 and pnt<3): mn = min(a[2], b[o[pnt]]) if(o[pnt]==1): ans+=(2*mn) b[o[pnt]]-=mn a[2]-=mn pnt+=1 p1 = 2 while(p1>=0 and p2<3): mn = min(a[p2], b[p1]) if(p1>p2): ans-=(2*p2) b[p1]-=mn a[p2]-=mn if(a[p2]==0): p2+=1 if(b[p1]==0): p1-=1 print(ans) ```
instruction
0
105,468
12
210,936
No
output
1
105,468
12
210,937
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
instruction
0
105,494
12
210,988
Tags: brute force, dp, implementation, math, two pointers Correct Solution: ``` n=int(input()) array1=list(map(int,input().split())) array2=list(map(int,input().split())) totalSum=sum([array2[i]*array1[i] for i in range(n)]) maxSum=totalSum """ for i in range(n): for j in range(i,i+2): start=i end=j curSum=totalSum while start>=0 and end<n: #curSum=curSum-array1[start]*array2[start]-array1[end]*array2[end] #curSum=curSum+array1[start]*array2[end]+array1[end]*array2[start] curSum+=(array1[start]-array1[end])*(array2[end]-array2[start]) start-=1 end+=1 maxSum=max(maxSum,curSum) """ for i in range(n): #for j in range(i,i+2): start=i end=i curSum=totalSum while start>=0 and end<n: curSum+=(array1[start]-array1[end])*(array2[end]-array2[start]) start-=1 end+=1 maxSum=max(maxSum,curSum) for i in range(n-1): start=i end=i+1 curSum=totalSum while start>=0 and end<n: curSum+=(array1[start]-array1[end])*(array2[end]-array2[start]) start-=1 end+=1 maxSum=max(maxSum,curSum) print(maxSum) ```
output
1
105,494
12
210,989
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
instruction
0
105,495
12
210,990
Tags: brute force, dp, implementation, math, two pointers Correct Solution: ``` # https://codeforces.com/contest/1519/submission/114616034 # import io,os # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) s = sum(a[i]*b[i] for i in range(n)) ans = s for i in range(n): l = i - 1 r = i + 1 dx = 0 while l >= 0 and r < n: dx -= (a[r] - a[l]) * (b[r] - b[l]) ans = max(ans, s + dx) l -= 1 r += 1 l = i r = i + 1 dx = 0 while l >= 0 and r < n: dx -= (a[r] - a[l]) * (b[r] - b[l]) ans = max(ans, s + dx) l -= 1 r += 1 print(ans) ```
output
1
105,495
12
210,991
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
instruction
0
105,496
12
210,992
Tags: brute force, dp, implementation, math, two pointers Correct Solution: ``` from collections import defaultdict from itertools import accumulate import sys input = sys.stdin.readline ''' for CASES in range(int(input())): n, m = map(int, input().split()) n = int(input()) A = list(map(int, input().split())) S = input().strip() sys.stdout.write(" ".join(map(str,ANS))+"\n") ''' inf = 100000000000000000 # 1e17 mod = 998244353 n=int(input()) A=list(map(int,input().split())) B=list(map(int,input().split())) ans=sum(A[i]*B[i] for i in range(n)) origin=ans for i in range(n): def extend(l,r=i+1,cur=origin): global ans while l>=0 and r<n: cur-=(A[l]-A[r])*(B[l]-B[r]) ans=max(ans,cur) l-=1 r+=1 extend(i) extend(i+1) print(ans) ```
output
1
105,496
12
210,993
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
instruction
0
105,497
12
210,994
Tags: brute force, dp, implementation, math, two pointers Correct Solution: ``` n = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) ans = 0 for i in range(n): ans+= (b[i]*a[i]) maxi = ans for i in range(n): l = i - 1 r = i + 1 temp_ans = ans while l >= 0 and r < n: temp_ans = temp_ans + (a[r]-a[l])*(b[l]-b[r]) maxi = max(maxi, temp_ans) l -= 1 r += 1 l = i r = i+1 temp_ans = ans while l>=0 and r<n: temp_ans = temp_ans +(a[r]-a[l])*(b[l]-b[r]) # print("temp_ans", temp_ans) maxi = max(maxi,temp_ans) l-=1 r+=1 print(maxi) ```
output
1
105,497
12
210,995
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
instruction
0
105,498
12
210,996
Tags: brute force, dp, implementation, math, two pointers Correct Solution: ``` N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) Base = 0 for i in range(N): Base += A[i] * B[i] Ans = Base for i in range(1, N - 1): Value = Base L = i - 1 R = i + 1 while L >= 0 and R < N: Value += (A[L] - A[R]) * (B[R] - B[L]) L -= 1 R += 1 if Value > Ans: Ans = Value for i in range(N - 1): Value = Base L = i R = i + 1 while L >= 0 and R < N: Value += (A[L] - A[R]) * (B[R] - B[L]) if Value > Ans: Ans = Value L -= 1 R += 1 print(Ans) ```
output
1
105,498
12
210,997
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
instruction
0
105,499
12
210,998
Tags: brute force, dp, implementation, math, two pointers Correct Solution: ``` def readline(): return map(int, input().split()) def main(): n = int(input()) a = list(readline()) b = list(readline()) max_diff = 0 for begin in range(n): for end in (begin, begin + 1): index_sum = begin + end diff = 0 for i in range(end, min(n, index_sum + 1)): diff -= (a[i] - a[index_sum-i]) * (b[i] - b[index_sum-i]) max_diff = max(diff, max_diff) print(sum(ai * bi for (ai, bi) in zip(a, b)) + max_diff) if __name__ == '__main__': main() ```
output
1
105,499
12
210,999
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
instruction
0
105,500
12
211,000
Tags: brute force, dp, implementation, math, two pointers Correct Solution: ``` ###### ### ####### ####### ## # ##### ### ##### # # # # # # # # # # # # # ### # # # # # # # # # # # # # ### ###### ######### # # # # # # ######### # ###### ######### # # # # # # ######### # # # # # # # # # # # #### # # # # # # # # # # ## # # # # # ###### # # ####### ####### # # ##### # # # # from __future__ import print_function # for PyPy2 # from itertools import permutations # from functools import cmp_to_key # for adding custom comparator # from fractions import Fraction # from collections import * from sys import stdin # from bisect import * # from heapq import * # from math import * g = lambda : stdin.readline().strip() gl = lambda : g().split() gil = lambda : [int(var) for var in gl()] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] mod = int(1e9)+7 inf = float("inf") n, = gil() a, b = gil(), gil() ans = 0 base = 0 # cal ans for odd len for ci in range(n): lo, ro, le, re, anso, anse = ci-1, ci+1, ci, ci+1, 0, 0 base += a[ci]*b[ci] while (le >= 0 and re < n) or (lo >= 0 and ro < n): if (le >= 0 and re < n):anse += (a[le]-a[re])*b[re] + (a[re]- a[le])*b[le] ;ans = max(anse, ans) if (lo >= 0 and ro < n):anso += (a[lo]- a[ro])*b[ro] + (a[ro]- a[lo])*b[lo] ;ans = max(anso, ans) le -= 1;re += 1;lo -= 1;ro += 1 print(ans+base) ```
output
1
105,500
12
211,001
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235.
instruction
0
105,501
12
211,002
Tags: brute force, dp, implementation, math, two pointers Correct Solution: ``` n=int(input()) array1=list(map(int,input().split())) array2=list(map(int,input().split())) totalSum=sum([array2[i]*array1[i] for i in range(n)]) maxSum=totalSum """ for i in range(n): for j in range(i,i+2): start=i end=j curSum=totalSum while start>=0 and end<n: #curSum=curSum-array1[start]*array2[start]-array1[end]*array2[end] #curSum=curSum+array1[start]*array2[end]+array1[end]*array2[start] curSum+=(array1[start]-array1[end])*(array2[end]-array2[start]) start-=1 end+=1 maxSum=max(maxSum,curSum) """ for i in range(n): #for j in range(i,i+2): start=i end=i curSum=totalSum while start>=0 and end<n: curSum+=(array1[start]-array1[end])*(array2[end]-array2[start]) start-=1 end+=1 maxSum=max(maxSum,curSum) for i in range(n): start=i end=i+1 curSum=totalSum while start>=0 and end<n: curSum+=(array1[start]-array1[end])*(array2[end]-array2[start]) start-=1 end+=1 maxSum=max(maxSum,curSum) print(maxSum) ```
output
1
105,501
12
211,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235. Submitted Solution: ``` n = int(input()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] base_sum = sum([a * b for a, b in zip(a, b)]) max_possible_sum = base_sum for num in range(1, 2 * n - 2): if num % 2 == 0: left = num // 2 - 1 right = num // 2 + 1 else: left = (num - 1) // 2 right = (num + 1) // 2 local_max_possible_sum = base_sum while left >= 0 and right < n: local_max_possible_sum -= (a[left] - a[right]) * (b[left] - b[right]) max_possible_sum = max(max_possible_sum, local_max_possible_sum) left -= 1 right += 1 print(max_possible_sum) ```
instruction
0
105,502
12
211,004
Yes
output
1
105,502
12
211,005
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) s = 0 for i in range(n): s+=(a[i]*b[i]) ans = s k = s for c in range(1, n-1): l = c-1 r = c+1 res = k while l>=0 and r<n: res+=((a[l]-a[r])*(b[r]-b[l])) ans = max(ans, res) l-=1 r+=1 for c in range(n-1): res = k l = c r = c+1 while l>=0 and r<n: res+=((a[l]-a[r])*(b[r]-b[l])) ans = max(ans, res) l-=1 r+=1 print(ans) ```
instruction
0
105,503
12
211,006
Yes
output
1
105,503
12
211,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235. Submitted Solution: ``` def func(l, r, d): global ans while l >= 0 and r < n: d -= (a[l] - a[r]) * (b[l] - b[r]) ans = max(ans, d) l -= 1 r += 1 n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 p = 0 for i in range(n): ans += a[i] * b[i] p += a[i] * b[i] for i in range(n): func(i - 1, i + 1, p) func(i, i + 1, p) print(ans) ```
instruction
0
105,504
12
211,008
Yes
output
1
105,504
12
211,009
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235. Submitted Solution: ``` n=int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) resi = 0 for j in range(n): resi+=a[j]*b[j] ans=resi+0 for j in range(n): tmp=resi+0 l=j r=j+1 while(l>=0 and r<n): tmp+=(a[l]-a[r])*(b[r]-b[l]) ans=max(tmp,ans) l-=1 r+=1 for j in range(n): tmp=resi+0 l=j-1 r=j+1 while(l>=0 and r<n): tmp+=(a[l]-a[r])*(b[r]-b[l]) ans=max(tmp,ans) r+=1 l-=1 print(ans) ```
instruction
0
105,505
12
211,010
Yes
output
1
105,505
12
211,011
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) init = 0 for i in range(n): init += a[i] * b[i] mx = init for i in range(2*n-1): now = init if i % 2 == 0: for j in range(min(i//2, (2*n-i)//2-1)): now += a[i//2-j] * b[i//2+j] + a[i//2+j] * b[i//2-j] now -= a[i//2-j] * b[i//2-j] + a[i//2+j] * b[i//2+j] mx = max(mx, now) else: for j in range(min(i//2+1, (2*n-i)//2)): now += a[i//2-j] * b[i//2+j+1] + a[i//2+j+1] * b[i//2-j] now -= a[i//2-j] * b[i//2-j] + a[i//2+j+1] * b[i//2+j+1] mx = max(mx, now) print(mx) ```
instruction
0
105,506
12
211,012
No
output
1
105,506
12
211,013
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235. Submitted Solution: ``` import sys,os from io import BytesIO,IOBase mod = 10**9+7; Mod = 998244353; INF = float('inf') # input = lambda: sys.stdin.readline().rstrip("\r\n") # inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split())) #______________________________________________________________________________________________________ # region fastio ''' BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # endregion''' #______________________________________________________________________________________________________ input = lambda: sys.stdin.readline().rstrip("\r\n") inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split())) # ______________________________________________________________________________________________________ # from math import * # from bisect import * # from heapq import * # from collections import defaultdict as dd # from collections import OrderedDict as odict # from collections import Counter as cc # from collections import deque # from itertools import groupby # sys.setrecursionlimit(5000) #this is must for dfs # ______________________________________________________________________________________________________ # segment tree for range minimum query and update 0 indexing # init = float('inf') # st = [init for i in range(4*len(a))] # def build(a,ind,start,end): # if start == end: # st[ind] = a[start] # else: # mid = (start+end)//2 # build(a,2*ind+1,start,mid) # build(a,2*ind+2,mid+1,end) # st[ind] = min(st[2*ind+1],st[2*ind+2]) # build(a,0,0,n-1) # def query(ind,l,r,start,end): # if start>r or end<l: # return init # if l<=start<=end<=r: # return st[ind] # mid = (start+end)//2 # return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end)) # def update(ind,val,stind,start,end): # if start<=ind<=end: # if start==end: # st[stind] = a[start] = val # else: # mid = (start+end)//2 # update(ind,val,2*stind+1,start,mid) # update(ind,val,2*stind+2,mid+1,end) # st[stind] = min(st[left],st[right]) # ______________________________________________________________________________________________________ # Checking prime in O(root(N)) # def isprime(n): # if (n % 2 == 0 and n > 2) or n == 1: return 0 # else: # s = int(n**(0.5)) + 1 # for i in range(3, s, 2): # if n % i == 0: # return 0 # return 1 # def lcm(a,b): # return (a*b)//gcd(a,b) # ______________________________________________________________________________________________________ # nCr under mod # def C(n,r,mod = 10**9+7): # if r>n: return 0 # if r>n-r: r = n-r # num = den = 1 # for i in range(r): # num = (num*(n-i))%mod # den = (den*(i+1))%mod # return (num*pow(den,mod-2,mod))%mod # def C(n,r): # if r>n: # return 0 # if r>n-r: # r = n-r # ans = 1 # for i in range(r): # ans = (ans*(n-i))//(i+1) # return ans # ______________________________________________________________________________________________________ # For smallest prime factor of a number # M = 2*10**5+10 # spf = [i for i in range(M)] # def spfs(M): # for i in range(2,M): # if spf[i]==i: # for j in range(i*i,M,i): # if spf[j]==j: # spf[j] = i # return # spfs(M) # ______________________________________________________________________________________________________ # def gtc(p): # print('Case #'+str(p)+': ',end='') # ______________________________________________________________________________________________________ tc = 1 # tc = int(input()) for test in range(1,tc+1): n, = inp() a = inp() b = inp() for i in range(n): a[i] = a[i]+0.0 b[i] = b[i]+0.0 lt = [a[0]*b[0]]*(n+1) for i in range(1,n): lt[i] = lt[i-1]+(a[i]*b[i]) rt = [a[-1]*b[-1]]*(n+1) for i in range(n-2,-1,-1): rt[i] = rt[i+1]+(a[i]*b[i]) lt[-1] = rt[-1] = 0 ans = lt[n-1] for i in range(n): left,right = i,i res = a[i]*b[i] ans = max(ans,res+lt[i-1]+rt[i+1]) while(left>0 and right<n-1): left-=1 right+=1 res+=a[left]*b[right]+a[right]*b[left] ans = max(ans,res+lt[left-1]+rt[right+1]) if i<n-1: left,right = i,i+1 res = a[i]*b[i+1]+a[i+1]*b[i] ans = max(ans,res+lt[i-1]+rt[i+2]) while(left>0 and right<n-1): left-=1 right+=1 res+=a[left]*b[right]+a[right]*b[left] ans = max(ans,res+lt[left-1]+rt[right+1]) print(ans) ```
instruction
0
105,507
12
211,014
No
output
1
105,507
12
211,015
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235. Submitted Solution: ``` import sys input = sys.stdin.readline n=int(input()) A=list(map(int,input().split())) B=list(map(int,input().split())) C=[A[i]*B[i] for i in range(n)] S=[0] for i in range(n): S.append(S[-1]+C[i]) ANS=S[-1] for i in range(n): XS=S[-1] for j in range(i): if i-j>=0 and i+j<n: XS-=C[i-j]+C[i+j] XS+=A[i-j]*B[i+j]+A[i+j]*B[i-j] ANS=max(ANS,XS) else: break for i in range(1,n): XS=S[-1]-C[i-1]-C[i]+A[i-1]*B[i]+A[i]*B[i-1] for j in range(1,i): if i-1-j>=0 and i+j<n: XS-=C[i-1-j]+C[i+j] XS+=A[i-1-j]*B[i+j]+A[i+j]*B[i-1-j] ANS=max(ANS,XS) else: break print(ANS) ```
instruction
0
105,508
12
211,016
No
output
1
105,508
12
211,017
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integer arrays a and b of length n. You can reverse at most one subarray (continuous subsegment) of the array a. Your task is to reverse such a subarray that the sum βˆ‘_{i=1}^n a_i β‹… b_i is maximized. Input The first line contains one integer n (1 ≀ n ≀ 5000). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^7). The third line contains n integers b_1, b_2, ..., b_n (1 ≀ b_i ≀ 10^7). Output Print single integer β€” maximum possible sum after reversing at most one subarray (continuous subsegment) of a. Examples Input 5 2 3 2 1 3 1 3 2 4 2 Output 29 Input 2 13 37 2 4 Output 174 Input 6 1 8 7 6 3 6 5 9 6 8 8 6 Output 235 Note In the first example, you can reverse the subarray [4, 5]. Then a = [2, 3, 2, 3, 1] and 2 β‹… 1 + 3 β‹… 3 + 2 β‹… 2 + 3 β‹… 4 + 1 β‹… 2 = 29. In the second example, you don't need to use the reverse operation. 13 β‹… 2 + 37 β‹… 4 = 174. In the third example, you can reverse the subarray [3, 5]. Then a = [1, 8, 3, 6, 7, 6] and 1 β‹… 5 + 8 β‹… 9 + 3 β‹… 6 + 6 β‹… 8 + 7 β‹… 8 + 6 β‹… 6 = 235. Submitted Solution: ``` import sys input=sys.stdin.readline def IN(): return int(input()) def INL(): return list(map(int,input().split())) global n global a global b def solve(): base=0 for i in range(n): base+=a[i]*b[i] res=base for i in range(n): temp=base st = i end= i+1 while st >=0 and end<n: temp-= a[st]*b[st] + a[end]*b[end] # parejo temp += a[st]*b[end] + a[end]*b[st] # cruzado res= max(res,temp) st-=1 end+=1 print(res) n=IN() a=INL() b=INL() solve() ```
instruction
0
105,509
12
211,018
No
output
1
105,509
12
211,019
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj. Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2. Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Input The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments. Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment. Output Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: * (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; * (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; * (5, 2), (2, 5) β€” match exactly.
instruction
0
105,866
12
211,732
Tags: greedy, implementation, sortings Correct Solution: ``` n=int(input()) segs=[] for i in range(n): a,b=map(int,input().split()) segs.append([a,b,i]) segs.sort(key=lambda x:[x[0],-x[1]]) for i in range(1,n): if segs[i][1]<=segs[i-1][1]: print(segs[i][2]+1,segs[i-1][2]+1) exit() segs.sort(key=lambda x:[-x[1],x[0]]) for i in range(1,n): if segs[i][0]>=segs[0][0]: print(segs[i][2]+1,segs[0][2]+1) exit() print('-1 -1') ```
output
1
105,866
12
211,733
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj. Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2. Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Input The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments. Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment. Output Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: * (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; * (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; * (5, 2), (2, 5) β€” match exactly.
instruction
0
105,867
12
211,734
Tags: greedy, implementation, sortings Correct Solution: ``` import sys input=sys.stdin.readline n = int(input()) ls = [] for i in range(n): l, r = list(map(int, input().strip().split())) ls.append([l, r, i]) ls = sorted(ls, key=lambda x: (x[0], -x[1])) flag = False for i in range(n-1): cur_l, cur_r, mother = ls[i] if cur_r >= ls[i+1][1]: print(ls[i+1][2] + 1, mother + 1) flag = True break if not flag: print(-1, -1) ```
output
1
105,867
12
211,735
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj. Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2. Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Input The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments. Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment. Output Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: * (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; * (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; * (5, 2), (2, 5) β€” match exactly.
instruction
0
105,868
12
211,736
Tags: greedy, implementation, sortings Correct Solution: ``` n = int(input()) arr = [] for i in range(n): l, r = map(int, input().split()) arr.append((l, -r, i)) ans = '-1 -1' a = sorted(arr) for i in range(len(a)): a[i] = (a[i][0], -a[i][1], a[i][2]) for i in range(len(a)-1): if a[i][0] <= a[i+1][0] and a[i][1] >= a[i+1][1]: ans = str(a[i+1][2] + 1) + ' ' + str(a[i][2] + 1) print(ans) ```
output
1
105,868
12
211,737
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj. Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2. Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Input The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments. Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment. Output Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: * (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; * (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; * (5, 2), (2, 5) β€” match exactly.
instruction
0
105,869
12
211,738
Tags: greedy, implementation, sortings Correct Solution: ``` n = int(input()) a = [] for i in range(1, n+1): l, r = map(int, input().split()) a.append((l, r, i)) a.sort() for i in range(n-1): if (a[i][0] == a[i+1][0]): print(str(a[i][2]) + ' ' + str(a[i+1][2])) break if (a[i][1] >= a[i+1][1]): print(str(a[i+1][2]) + ' ' + str(a[i][2])) break else: print('-1 -1') ```
output
1
105,869
12
211,739
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj. Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2. Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Input The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments. Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment. Output Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: * (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; * (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; * (5, 2), (2, 5) β€” match exactly.
instruction
0
105,870
12
211,740
Tags: greedy, implementation, sortings Correct Solution: ``` from sys import stdin class segment : def __init__(self,x,y,n) : self.a = x self.b = y self.no = n def __gt__(self,other): if(self.a == other.a): return self.b < other.b return self.a > other.a def __lt__(self,other): if(self.a == other.a): return self.b > other.b return self.a < other.a def __eq__(self,other): if(self.a==other.a and self.b == self.b): return True else: return False def __str__(self) : return str(self.no) n = int(stdin.readline()) arr = [] for i in range(n) : r,z = map(int,stdin.readline().split()) s = segment(r,z,i+1) arr.append(s) arr.sort() for i in range(1,n): if(arr[i].b <= arr[i-1].b) : print (arr[i],arr[i-1]) exit() print (-1,-1) ```
output
1
105,870
12
211,741
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj. Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2. Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Input The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments. Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment. Output Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: * (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; * (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; * (5, 2), (2, 5) β€” match exactly.
instruction
0
105,871
12
211,742
Tags: greedy, implementation, sortings Correct Solution: ``` n = int(input()) l = [i for i in range(n)] d = [tuple(map(int, input().split())) for _ in range(n)] l.sort(key = lambda x: (d[x][0], -d[x][1])) for i in range(n - 1): if d[l[i + 1]][1] <= d[l[i]][1]: print(l[i + 1] + 1, l[i] + 1) exit() print(-1, -1) ```
output
1
105,871
12
211,743
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj. Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2. Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Input The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments. Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment. Output Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: * (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; * (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; * (5, 2), (2, 5) β€” match exactly.
instruction
0
105,872
12
211,744
Tags: greedy, implementation, sortings Correct Solution: ``` n=int(input()) a=[list(map(int,input().split())) for i in range(n)] for i in range(n): a[i].append(i+1) a.sort() for i in range(n-1): if a[i][1]>=a[i+1][1]: exit(print(a[i+1][2],a[i][2])) if a[i][0]==a[i+1][0] and a[i][1]<a[i+1][1]: exit(print(a[i][2],a[i+1][2])) print('-1','-1') ```
output
1
105,872
12
211,745
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj. Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2. Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Input The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments. Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment. Output Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: * (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; * (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; * (5, 2), (2, 5) β€” match exactly.
instruction
0
105,873
12
211,746
Tags: greedy, implementation, sortings Correct Solution: ``` N = int(input()) A = [] for i in range(1, N+1): l, r = map(int, input().split()) A.append([l, r, i]) A.sort(key=lambda x:x[0]) if N == 1: print(-1, -1) quit() a = A[0][1] for i in range(1, N): if A[i][0] == A[i-1][0] and A[i][1] > A[i-1][1]: print(A[i-1][2], A[i][2]) quit() elif A[i][1] <= a: print(A[i][2], A[i-1][2]) quit() else: if i == N-1: print(-1, -1) quit() else: a = A[i][1] ```
output
1
105,873
12
211,747
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a_1, a_2, ..., a_n. All a_i are pairwise distinct. Let's define function f(l, r) as follows: * let's define array b_1, b_2, ..., b_{r - l + 1}, where b_i = a_{l - 1 + i}; * sort array b in increasing order; * result of the function f(l, r) is βˆ‘_{i = 1}^{r - l + 1}{b_i β‹… i}. Calculate \left(βˆ‘_{1 ≀ l ≀ r ≀ n}{f(l, r)}\right) mod (10^9+7), i.e. total sum of f for all subsegments of a modulo 10^9+7. Input The first line contains one integer n (1 ≀ n ≀ 5 β‹… 10^5) β€” the length of array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9, a_i β‰  a_j for i β‰  j) β€” array a. Output Print one integer β€” the total sum of f for all subsegments of a modulo 10^9+7 Examples Input 4 5 2 4 7 Output 167 Input 3 123456789 214365879 987654321 Output 582491518 Note Description of the first example: * f(1, 1) = 5 β‹… 1 = 5; * f(1, 2) = 2 β‹… 1 + 5 β‹… 2 = 12; * f(1, 3) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 = 25; * f(1, 4) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 + 7 β‹… 4 = 53; * f(2, 2) = 2 β‹… 1 = 2; * f(2, 3) = 2 β‹… 1 + 4 β‹… 2 = 10; * f(2, 4) = 2 β‹… 1 + 4 β‹… 2 + 7 β‹… 3 = 31; * f(3, 3) = 4 β‹… 1 = 4; * f(3, 4) = 4 β‹… 1 + 7 β‹… 2 = 18; * f(4, 4) = 7 β‹… 1 = 7;
instruction
0
106,184
12
212,368
Tags: combinatorics, data structures, math, sortings Correct Solution: ``` import sys MOD = (int)(1e9+7) def add(a, b): a += b if a >= MOD: a -= MOD return a def mul(a, b): return (a * b) % MOD class fenwickTree: def __init__(self, max_val): self.max_val = max_val + 5 self.tree = [0] * self.max_val def update(self, idx, value): idx += 1 while idx < self.max_val: self.tree[idx] = add(self.tree[idx], value) idx += (idx & (-idx)) def read(self, idx): idx += 1 res = 0 while idx > 0: res = add(res, self.tree[idx]) idx -= (idx & (-idx)) return res inp = [int(x) for x in sys.stdin.read().split()] n = inp[0] a = [] for i in range(1, n + 1): a.append(inp[i]) sorted_array = sorted(a) dict = {} for i in range(n): dict[sorted_array[i]] = i factor = [0] * n for i in range(0, n): factor[i] = mul(i + 1, n - i) left_tree = fenwickTree(n) for i in range(0, n): element_idx = dict[a[i]] factor[i] = add(factor[i], mul(n - i, left_tree.read(element_idx))) left_tree.update(element_idx, i + 1) right_tree = fenwickTree(n) for i in range(n - 1, -1, -1): element_idx = dict[a[i]] factor[i] = add(factor[i], mul(i + 1, right_tree.read(element_idx))) right_tree.update(element_idx, n - i) ans = 0 for i in range(n): ans = add(ans, mul(a[i], factor[i])) print(ans) ```
output
1
106,184
12
212,369
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_1, a_2, ..., a_n. All a_i are pairwise distinct. Let's define function f(l, r) as follows: * let's define array b_1, b_2, ..., b_{r - l + 1}, where b_i = a_{l - 1 + i}; * sort array b in increasing order; * result of the function f(l, r) is βˆ‘_{i = 1}^{r - l + 1}{b_i β‹… i}. Calculate \left(βˆ‘_{1 ≀ l ≀ r ≀ n}{f(l, r)}\right) mod (10^9+7), i.e. total sum of f for all subsegments of a modulo 10^9+7. Input The first line contains one integer n (1 ≀ n ≀ 5 β‹… 10^5) β€” the length of array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9, a_i β‰  a_j for i β‰  j) β€” array a. Output Print one integer β€” the total sum of f for all subsegments of a modulo 10^9+7 Examples Input 4 5 2 4 7 Output 167 Input 3 123456789 214365879 987654321 Output 582491518 Note Description of the first example: * f(1, 1) = 5 β‹… 1 = 5; * f(1, 2) = 2 β‹… 1 + 5 β‹… 2 = 12; * f(1, 3) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 = 25; * f(1, 4) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 + 7 β‹… 4 = 53; * f(2, 2) = 2 β‹… 1 = 2; * f(2, 3) = 2 β‹… 1 + 4 β‹… 2 = 10; * f(2, 4) = 2 β‹… 1 + 4 β‹… 2 + 7 β‹… 3 = 31; * f(3, 3) = 4 β‹… 1 = 4; * f(3, 4) = 4 β‹… 1 + 7 β‹… 2 = 18; * f(4, 4) = 7 β‹… 1 = 7; Submitted Solution: ``` def main(): MOD = 1000000007 # inp = readnumbers() # n = inp[0] # a = inp[1:] n = int(input()) a = list(map(int, input().split())) ans = 0 # for i in range(n): # ans += a[i] * (i + 1) * (n - i) # for j in range(i): # if a[j] < a[i]: # ans += a[i] * (j + 1) * (n - i) # for j in range(i + 1, n): # if a[j] < a[i]: # ans += a[i] * (n - j) * (i + 1) # print(ans % MOD) for i in range(n): a[i] = (a[i] << 20) | (i + 1) a.sort() x = [0] * (n * 6 + 10) ad1 = 0 ad2 = 0 global re re = 0 if n > 800: return M = 1 while M <= n: M *= 2 def sum1(a, b, l, r, i): if a == l and b == r: global re re += x[i] return mi = (l + r) // 2 if a < mi: sum1(a, min(mi, b), l, mi, i * 2) if mi < b: sum1(max(mi, a), b, mi, r, i * 2 + 1) def sum2(a, b, l, r, i): if a == l and b == r: global re re += y[i] return mi = (l + r) // 2 if a < mi: sum2(a, min(mi, b), l, mi, i * 2) if mi < b: sum2(max(mi, a), b, mi, r, i * 2 + 1) bm = (1 << 20) - 1 for i in a: ta = i >> 20 tb = i & bm ad1 = tb ad2 = n - tb + 1 j = M + tb while True: ja = j << 1 x[ja] += ad1 if x[ja] >= MOD: x[ja] -= MOD jb = ja | 1 x[jb] += ad2 if x[jb] >= MOD: x[jb] -= MOD if not j: break j >>= 1 tot = tb * (n - tb + 1) if tb != 1: re = 0 la = M lb = M + tb while (la ^ lb) != 1: if not (la & 1): re += x[(la << 1) ^ 2] if (lb & 1): re += x[(lb << 1) ^ 2] la >>= 1 lb >>= 1 re %= MOD tot += re * (n - tb + 1) if tb != n: re = 0 la = M + tb lb = M + n + 1 while (la ^ lb) != 1: if not (la & 1): re += x[(la << 1) ^ 3] if (lb & 1): re += x[(lb << 1) ^ 3] la >>= 1 lb >>= 1 re %= MOD tot += re * tb tot %= MOD ans = (ans + ta * tot) % MOD print(ans) ######## Python 2 and 3 footer by Pajenegod and c1729 # Note because cf runs old PyPy3 version which doesn't have the sped up # unicode strings, PyPy3 strings will many times be slower than pypy2. # There is a way to get around this by using binary strings in PyPy3 # but its syntax is different which makes it kind of a mess to use. # So on cf, use PyPy2 for best string performance. py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' # Read all remaining integers in stdin, type is given by optional argument, this is fast def readnumbers(zero = 0): conv = ord if py2 else lambda x:x A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read() try: while True: if s[i] >= b'0' [0]: numb = 10 * numb + conv(s[i]) - 48 elif s[i] == b'-' [0]: sign = -1 elif s[i] != b'\r' [0]: A.append(sign*numb) numb = zero; sign = 1 i += 1 except:pass if s and s[-1] >= b'0' [0]: A.append(sign*numb) return A if __name__== "__main__": main() ```
instruction
0
106,185
12
212,370
No
output
1
106,185
12
212,371
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_1, a_2, ..., a_n. All a_i are pairwise distinct. Let's define function f(l, r) as follows: * let's define array b_1, b_2, ..., b_{r - l + 1}, where b_i = a_{l - 1 + i}; * sort array b in increasing order; * result of the function f(l, r) is βˆ‘_{i = 1}^{r - l + 1}{b_i β‹… i}. Calculate \left(βˆ‘_{1 ≀ l ≀ r ≀ n}{f(l, r)}\right) mod (10^9+7), i.e. total sum of f for all subsegments of a modulo 10^9+7. Input The first line contains one integer n (1 ≀ n ≀ 5 β‹… 10^5) β€” the length of array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9, a_i β‰  a_j for i β‰  j) β€” array a. Output Print one integer β€” the total sum of f for all subsegments of a modulo 10^9+7 Examples Input 4 5 2 4 7 Output 167 Input 3 123456789 214365879 987654321 Output 582491518 Note Description of the first example: * f(1, 1) = 5 β‹… 1 = 5; * f(1, 2) = 2 β‹… 1 + 5 β‹… 2 = 12; * f(1, 3) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 = 25; * f(1, 4) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 + 7 β‹… 4 = 53; * f(2, 2) = 2 β‹… 1 = 2; * f(2, 3) = 2 β‹… 1 + 4 β‹… 2 = 10; * f(2, 4) = 2 β‹… 1 + 4 β‹… 2 + 7 β‹… 3 = 31; * f(3, 3) = 4 β‹… 1 = 4; * f(3, 4) = 4 β‹… 1 + 7 β‹… 2 = 18; * f(4, 4) = 7 β‹… 1 = 7; Submitted Solution: ``` n = int(input()) m = ([int(x) for x in input().split()]) b = [0] * (n+1) def f(l, r): global b for ii in range(r-l+1): b[ii] = m[l-1+ii] c = sorted(b[:r-l+1].copy()) s = 0 for ii in range(r - l+1): s += c[ii]*(ii+1) del c return s ss = 0 p = 10**9 + 7 for i in range(n): for j in range(n): ss += f(i+1, j+1) % p print(ss) ```
instruction
0
106,186
12
212,372
No
output
1
106,186
12
212,373
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_1, a_2, ..., a_n. All a_i are pairwise distinct. Let's define function f(l, r) as follows: * let's define array b_1, b_2, ..., b_{r - l + 1}, where b_i = a_{l - 1 + i}; * sort array b in increasing order; * result of the function f(l, r) is βˆ‘_{i = 1}^{r - l + 1}{b_i β‹… i}. Calculate \left(βˆ‘_{1 ≀ l ≀ r ≀ n}{f(l, r)}\right) mod (10^9+7), i.e. total sum of f for all subsegments of a modulo 10^9+7. Input The first line contains one integer n (1 ≀ n ≀ 5 β‹… 10^5) β€” the length of array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9, a_i β‰  a_j for i β‰  j) β€” array a. Output Print one integer β€” the total sum of f for all subsegments of a modulo 10^9+7 Examples Input 4 5 2 4 7 Output 167 Input 3 123456789 214365879 987654321 Output 582491518 Note Description of the first example: * f(1, 1) = 5 β‹… 1 = 5; * f(1, 2) = 2 β‹… 1 + 5 β‹… 2 = 12; * f(1, 3) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 = 25; * f(1, 4) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 + 7 β‹… 4 = 53; * f(2, 2) = 2 β‹… 1 = 2; * f(2, 3) = 2 β‹… 1 + 4 β‹… 2 = 10; * f(2, 4) = 2 β‹… 1 + 4 β‹… 2 + 7 β‹… 3 = 31; * f(3, 3) = 4 β‹… 1 = 4; * f(3, 4) = 4 β‹… 1 + 7 β‹… 2 = 18; * f(4, 4) = 7 β‹… 1 = 7; Submitted Solution: ``` maxn = int(5e6 + 1) mod = int(1e9 + 7) n = int(input()) rbit = [0] * maxn lbit = [0] * maxn def update(left, i, val): if i == 0: return while i <= n: if left: lbit[i] = lbit[i] + val else: rbit[i] = rbit[i] + val i = i + (i & (-i)) def query(left, i): res = 0 while i > 0: if left: res = res + lbit[i] else: res = res + rbit[i] i = i - (i & (-i)) return res % mod a = sorted(list(map(lambda kv: (int(kv[1]), kv[0] + 1), enumerate(input().split())))) res = 0 for i in range(n): val = a[i][0] idx = a[i][1] rdx = n - idx + 1 res = (res + val * (idx * rdx + val * (query(True, idx) * idx + query(False, idx) * rdx)) % mod) % mod update(True, 1, rdx) update(True, idx, -rdx) update(False, idx + 1, idx) print(res % mod) ```
instruction
0
106,187
12
212,374
No
output
1
106,187
12
212,375
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a_1, a_2, ..., a_n. All a_i are pairwise distinct. Let's define function f(l, r) as follows: * let's define array b_1, b_2, ..., b_{r - l + 1}, where b_i = a_{l - 1 + i}; * sort array b in increasing order; * result of the function f(l, r) is βˆ‘_{i = 1}^{r - l + 1}{b_i β‹… i}. Calculate \left(βˆ‘_{1 ≀ l ≀ r ≀ n}{f(l, r)}\right) mod (10^9+7), i.e. total sum of f for all subsegments of a modulo 10^9+7. Input The first line contains one integer n (1 ≀ n ≀ 5 β‹… 10^5) β€” the length of array a. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9, a_i β‰  a_j for i β‰  j) β€” array a. Output Print one integer β€” the total sum of f for all subsegments of a modulo 10^9+7 Examples Input 4 5 2 4 7 Output 167 Input 3 123456789 214365879 987654321 Output 582491518 Note Description of the first example: * f(1, 1) = 5 β‹… 1 = 5; * f(1, 2) = 2 β‹… 1 + 5 β‹… 2 = 12; * f(1, 3) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 = 25; * f(1, 4) = 2 β‹… 1 + 4 β‹… 2 + 5 β‹… 3 + 7 β‹… 4 = 53; * f(2, 2) = 2 β‹… 1 = 2; * f(2, 3) = 2 β‹… 1 + 4 β‹… 2 = 10; * f(2, 4) = 2 β‹… 1 + 4 β‹… 2 + 7 β‹… 3 = 31; * f(3, 3) = 4 β‹… 1 = 4; * f(3, 4) = 4 β‹… 1 + 7 β‹… 2 = 18; * f(4, 4) = 7 β‹… 1 = 7; Submitted Solution: ``` n=int(input()) b=list(map(int,input().split())) sum=0 for i in range(n): for j in range(i,n): a=b[:(j-i+1)] a.sort() for k in range(1,j-i+2): sum=sum+k*a[k-1] r=sum%(100000007) print(r) ```
instruction
0
106,188
12
212,376
No
output
1
106,188
12
212,377
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
106,205
12
212,410
Tags: constructive algorithms, greedy, math Correct Solution: ``` n=int(input()) if n%2: print("YES") ls=[0]*2*n c=1 i=-1 while c<=2*n: i=i+1 ls[i]=c i=(i+n)%(2*n) ls[i]=c+1 c=c+2 print(*ls) else: print("NO") ```
output
1
106,205
12
212,411
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
106,206
12
212,412
Tags: constructive algorithms, greedy, math Correct Solution: ``` def almost_equal(n): if n%2==0: return None else: r = [1] r2 = [2] m = 1 while m<n: m += 2 r += [2*m,2*m -3] r2 += [2*m-1,2*m-2] return r+r2 n = int(input().strip()) r = almost_equal(n) if r: print("YES") print(' '.join([str(x) for x in r])) else: print("NO") ```
output
1
106,206
12
212,413
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
106,207
12
212,414
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) a = [0] * (2*n) curr = 1 for i in range(n): if i%2 == 0: a[i] = curr curr += 1 a[i+n] = curr curr += 1 else: a[i+n] = curr curr += 1 a[i] = curr curr += 1 arr = a + a[:n-1] s = set() window = sum(a[:n]) s.add(window) for i in range(n, 3*n-1): window += arr[i] window -= arr[i-n] s.add(window) if len(s) > 2: print("NO") else: print("YES") print(*a) ```
output
1
106,207
12
212,415
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
106,208
12
212,416
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) n2 = 2 * n if n == 1: print('YES\n1 2') else: a = [None] * n2 now = n2 flag = True for i in range(n): if flag: a[i] = now a[n + i] = now - 1 else: a[i] = now - 1 a[n + i] = now flag = 1 - flag now -= 2 min_ans = sum(a[:n]) max_ans = min_ans now = min_ans for i in range(n2): now -= a[i] now += a[(n + i) % n2] min_ans = min(min_ans, now) max_ans = max(max_ans, now) if max_ans - min_ans > 1: print('NO') else: print('YES') print(' '.join(list(map(str, a)))) ```
output
1
106,208
12
212,417
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
106,209
12
212,418
Tags: constructive algorithms, greedy, math Correct Solution: ``` n=int(input()) if(n%2!=0): a=[0]*(2*n+1) for i in range(1,n+1): if(i%2==1): a[i]=2*i a[n+i]=2*i-1 else: a[i]=2*i-1 a[n+i]=2*i print("YES") for i in range(1,2*n+1): print(a[i],end=' ') else: print("NO") ```
output
1
106,209
12
212,419
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
106,210
12
212,420
Tags: constructive algorithms, greedy, math Correct Solution: ``` n = int(input()) a = [1] b = [] counter = 2 for i in range(2, 2*n + 1): if counter < 2: a.append(i) else: b.append(i) counter = (counter + 1) % 4 c = a + b sums = [] sum = 0 for i in range(n): sum += c[i] sums.append(sum) for i in range(len(c) - n): sum = sum - c[i] + c[i + n] sums.append(sum) sum = sum - c[len(c) - n] + c[0] sums.append(sum) answer = True comp = sorted(sums)[-1] for i in sums: if(abs(i - comp) > 1): answer = False #print(sums) if(answer): print('YES') print(*a, *b) else: print('NO') ```
output
1
106,210
12
212,421
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≀ n ≀ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers β€” numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
106,212
12
212,424
Tags: constructive algorithms, greedy, math Correct Solution: ``` import sys readline = sys.stdin.readline N = int(readline()) if N & 1: print('YES') Ans = list(range(1, 1+2*N, 2)) + list(range(2, 1+2*N, 2)) for i in range(0, N, 2): Ans[i], Ans[i+N] = Ans[i+N], Ans[i] print(*Ans) else: print('NO') ```
output
1
106,212
12
212,425
Provide tags and a correct Python 3 solution for this coding contest problem. A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers n,k. Construct a grid A with size n Γ— n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k. Let's define: * A_{i,j} as the integer in the i-th row and the j-th column. * R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 ≀ i ≀ n). * C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 ≀ j ≀ n). * In other words, R_i are row sums and C_j are column sums of the grid A. * For the grid A let's define the value f(A) = (max(R)-min(R))^2 + (max(C)-min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Next t lines contain descriptions of test cases. For each test case the only line contains two integers n, k (1 ≀ n ≀ 300, 0 ≀ k ≀ n^2). It is guaranteed that the sum of n^2 for all test cases does not exceed 10^5. Output For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied. After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to A_{i,j}. If there are multiple answers you can print any. Example Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 Note In the first test case, the sum of all elements in the grid is equal to 2, so the condition is satisfied. R_1 = 1, R_2 = 1 and C_1 = 1, C_2 = 1. Then, f(A) = (1-1)^2 + (1-1)^2 = 0, which is the minimum possible value of f(A). In the second test case, the sum of all elements in the grid is equal to 8, so the condition is satisfied. R_1 = 3, R_2 = 3, R_3 = 2 and C_1 = 3, C_2 = 2, C_3 = 3. Then, f(A) = (3-2)^2 + (3-2)^2 = 2. It can be proven, that it is the minimum possible value of f(A).
instruction
0
106,305
12
212,610
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` import math import sys t = int(input()) result = [] for cs in range(t): n, k = map(int, input().split()) a = [[0] * n for _ in range(n)] result.append('0' if k % n == 0 else '2') for i in range(n): cur = 0 while cur < n and k > 0: a[cur][(i + cur) % n] = 1 k -= 1 cur += 1 for i in range(n): result.append(''.join(map(str, a[i]))) print('\n'.join(result)) ```
output
1
106,305
12
212,611
Provide tags and a correct Python 3 solution for this coding contest problem. A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers n,k. Construct a grid A with size n Γ— n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k. Let's define: * A_{i,j} as the integer in the i-th row and the j-th column. * R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 ≀ i ≀ n). * C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 ≀ j ≀ n). * In other words, R_i are row sums and C_j are column sums of the grid A. * For the grid A let's define the value f(A) = (max(R)-min(R))^2 + (max(C)-min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Next t lines contain descriptions of test cases. For each test case the only line contains two integers n, k (1 ≀ n ≀ 300, 0 ≀ k ≀ n^2). It is guaranteed that the sum of n^2 for all test cases does not exceed 10^5. Output For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied. After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to A_{i,j}. If there are multiple answers you can print any. Example Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 Note In the first test case, the sum of all elements in the grid is equal to 2, so the condition is satisfied. R_1 = 1, R_2 = 1 and C_1 = 1, C_2 = 1. Then, f(A) = (1-1)^2 + (1-1)^2 = 0, which is the minimum possible value of f(A). In the second test case, the sum of all elements in the grid is equal to 8, so the condition is satisfied. R_1 = 3, R_2 = 3, R_3 = 2 and C_1 = 3, C_2 = 2, C_3 = 3. Then, f(A) = (3-2)^2 + (3-2)^2 = 2. It can be proven, that it is the minimum possible value of f(A).
instruction
0
106,306
12
212,612
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` t = int(input()) for test in range(t): n,k = [int(i) for i in input().split()] tab = [["0" for c in range(n)] for r in range(n)] row = 0 col = 0 while k>0: tab[row][col] = "1" row = (row+1)%n col += 1 if col==n: col = 0 row = (row+1)%n k -= 1 if col==0: print(0) else: print(2) for row in range(n): print(''.join(tab[row])) ```
output
1
106,306
12
212,613
Provide tags and a correct Python 3 solution for this coding contest problem. A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers n,k. Construct a grid A with size n Γ— n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k. Let's define: * A_{i,j} as the integer in the i-th row and the j-th column. * R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 ≀ i ≀ n). * C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 ≀ j ≀ n). * In other words, R_i are row sums and C_j are column sums of the grid A. * For the grid A let's define the value f(A) = (max(R)-min(R))^2 + (max(C)-min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Next t lines contain descriptions of test cases. For each test case the only line contains two integers n, k (1 ≀ n ≀ 300, 0 ≀ k ≀ n^2). It is guaranteed that the sum of n^2 for all test cases does not exceed 10^5. Output For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied. After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to A_{i,j}. If there are multiple answers you can print any. Example Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 Note In the first test case, the sum of all elements in the grid is equal to 2, so the condition is satisfied. R_1 = 1, R_2 = 1 and C_1 = 1, C_2 = 1. Then, f(A) = (1-1)^2 + (1-1)^2 = 0, which is the minimum possible value of f(A). In the second test case, the sum of all elements in the grid is equal to 8, so the condition is satisfied. R_1 = 3, R_2 = 3, R_3 = 2 and C_1 = 3, C_2 = 2, C_3 = 3. Then, f(A) = (3-2)^2 + (3-2)^2 = 2. It can be proven, that it is the minimum possible value of f(A).
instruction
0
106,307
12
212,614
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` import sys input = lambda:sys.stdin.readline().strip() t = int(input()) while t: t-=1 n,k = map(int,input().split()) if k%n==0: print(0) else: print(2) ans = [[0]*n for _ in range(n)] p = 0 q = 0 while k: ans[p][q] = 1 k-=1 p+=1 q+=1 q%=n if p==n: p=0 q+=1 q%=n for i in range(n): print(''.join(map(str,ans[i]))) ```
output
1
106,307
12
212,615
Provide tags and a correct Python 3 solution for this coding contest problem. A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers n,k. Construct a grid A with size n Γ— n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k. Let's define: * A_{i,j} as the integer in the i-th row and the j-th column. * R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 ≀ i ≀ n). * C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 ≀ j ≀ n). * In other words, R_i are row sums and C_j are column sums of the grid A. * For the grid A let's define the value f(A) = (max(R)-min(R))^2 + (max(C)-min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Next t lines contain descriptions of test cases. For each test case the only line contains two integers n, k (1 ≀ n ≀ 300, 0 ≀ k ≀ n^2). It is guaranteed that the sum of n^2 for all test cases does not exceed 10^5. Output For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied. After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to A_{i,j}. If there are multiple answers you can print any. Example Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 Note In the first test case, the sum of all elements in the grid is equal to 2, so the condition is satisfied. R_1 = 1, R_2 = 1 and C_1 = 1, C_2 = 1. Then, f(A) = (1-1)^2 + (1-1)^2 = 0, which is the minimum possible value of f(A). In the second test case, the sum of all elements in the grid is equal to 8, so the condition is satisfied. R_1 = 3, R_2 = 3, R_3 = 2 and C_1 = 3, C_2 = 2, C_3 = 3. Then, f(A) = (3-2)^2 + (3-2)^2 = 2. It can be proven, that it is the minimum possible value of f(A).
instruction
0
106,308
12
212,616
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` for z in range(int(input())): n,k = list(map(int,input().split())) arr = [[0 for i in range(n)] for i in range(n)] c = 0 s = [0,0] while(k!=0): # print(s) arr[s[0]][s[1]]=1 s[0] = (s[0]+1)%n s[1] = (s[1]+1)%n c+=1 if(c==n): c=0 s[1]=(s[1]+1)%n k-=1 min_c,max_c = n,0 for i in range(n): col = 0 for j in range(n): if(arr[j][i]): col+=1 if col<min_c: min_c = col if col>max_c: max_c =col min_r,max_r = n,0 for i in range(n): row = 0 for j in range(n): if(arr[i][j]): row+=1 if row<min_r: min_r = row if row>max_r: max_r =row print((max_c-min_c)**2 + (max_r-min_r)**2) for i in arr: for j in i: print(j,end="") print() ```
output
1
106,308
12
212,617
Provide tags and a correct Python 3 solution for this coding contest problem. A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers n,k. Construct a grid A with size n Γ— n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k. Let's define: * A_{i,j} as the integer in the i-th row and the j-th column. * R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 ≀ i ≀ n). * C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 ≀ j ≀ n). * In other words, R_i are row sums and C_j are column sums of the grid A. * For the grid A let's define the value f(A) = (max(R)-min(R))^2 + (max(C)-min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Next t lines contain descriptions of test cases. For each test case the only line contains two integers n, k (1 ≀ n ≀ 300, 0 ≀ k ≀ n^2). It is guaranteed that the sum of n^2 for all test cases does not exceed 10^5. Output For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied. After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to A_{i,j}. If there are multiple answers you can print any. Example Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 Note In the first test case, the sum of all elements in the grid is equal to 2, so the condition is satisfied. R_1 = 1, R_2 = 1 and C_1 = 1, C_2 = 1. Then, f(A) = (1-1)^2 + (1-1)^2 = 0, which is the minimum possible value of f(A). In the second test case, the sum of all elements in the grid is equal to 8, so the condition is satisfied. R_1 = 3, R_2 = 3, R_3 = 2 and C_1 = 3, C_2 = 2, C_3 = 3. Then, f(A) = (3-2)^2 + (3-2)^2 = 2. It can be proven, that it is the minimum possible value of f(A).
instruction
0
106,309
12
212,618
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` input=__import__('sys').stdin.readline for _ in range(int(input())): n,k=map(int,input().split()) print(2if k%n else 0) ans=[['0']*n for i in range(n)] x=y=0 while k:k-=1;ans[x][y]='1';y=(y+1+int(x==n-1))%n;x=(x+1)%n for i in ans:print(''.join(i)) ```
output
1
106,309
12
212,619
Provide tags and a correct Python 3 solution for this coding contest problem. A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers n,k. Construct a grid A with size n Γ— n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k. Let's define: * A_{i,j} as the integer in the i-th row and the j-th column. * R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 ≀ i ≀ n). * C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 ≀ j ≀ n). * In other words, R_i are row sums and C_j are column sums of the grid A. * For the grid A let's define the value f(A) = (max(R)-min(R))^2 + (max(C)-min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Next t lines contain descriptions of test cases. For each test case the only line contains two integers n, k (1 ≀ n ≀ 300, 0 ≀ k ≀ n^2). It is guaranteed that the sum of n^2 for all test cases does not exceed 10^5. Output For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied. After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to A_{i,j}. If there are multiple answers you can print any. Example Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 Note In the first test case, the sum of all elements in the grid is equal to 2, so the condition is satisfied. R_1 = 1, R_2 = 1 and C_1 = 1, C_2 = 1. Then, f(A) = (1-1)^2 + (1-1)^2 = 0, which is the minimum possible value of f(A). In the second test case, the sum of all elements in the grid is equal to 8, so the condition is satisfied. R_1 = 3, R_2 = 3, R_3 = 2 and C_1 = 3, C_2 = 2, C_3 = 3. Then, f(A) = (3-2)^2 + (3-2)^2 = 2. It can be proven, that it is the minimum possible value of f(A).
instruction
0
106,310
12
212,620
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` t = int(input()) while t!=0: n,k=map(int,input().split()) list1=[[0 for i in range(n)] for j in range(n)] i,j=0,0 ans=0 if k%n==0: ans=0 else: ans=2 for _ in range(k): list1[i][j]=1 i+=1 j=(j+1)%n if i==n: i=0 j=(j+1)%n print(ans) for i in list1: for j in i: print(j,end='') print() t-=1 ```
output
1
106,310
12
212,621
Provide tags and a correct Python 3 solution for this coding contest problem. A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers n,k. Construct a grid A with size n Γ— n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k. Let's define: * A_{i,j} as the integer in the i-th row and the j-th column. * R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 ≀ i ≀ n). * C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 ≀ j ≀ n). * In other words, R_i are row sums and C_j are column sums of the grid A. * For the grid A let's define the value f(A) = (max(R)-min(R))^2 + (max(C)-min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Next t lines contain descriptions of test cases. For each test case the only line contains two integers n, k (1 ≀ n ≀ 300, 0 ≀ k ≀ n^2). It is guaranteed that the sum of n^2 for all test cases does not exceed 10^5. Output For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied. After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to A_{i,j}. If there are multiple answers you can print any. Example Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 Note In the first test case, the sum of all elements in the grid is equal to 2, so the condition is satisfied. R_1 = 1, R_2 = 1 and C_1 = 1, C_2 = 1. Then, f(A) = (1-1)^2 + (1-1)^2 = 0, which is the minimum possible value of f(A). In the second test case, the sum of all elements in the grid is equal to 8, so the condition is satisfied. R_1 = 3, R_2 = 3, R_3 = 2 and C_1 = 3, C_2 = 2, C_3 = 3. Then, f(A) = (3-2)^2 + (3-2)^2 = 2. It can be proven, that it is the minimum possible value of f(A).
instruction
0
106,311
12
212,622
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` import sys import math strInp = lambda : input().strip().split() intInp = lambda : list(map(int,strInp())) for t in range(int(input())): n , k = intInp() if k == 0: print(0) for i in range(n): for j in range(n): print('0', end="") print() else: if k%n == 0: print(0) else: print(2) ans = [] for i in range(n): ans.append(['0'] * n) i = 0 #rows j = 0 #columns circle = 0 while k > 0: ans[i][j] = '1' i += 1 j += 1 circle += 1 if i == n: i = 0 if j == n: j = 0 if circle % n == 0: j = circle//n k -= 1 for i in range(n): for j in range(n): print(ans[i][j], end="") print() ```
output
1
106,311
12
212,623
Provide tags and a correct Python 3 solution for this coding contest problem. A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it! You are given integers n,k. Construct a grid A with size n Γ— n consisting of integers 0 and 1. The very important condition should be satisfied: the sum of all elements in the grid is exactly k. In other words, the number of 1 in the grid is equal to k. Let's define: * A_{i,j} as the integer in the i-th row and the j-th column. * R_i = A_{i,1}+A_{i,2}+...+A_{i,n} (for all 1 ≀ i ≀ n). * C_j = A_{1,j}+A_{2,j}+...+A_{n,j} (for all 1 ≀ j ≀ n). * In other words, R_i are row sums and C_j are column sums of the grid A. * For the grid A let's define the value f(A) = (max(R)-min(R))^2 + (max(C)-min(C))^2 (here for an integer sequence X we define max(X) as the maximum value in X and min(X) as the minimum value in X). Find any grid A, which satisfies the following condition. Among such grids find any, for which the value f(A) is the minimum possible. Among such tables, you can find any. Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Next t lines contain descriptions of test cases. For each test case the only line contains two integers n, k (1 ≀ n ≀ 300, 0 ≀ k ≀ n^2). It is guaranteed that the sum of n^2 for all test cases does not exceed 10^5. Output For each test case, firstly print the minimum possible value of f(A) among all tables, for which the condition is satisfied. After that, print n lines contain n characters each. The j-th character in the i-th line should be equal to A_{i,j}. If there are multiple answers you can print any. Example Input 4 2 2 3 8 1 0 4 16 Output 0 10 01 2 111 111 101 0 0 0 1111 1111 1111 1111 Note In the first test case, the sum of all elements in the grid is equal to 2, so the condition is satisfied. R_1 = 1, R_2 = 1 and C_1 = 1, C_2 = 1. Then, f(A) = (1-1)^2 + (1-1)^2 = 0, which is the minimum possible value of f(A). In the second test case, the sum of all elements in the grid is equal to 8, so the condition is satisfied. R_1 = 3, R_2 = 3, R_3 = 2 and C_1 = 3, C_2 = 2, C_3 = 3. Then, f(A) = (3-2)^2 + (3-2)^2 = 2. It can be proven, that it is the minimum possible value of f(A).
instruction
0
106,312
12
212,624
Tags: constructive algorithms, greedy, implementation Correct Solution: ``` for _ in range(int(input())): n,k=map(int,input().split()) a=[["0" for i in range(n+1)] for j in range(n+1)] r=1 c=1 for i in range(k): a[r][c]="1" r+=1 c+=1 if c==n+1: c=1 if r==n+1: r=1 c=(i+1)//n +1 if k%n: print(2) else: print(0) for i in a[1:]: print("".join(i[1:])) ```
output
1
106,312
12
212,625
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem MEX of a certain array is the smallest positive integer not contained in this array. Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today. You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers. An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself. Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays! Input The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the length of the array. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” the elements of the array. Output Print a single integer β€” the MEX of MEXes of all subarrays. Examples Input 3 1 3 2 Output 3 Input 5 1 4 3 1 2 Output 6
instruction
0
106,341
12
212,682
Tags: binary search, data structures, two pointers Correct Solution: ``` from typing import TypeVar, Generic, Callable, List import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') T = TypeVar('T') class SegmentTree(Generic[T]): __slots__ = ["size", "tree", "identity", "op", "update_op"] def __init__(self, size: int, identity: T, op: Callable[[T, T], T], update_op: Callable[[T, T], T]) -> None: self.size = size self.tree = [identity] * (size * 2) self.identity = identity self.op = op self.update_op = update_op def build(self, a: List[T]) -> None: tree = self.tree tree[self.size:self.size + len(a)] = a for i in range(self.size - 1, 0, -1): tree[i] = self.op(tree[i << 1], tree[(i << 1) + 1]) def find(self, left: int, right: int) -> T: left += self.size right += self.size tree, result, op = self.tree, self.identity, self.op while left < right: if left & 1: result = op(tree[left], result) left += 1 if right & 1: result = op(tree[right - 1], result) left, right = left >> 1, right >> 1 return result def update(self, i: int, value: T) -> None: op, tree = self.op, self.tree i = self.size + i tree[i] = self.update_op(tree[i], value) while i > 1: i >>= 1 tree[i] = op(tree[i << 1], tree[(i << 1) + 1]) n = int(input()) a = list(map(int, input().split())) segt = SegmentTree[int](n + 10, 10**9, min, max) segt.build([-1] * (n + 10)) prev = [-1] * (n + 10) flag = [0] + [1] * (n + 10) for i, x in enumerate(a): if x != 1: flag[1] = 0 left = segt.find(1, x) if left > prev[x]: flag[x] = 0 prev[x] = i segt.update(x, i) for i, f in enumerate(flag): if f and not (prev[i] < segt.find(1, i) < n): print(i) exit() ```
output
1
106,341
12
212,683
Provide tags and a correct Python 3 solution for this coding contest problem. In this problem MEX of a certain array is the smallest positive integer not contained in this array. Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today. You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers. An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself. Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays! Input The first line contains a single integer n (1 ≀ n ≀ 10^5) β€” the length of the array. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” the elements of the array. Output Print a single integer β€” the MEX of MEXes of all subarrays. Examples Input 3 1 3 2 Output 3 Input 5 1 4 3 1 2 Output 6
instruction
0
106,342
12
212,684
Tags: binary search, data structures, two pointers Correct Solution: ``` from math import inf, log2 class SegmentTree: def __init__(self, array, func=max): self.n = len(array) self.size = 2**(int(log2(self.n-1))+1) if self.n != 1 else 1 self.func = func self.default = 0 if self.func != min else inf self.data = [self.default] * (2 * self.size) self.process(array) def process(self, array): self.data[self.size : self.size+self.n] = array for i in range(self.size-1, -1, -1): self.data[i] = self.func(self.data[2*i], self.data[2*i+1]) def query(self, alpha, omega): """Returns the result of function over the range (inclusive)!""" if alpha == omega: return self.data[alpha + self.size] res = self.default alpha += self.size omega += self.size + 1 while alpha < omega: if alpha & 1: res = self.func(res, self.data[alpha]) alpha += 1 if omega & 1: omega -= 1 res = self.func(res, self.data[omega]) alpha >>= 1 omega >>= 1 return res def update(self, index, value): """Updates the element at index to given value!""" index += self.size self.data[index] = value index >>= 1 while index: self.data[index] = self.func(self.data[2*index], self.data[2*index+1]) index >>= 1 # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- from math import gcd, ceil def prod(a, mod=10**9+7): ans = 1 for each in a: ans = (ans * each) % mod return ans def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y for _ in range(int(input()) if not True else 1): n = int(input()) #n, m = map(int, input().split()) #a, b = map(int, input().split()) #c, d = map(int, input().split()) a = list(map(int, input().split())) if a == [1]*n: print(1) quit() di = {} for i in range(n): if a[i] not in di: di[a[i]] = [-1] di[a[i]] += [i] queries = [] dx = [0]*(n+69) for each in di: di[each] += [n] for j in range(len(di[each])-1): if di[each][j]+1 == n or di[each][j+1]-1 == -1:continue queries += [[di[each][j]+1, di[each][j+1]-1, each]] queries += [[0, n-1, n+3]] queries = sorted(queries, key=lambda x: x[1]) st = SegmentTree([69696969] + [-1]*(n+69), func=min) j = 0 for i in range(n): st.update(a[i], i) while j < len(queries) and queries[j][1] == i: less_than = queries[j][0] alpha, omega = 0, n+1 while alpha < omega: mid = (alpha+omega) // 2 if st.query(alpha, mid) < less_than: omega = mid else: alpha = mid + 1 #print(queries[j][0], queries[j][1], "->", omega) dx[queries[j][2]] = max(dx[queries[j][2]], omega) j += 1 ans = 0 for i in range(1, n+2): if dx[i] != i: ans = i break if dx[n+3] == ans: ans += 1 #print(dx) print(ans) ```
output
1
106,342
12
212,685