message stringlengths 2 433k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,225 | 12 | 106,450 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
aas = []
for _ in range(int(input())):
d, m = map(int, input().split())
A = []
s = 1
sus = 0
while sus + s <= d:
A.append(s)
sus += s
s *= 2
A.append(d - sum(A))
ans = 1
for x in A:
ans *= (x+1)
ans %= m
ans -= 1
ans %= m
aas.append(str(ans))
print('\n'.join(aas))
``` | output | 1 | 53,225 | 12 | 106,451 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n ≥ 1
* 1 ≤ a_1 < a_2 < ... < a_n ≤ d
* Define an array b of length n as follows: b_1 = a_1, ∀ i > 1, b_i = b_{i - 1} ⊕ a_i, where ⊕ is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 ≤ t ≤ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 ≤ d, m ≤ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0 | instruction | 0 | 53,226 | 12 | 106,452 |
Tags: bitmasks, combinatorics, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
for i in arr:
stdout.write(str(i)+' ')
stdout.write('\n')
range = xrange # not for python 3.0+
for t in range(input()):
d,m=in_arr()
ans=1
for i in range(30):
if d<(1<<i):
break
ans=(ans*(min((1<<(i+1))-1,d)-(1<<i)+2))%m
ans=(ans-1)%m
pr_num(ans)
``` | output | 1 | 53,226 | 12 | 106,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 53,235 | 12 | 106,470 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from bisect import *
from collections import *
from math import gcd,ceil,sqrt,floor,inf
from heapq import *
from itertools import *
#from operator import add,mul,sub,xor,truediv,floordiv
from functools import *
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# 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)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
#------------------------------------------------------------------------
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
farr=[1]
ifa=[]
def fact(x,mod=0):
if mod:
while x>=len(farr):
farr.append(farr[-1]*len(farr)%mod)
else:
while x>=len(farr):
farr.append(farr[-1]*len(farr))
return farr[x]
def ifact(x,mod):
global ifa
fact(x,mod)
ifa.append(pow(farr[-1],mod-2,mod))
for i in range(x,0,-1):
ifa.append(ifa[-1]*i%mod)
ifa.reverse()
def per(i,j,mod=0):
if i<j: return 0
if not mod:
return fact(i)//fact(i-j)
return farr[i]*ifa[i-j]%mod
def com(i,j,mod=0):
if i<j: return 0
if not mod:
return per(i,j)//fact(j)
return per(i,j,mod)*ifa[j]%mod
def catalan(n):
return com(2*n,n)//(n+1)
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def lowbit(n):
return n&-n
def inverse(a,m):
a%=m
if a<=1: return a
return ((1-inverse(m,a)*m)//a)%m
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
'''
class SMT:
def __init__(self,arr):
self.n=len(arr)-1
self.arr=[0]*(self.n<<2)
self.lazy=[0]*(self.n<<2)
def Build(l,r,rt):
if l==r:
self.arr[rt]=arr[l]
return
m=(l+r)>>1
Build(l,m,rt<<1)
Build(m+1,r,rt<<1|1)
self.pushup(rt)
Build(1,self.n,1)
def pushup(self,rt):
self.arr[rt]=self.arr[rt<<1]+self.arr[rt<<1|1]
def pushdown(self,rt,ln,rn):#lr,rn表区间数字数
if self.lazy[rt]:
self.lazy[rt<<1]+=self.lazy[rt]
self.lazy[rt<<1|1]+=self.lazy[rt]
self.arr[rt<<1]+=self.lazy[rt]*ln
self.arr[rt<<1|1]+=self.lazy[rt]*rn
self.lazy[rt]=0
def update(self,L,R,c,l=1,r=None,rt=1):#L,R表示操作区间
if r==None: r=self.n
if L<=l and r<=R:
self.arr[rt]+=c*(r-l+1)
self.lazy[rt]+=c
return
m=(l+r)>>1
self.pushdown(rt,m-l+1,r-m)
if L<=m: self.update(L,R,c,l,m,rt<<1)
if R>m: self.update(L,R,c,m+1,r,rt<<1|1)
self.pushup(rt)
def query(self,L,R,l=1,r=None,rt=1):
if r==None: r=self.n
#print(L,R,l,r,rt)
if L<=l and R>=r:
return self.arr[rt]
m=(l+r)>>1
self.pushdown(rt,m-l+1,r-m)
ans=0
if L<=m: ans+=self.query(L,R,l,m,rt<<1)
if R>m: ans+=self.query(L,R,m+1,r,rt<<1|1)
return ans
'''
class DSU:#容量+路径压缩
def __init__(self,n):
self.c=[-1]*n
def same(self,x,y):
return self.find(x)==self.find(y)
def find(self,x):
if self.c[x]<0:
return x
self.c[x]=self.find(self.c[x])
return self.c[x]
def union(self,u,v):
u,v=self.find(u),self.find(v)
if u==v:
return False
if self.c[u]>self.c[v]:
u,v=v,u
self.c[u]+=self.c[v]
self.c[v]=u
return True
def size(self,x): return -self.c[self.find(x)]
class UFS:#秩+路径
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return prime
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
seen.add(u)
for v,w in graph[u]:
if v not in d or d[v]>d[u]+w:
d[v]=d[u]+w
heappush(heap,(d[v],v))
return d
def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)]
class DLN:
def __init__(self,val):
self.val=val
self.pre=None
self.next=None
def nb(i,j):
for ni,nj in [[i+1,j],[i-1,j],[i,j-1],[i,j+1]]:
if 0<=ni<n and 0<=nj<m:
yield ni,nj
@bootstrap
def gdfs(r,p):
if len(g[r])==1 and p!=-1:
yield None
for ch in g[r]:
if ch!=p:
yield gdfs(ch,r)
yield None
def lcm(a,b):
return a*b//gcd(a,b)
t=N()
for i in range(t):
n,k=RL()
f=False
a=RLL()
ans='no'
for i in range(n):
if a[i]<k:
a[i]=0
elif a[i]==k:
f=True
if f:
if n<2:
ans='yes'
else:
pre=-inf
f2=False
for i in range(n):
if a[i]:
if i-pre<3:
f2=True
break
pre=i
if f2:
ans='yes'
print(ans)
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thread(target=main)
t.start()
t.join()
'''
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thread(target=main)
t.start()
t.join()
'''
``` | output | 1 | 53,235 | 12 | 106,471 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 53,236 | 12 | 106,472 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from functools import reduce,cmp_to_key,lru_cache
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
# import sys
# input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip().split()]
def st():return str(input().rstrip())[2:-1]
def val():return int(input().rstrip())
def li2():return [str(i)[2:-1] for i in input().rstrip().split()]
def li3():return [int(str(i)[2:-1]) for i in input().rstrip()]
def do2(l):
n = len(l)
for i in range(2,n + 1):
if sum(l[i-2:i]) > 0:return 1
for i in range(2,n):
if sum(l[i-2:i + 1]) > 0:return 1
return 0
for _ in range(val()):
n,k = li()
l = li()
if k not in l:
print('NO')
continue
if n == 1:
print('YES')
continue
for i in range(n):
if l[i] < k:l[i] = - 1
else:l[i] = 1
print('YES' if do2(l[:]) or do2(l[::-1]) else 'NO')
``` | output | 1 | 53,236 | 12 | 106,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 53,237 | 12 | 106,474 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,ls=None,Directed=False,index=1):
org_inp=[];g=[[] for i in range(V)]
FromStdin=True if ls==None else False
for i in range(E):
if FromStdin:
inp=LI()
org_inp.append(inp)
else:
inp=ls[i]
if len(inp)==2:
a,b=inp;c=1
else:
a,b,c=inp
if index==1:a-=1;b-=1
aa=(a,c);bb=(b,c);g[a].append(bb)
if not Directed:g[b].append(aa)
return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
mp=[boundary]*(w+2);found={}
for i in range(h):
s=input()
for char in search:
if char in s:
found[char]=((i+1)*(w+2)+s.index(char)+1)
mp_def[char]=mp_def[replacement_of_found]
mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
mp+=[boundary]*(w+2)
return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(n,base=2):
rt=[]
for tb in range(base**n):s=[tb//(base**bt)%base for bt in range(n)];rt+=[s]
return rt
def gcd(x,y):
if y==0:return x
if x%y==0:return y
while x%y!=0:x,y=y,x%y
return y
def show(*inp,end='\n'):
if show_flg:print(*inp,end=end)
YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
FourNb=[(1,0),(-1,0),(0,1),(0,-1)];EightNb=[(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('EWNS',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
class Tree:
def __init__(self,inp_size=None,ls=None,init=True,index=0):
self.LCA_init_stat=False
self.ETtable=[]
if init:
if ls==None:
self.stdin(inp_size,index=index)
else:
self.size=len(ls)+1
self.edges,_=GI(self.size,self.size-1,ls,index=index)
return
def stdin(self,inp_size=None,index=1):
if inp_size==None:
self.size=int(input())
else:
self.size=inp_size
self.edges,_=GI(self.size,self.size-1,index=index)
return
def listin(self,ls,index=0):
self.size=len(ls)+1
self.edges,_=GI(self.size,self.size-1,ls,index=index)
return
def __str__(self):
return str(self.edges)
def dfs(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
q=deque()
q.append(x)
v=[-1]*self.size
v[x]=root_v
while q:
c=q.pop()
for nb,d in self.edges[c]:
if v[nb]==-1:
q.append(nb)
v[nb]=func(v[c],nb,d)
return v
def EulerTour(self,x):
q=deque()
q.append(x)
self.depth=[None]*self.size
self.depth[x]=0
self.ETtable=[]
self.ETdepth=[]
self.ETin=[-1]*self.size
self.ETout=[-1]*self.size
cnt=0
while q:
c=q.pop()
if c<0:
ce=~c
else:
ce=c
for nb,d in self.edges[ce]:
if self.depth[nb]==None:
q.append(~ce)
q.append(nb)
self.depth[nb]=self.depth[ce]+1
self.ETtable.append(ce)
self.ETdepth.append(self.depth[ce])
if self.ETin[ce]==-1:
self.ETin[ce]=cnt
else:
self.ETout[ce]=cnt
cnt+=1
return
def LCA_init(self,root):
self.EulerTour(root)
self.st=SparseTable(self.ETdepth,init_func=min,init_idl=inf)
self.LCA_init_stat=True
return
def LCA(self,root,x,y):
if self.LCA_init_stat==False:
self.LCA_init(root)
xin,xout=self.ETin[x],self.ETout[x]
yin,yout=self.ETin[y],self.ETout[y]
a=min(xin,yin)
b=max(xout,yout,xin,yin)
id_of_min_dep_in_et=self.st.query_id(a,b+1)
return self.ETtable[id_of_min_dep_in_et]
class SparseTable: # O(N log N) for init, O(1) for query(l,r)
def __init__(self,ls,init_func=min,init_idl=float('inf')):
self.func=init_func
self.idl=init_idl
self.size=len(ls)
self.N0=self.size.bit_length()
self.table=[ls[:]]
self.index=[list(range(self.size))]
self.lg=[0]*(self.size+1)
for i in range(2,self.size+1):
self.lg[i]=self.lg[i>>1]+1
for i in range(self.N0):
tmp=[self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) for j in range(self.size)]
tmp_id=[self.index[i][j] if self.table[i][j]==self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) else self.index[i][min(j+(1<<i),self.size-1)] for j in range(self.size)]
self.table+=[tmp]
self.index+=[tmp_id]
# return func of [l,r)
def query(self,l,r):
if r>self.size:r=self.size
#N=(r-l).bit_length()-1
N=self.lg[r-l]
return self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))])
# return index of which val[i] = func of v among [l,r)
def query_id(self,l,r):
if r>self.size:r=self.size
#N=(r-l).bit_length()-1
N=self.lg[r-l]
a,b=self.index[N][l],self.index[N][max(0,r-(1<<N))]
if self.table[0][a]==self.func(self.table[N][l],self.table[N][r-(1<<N)]):
b=a
return b
def __str__(self):
return str(self.table[0])
def print(self):
for i in self.table:
print(*i)
class Comb:
def __init__(self,n,mo=10**9+7):
self.fac=[0]*(n+1)
self.inv=[1]*(n+1)
self.fac[0]=1
self.fact(n)
for i in range(1,n+1):
self.fac[i]=i*self.fac[i-1]%mo
self.inv[n]*=i
self.inv[n]%=mo
self.inv[n]=pow(self.inv[n],mo-2,mo)
for i in range(1,n):
self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
return
def fact(self,n):
return self.fac[n]
def invf(self,n):
return self.inv[n]
def comb(self,x,y):
if y<0 or y>x:
return 0
return self.fac[x]*self.inv[x-y]*self.inv[y]%mo
show_flg=False
show_flg=True
ans=0
def solve(n,k,a):
if k not in a:
return 'no'
if n==1 and a[0]==k:
return 'yes'
b=[1 if i>=k else 0 for i in a]
for i in range(n-2):
if sum(b[i:i+3])>=2 or sum(b[i:i+2])==2:
return 'yes'
if n>=2 and sum(b[-2:])==2:
return 'yes'
return 'no'
T=I()
for _ in range(T):
ans=0
n,k=LI()
a=LI()
a1=solve(n,k,a)
print(a1)
``` | output | 1 | 53,237 | 12 | 106,475 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 53,238 | 12 | 106,476 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
def check_gk(A, k):
N = len(A)
for i in range(N-1):
if A[i] >= k and A[i+1] >= k:
return True
for i in range(N-2):
if A[i] >= k and A[i+2] >= k:
return True
return False
def answer(b):
if b:
print("yes")
else:
print("no")
t = int(input())
for i in range(t):
n, k = input().split(' ')
n, k = int(n), int(k)
A = list(map(int, input().split(' ')))
if n <= 1:
answer(k in A)
else:
answer(k in A and check_gk(A, k))
``` | output | 1 | 53,238 | 12 | 106,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 53,239 | 12 | 106,478 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
t = int(input())
out = []
for tt in range(t):
n, k = map(int, input().split())
if n == 1:
out.append("yes" if int(input()) == k else "no")
continue
a = []
hasa = False
for i in input().split():
x = int(i)
if x == k:
hasa = True
a.append(x >= k)
if not hasa:
out.append("no")
continue
ans = a[-1] and a[-2]
for i in range(n-2):
if a[i] and (a[i+1] or a[i+2]):
ans = True
break
if ans:
break
out.append("yes" if ans else "no")
print('\n'.join(out))
``` | output | 1 | 53,239 | 12 | 106,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 53,240 | 12 | 106,480 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
n,k=map(int,input().split())
ar=list(map(int,input().split()))
if(k not in ar):
print("No")
elif(n==1):
print("Yes")
else:
ans="No"
for i in range(n-1):
if(ar[i]>=k<=ar[i+1]):
ans="Yes"
for i in range(n-2):
if(ar[i]>=k<=ar[i+2]):
ans="Yes"
print(ans)
``` | output | 1 | 53,240 | 12 | 106,481 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 53,241 | 12 | 106,482 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from sys import stdin
input=stdin.buffer.readline
T=int(input())
for _ in range(T):
n,k = map(int,input().split())
ls = list(map(int,input().split()))
if n==1: print("yes" if ls[0]==k else "no"); continue
c1,c2=0,0
for i,u in enumerate(ls):
if u==k: c1=1
if i>0 and ls[i-1]>=k and u>=k:
c2=1
if i>1 and ls[i-2]>=k and u>=k:
c2=1
print("yes" if c1 and c2 else "no")
``` | output | 1 | 53,241 | 12 | 106,483 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10]. | instruction | 0 | 53,242 | 12 | 106,484 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
printn = lambda x: print(x,end='')
import sys
input = sys.stdin.readline
inn = lambda : int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
def ddprint(x):
if DBG:
print(x)
t = inn()
for tt in range(t):
n,k = inm()
a = inl()
if a.count(k)==0:
print('no')
continue
if n==1:
print('yes')
continue
#ddprint(f"n {n} k {k} a {a}")
done = False
for i in range(n):
if a[i]<k:
continue
sml = 0
for j in range(i-1,-1,-1):
sml += 1 if a[j]<k else -1
if sml<=0:
#ddprint(f"y1 i {i} j {j} sml {sml}")
print('yes')
done = True
break
if a[j]>=k:
break
if done:
break
sml = 0
for j in range(i+1,n):
sml += 1 if a[j]<k else -1
if sml<=0:
#ddprint(f"y2 i {i} j {j} sml {sml}")
print('yes')
done = True
break
if a[j]>=k:
break
if done:
break
if not done:
print('no')
``` | output | 1 | 53,242 | 12 | 106,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11]. | instruction | 0 | 53,271 | 12 | 106,542 |
Tags: greedy, sortings
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
ar=list(map(int,input().split()))
br=list(map(int,input().split()))
li=[]
for i in range(n):
if(br[i]==0):
li.append(ar[i])
li.sort(reverse=True)
j=0
for i in range(n):
if(br[i]==0):
ar[i]=li[j]
j+=1
print(*ar)
``` | output | 1 | 53,271 | 12 | 106,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11]. | instruction | 0 | 53,272 | 12 | 106,544 |
Tags: greedy, sortings
Correct Solution:
```
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
lock = list(map(int, input().split()))
b = []
for i in range(n):
if lock[i] == 0:
b.append(a[i])
b.sort()
b=b[::-1]
ind = 0
for i in range(n):
if lock[i] == 0:
a[i]=b[ind]
ind+=1
a=[str(i) for i in a]
print(" ".join(a))
``` | output | 1 | 53,272 | 12 | 106,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11]. | instruction | 0 | 53,273 | 12 | 106,546 |
Tags: greedy, sortings
Correct Solution:
```
# 1
import itertools
import math
import sys
from collections import defaultdict
def stdinWrapper():
data = '''5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
'''
for line in data.split('\n'):
yield line
if '--debug' not in sys.argv:
def stdinWrapper():
while True:
yield input()
inputs = stdinWrapper()
def inputWrapper():
return next(inputs)
def getType(_type):
return _type(inputWrapper())
def getArray(_type):
return [_type(x) for x in inputWrapper().split()]
''' Solution '''
def solve(a, locked):
def pref_sum(data):
res = [data[0]]
for i in range(1, len(data)):
res.append(res[i-1] + data[i])
return res
def k(data):
data = pref_sum(data)
for i in reversed(range(len(data))):
if data[i] < 0:
return i+1
return 0
unlocked = []
for i in range(len(a)):
if not locked[i]:
unlocked.append(a[i])
unlocked_sorted = list(reversed(sorted(unlocked)))
result = []
for i in range(len(a)):
if locked[i]:
result.append(a[i])
else:
result.append(unlocked_sorted.pop(0))
# print(pref_sum(result))
# print(k(result))
return ' '.join([str(x) for x in result])
t = getType(int)
for _ in range(t):
n = getType(int)
a = getArray(int)
locked = [bool(x) for x in getArray(int)]
print(solve(a, locked))
``` | output | 1 | 53,273 | 12 | 106,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11]. | instruction | 0 | 53,274 | 12 | 106,548 |
Tags: greedy, sortings
Correct Solution:
```
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
# from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var): sys.stdout.write(' '.join(map(str, var)) + '\n')
def out(var): sys.stdout.write(str(var) + '\n')
from decimal import Decimal
# from fractions import Fraction
# sys.setrecursionlimit(100000)
mod = int(1e9) + 7
INF=float('inf')
for t in range(int(data())):
n=int(data())
a=mdata()
l=mdata()
un=[]
for i in range(n):
if l[i]==0:
un.append(a[i])
k=0
s=0
if un:
un.sort(reverse=True)
cnt=0
for i in range(n):
if l[i] == 0:
a[i]= un[cnt]
cnt += 1
outl(a)
``` | output | 1 | 53,274 | 12 | 106,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11]. | instruction | 0 | 53,275 | 12 | 106,550 |
Tags: greedy, sortings
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
unlock=[]
for i in range(len(l1)):
if(l2[i]==0):
unlock.append(l1[i])
l1[i]="a"
unlock.sort()
for i in range(n):
if(l1[i]=="a"):
l1[i]=unlock[-1]
unlock=unlock[:-1]
ans=""
for i in range(n):
ans+=str(l1[i])+" "
print(ans)
``` | output | 1 | 53,275 | 12 | 106,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11]. | instruction | 0 | 53,276 | 12 | 106,552 |
Tags: greedy, sortings
Correct Solution:
```
#------------------------------warmup----------------------------
# *******************************
# * AUTHOR: RAJDEEP GHOSH *
# * NICK : Rajdeep2k *
# * INSTITUTION: IIEST, SHIBPUR *
# *******************************
import os
import sys
from io import BytesIO, IOBase
import math
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")
#-------------------game starts now---------------------------------------------------
t=(int)(input())
for _ in range(t):
n=(int)(input())
l=list(map(int,input().split()))
# a,b=map(int,input().split())
pl=list(map(int,input().split()))
ar=list()
for i in range(n):
if pl[i]==0:
ar.append(l[i])
ar.sort(reverse=True)
c=0
ans=list()
for i in range(n):
if pl[i]==1:
ans.append(l[i])
else:
ans.append(ar[c])
c+=1
print(*ans)
``` | output | 1 | 53,276 | 12 | 106,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11]. | instruction | 0 | 53,277 | 12 | 106,554 |
Tags: greedy, sortings
Correct Solution:
```
y=lambda:[*map(int,input().split())]
r=range
for _ in r(int(input())):
n=int(input());a=y();l=y();b=[]
for i in r(n):
if l[i]<1:b+=[a[i]]
b.sort()
for i in r(n):
if l[i]<1:a[i]=b.pop()
print(' '.join(map(str,a)))
``` | output | 1 | 53,277 | 12 | 106,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11]. | instruction | 0 | 53,278 | 12 | 106,556 |
Tags: greedy, sortings
Correct Solution:
```
I=input
for _ in[0]*int(I()):
I();*a,=map(int,I().split());b=I()[::2];c=sorted(x
for x,y in zip(a,b)if'1'>y);i=k=l=s=0;j=-1
for x in b:
if'1'>x:a[i]=c[j];j-=1
i+=1
for x in a:l+=1;s+=x;k=(k,l)[l<0]
print(*a)
``` | output | 1 | 53,278 | 12 | 106,557 |
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, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
Submitted Solution:
```
import sys
pin = sys.stdin.readline
for T in range(int(pin())):
N = int(pin())
A = [*map(int,pin().split())]
L = [*map(int,pin().split())]
R = [0 for n in range(N)]
S = []
for n in range(N):
a,l = A[n],L[n]
if l: R[n] = a
else: S.append(a)
S.sort(reverse=True)
i = 0
for n in range(N):
if L[n]: continue
else: R[n] = S[i]; i += 1
for r in R: print(r,end=' ')
print()
``` | instruction | 0 | 53,279 | 12 | 106,558 |
Yes | output | 1 | 53,279 | 12 | 106,559 |
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, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
Submitted Solution:
```
t=int(input())
for i in range(t):
n=int(input())
arr=[int(i) for i in input().split()]
locks=[int(i) for i in input().split()]
new_arr=[arr[i] if locks[i]==1 else float('-inf') for i in range(n)]
curr=[]
for i in range(n):
if locks[i]==0:
curr.append(arr[i])
curr=sorted(curr)[::-1]
j=0
for i in range(n):
if new_arr[i]==float('-inf'):
new_arr[i]=curr[j]
j+=1
else:
pass
for j in new_arr:
print(j,end=" ")
print()
``` | instruction | 0 | 53,280 | 12 | 106,560 |
Yes | output | 1 | 53,280 | 12 | 106,561 |
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, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
Submitted Solution:
```
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
l=[a[i] for i in range(n) if b[i]==0]
l.sort(reverse =True)
c=0
for i in range(n):
if b[i]==0:
a[i]=l[c]
c+=1
print(*a)
``` | instruction | 0 | 53,281 | 12 | 106,562 |
Yes | output | 1 | 53,281 | 12 | 106,563 |
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, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
Submitted Solution:
```
from sys import stdin, stdout
def main():
t = int(stdin.readline())
outputs = []
for __ in range(t):
output = []
arr = []
n = int(stdin.readline())
a = [int(num) for num in stdin.readline().split()]
l = [int(num) for num in stdin.readline().split()]
for i in range(n):
if not l[i]:
arr.append(a[i])
else:
continue
arr.sort()
for i in range(n):
if not l[i]:
output.append(f'{arr.pop()}')
else:
output.append(f'{a[i]}')
outputs.append(' '.join(output)+'\n')
for output in outputs:
stdout.write(output)
if __name__ == '__main__':
main()
``` | instruction | 0 | 53,282 | 12 | 106,564 |
Yes | output | 1 | 53,282 | 12 | 106,565 |
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, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
A = list(map(int, input().split()))
L = list(map(int, input().split()))
P = []
N = []
for i in range(n):
if L[i] == 0:
if A[i] >= 0:
P.append(A[i])
else:
N.append(A[i])
P.sort(reverse=True)
s = sum(A)
if s >= 0:
N.sort()
else:
N.sort(reverse=True)
T = P+N
T.reverse()
ans = []
for i in range(n):
if L[i] == 1:
ans.append(A[i])
else:
ans.append(T.pop())
print(*ans)
``` | instruction | 0 | 53,283 | 12 | 106,566 |
No | output | 1 | 53,283 | 12 | 106,567 |
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, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
Submitted Solution:
```
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
a=list(map(int,input().split()))
s=[l[i] for i in range(n) if a[i]==0]
s.sort(reverse=True)
print(' '.join(list(map(str,l))))
``` | instruction | 0 | 53,284 | 12 | 106,568 |
No | output | 1 | 53,284 | 12 | 106,569 |
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, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
Submitted Solution:
```
import sys
LI=lambda:list(map(int, sys.stdin.readline().split()))
MI=lambda:map(int, sys.stdin.readline().split())
SI=lambda:sys.stdin.readline().strip('\n')
II=lambda:int(sys.stdin.readline())
for _ in range(II()):
n=II()
a=LI()
l, u=[], []
tp=LI()
for i, v in enumerate(tp):
if v:
l.append(a[i])
else:
u.append(a[i])
rsu=sorted(u, reverse=True)
p, i, j, ok=0, 0, 0, 1
for v in tp:
if v:
p+=l[i]
i+=1
else:
p+=rsu[j]
j+=1
if p<0:
ok=0
break
ans=[]
i, j=0, 0
if ok:
for v in tp:
if v:
ans.append(l[i])
i+=1
else:
ans.append(rsu[j])
j+=1
else:
su=sorted(u)
for v in tp:
if v:
ans.append(l[i])
i+=1
else:
ans.append(su[j])
j+=1
print(*ans)
``` | instruction | 0 | 53,285 | 12 | 106,570 |
No | output | 1 | 53,285 | 12 | 106,571 |
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, consisting of n integers.
Each position i (1 ≤ i ≤ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 ≤ j ≤ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 ≤ n ≤ 100) — the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 ≤ a_i ≤ 10^5) — the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 ≤ l_i ≤ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers — the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
l = list(map(int, input().split()))
unlocked = [a[i] for i in range(n) if not l[i]]
unlocked.sort(reverse=True)
a1 = a.copy()
a2 = a.copy()
ps = 0
j = 0
k1 = 0
for i in range(n):
if not l[i]:
a1[i] = unlocked[j]
j += 1
ps += a1[i]
if ps < 0:
k1 = i
unlocked.reverse()
ps = 0
j = 0
k2 = 0
for i in range(n):
if not l[i]:
a2[i] = unlocked[j]
j += 1
ps += a2[i]
if ps < 0:
k2 = i
if k1 < k2:
print(*a1)
else:
print(*a2)
``` | instruction | 0 | 53,286 | 12 | 106,572 |
No | output | 1 | 53,286 | 12 | 106,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i. | instruction | 0 | 53,287 | 12 | 106,574 |
Tags: math
Correct Solution:
```
for i in range(int(input())):
m = int(input().split()[1])
s = sum(map(int, input().split()))
print('NO' if s!=m else 'YES')
``` | output | 1 | 53,287 | 12 | 106,575 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i. | instruction | 0 | 53,288 | 12 | 106,576 |
Tags: math
Correct Solution:
```
# import sys
# sys.stdin = open('input.txt','r')
# sys.stdout = open('output.txt','w')
t = int(input())
while t:
n,m = map(int,input().split())
arr = list(map(int,input().split()))
s = sum(arr)
if s == m:
print('YES')
else:
print('NO')
t-=1
``` | output | 1 | 53,288 | 12 | 106,577 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i. | instruction | 0 | 53,289 | 12 | 106,578 |
Tags: math
Correct Solution:
```
t=int(input())
for i in range(t):
n,m=map(int,input().split())
arr=list(map(int,input().split()))
if sum(arr)==m:
print("YES")
else:
print("NO")
``` | output | 1 | 53,289 | 12 | 106,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i. | instruction | 0 | 53,290 | 12 | 106,580 |
Tags: math
Correct Solution:
```
t = int(input())
for _ in range(t):
n,m = [int(i) for i in input().split(" ")]
a = [int(i) for i in input().split(" ")]
print("YES" if sum(a) == m else "NO")
``` | output | 1 | 53,290 | 12 | 106,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i. | instruction | 0 | 53,291 | 12 | 106,582 |
Tags: math
Correct Solution:
```
for _ in range(int(input())):
n,m=map(int,input().split())
arr=list(map(int,input().split()))
sum1=0
for i in range(1,n+1):
for j in range(i,n+1):
sum1+=arr[j-1]/j
if(round(sum1)==m):
print("YES")
else:
print("NO")
``` | output | 1 | 53,291 | 12 | 106,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i. | instruction | 0 | 53,292 | 12 | 106,584 |
Tags: math
Correct Solution:
```
for _ in range(int(input())):
n, m = input().split()
m = int(m)
a = list(map(int, input().split()))
if sum(a) == m:
print("YES")
else:
print("NO")
``` | output | 1 | 53,292 | 12 | 106,585 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i. | instruction | 0 | 53,293 | 12 | 106,586 |
Tags: math
Correct Solution:
```
cases = int(input())
for case in range(cases):
_, m = input().split()
print('YES' if sum(map(int, input().split())) == int(m) else 'NO')
``` | output | 1 | 53,293 | 12 | 106,587 |
Provide tags and a correct Python 3 solution for this coding contest problem.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i. | instruction | 0 | 53,294 | 12 | 106,588 |
Tags: math
Correct Solution:
```
for i in range(int(input())):
n,k=list(map(int,input().split()))
if sum(list(map(int,input().split())))==k:
print("YES")
else:
print("NO")
``` | output | 1 | 53,294 | 12 | 106,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Submitted Solution:
```
def solve(n,m):
s=list(map(int,input().split()))
if sum(s)==m :
return 'YES'
return 'NO'
for _ in range(int(input())):
n,m=map(int,input().split())
print(solve(n,m))
``` | instruction | 0 | 53,295 | 12 | 106,590 |
Yes | output | 1 | 53,295 | 12 | 106,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Submitted Solution:
```
# ------------------- 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 --------------------
import math
from collections import Counter,defaultdict,deque
def int1():
return int(input())
def map1():
return map(int,input().split())
def list1():
return list(map(int,input().split()))
mod=pow(10,9)+7
def solve():
n,m=map1()
l1=list1()
sum1=sum(l1)
if(sum1==m):
print("YES")
else:
print("NO")
for _ in range(int(input())):
solve()
``` | instruction | 0 | 53,296 | 12 | 106,592 |
Yes | output | 1 | 53,296 | 12 | 106,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Submitted Solution:
```
for _ in range(int(input())):
n, m = map(int, input().split())
sm = sum(map(int, input().split()))
if m == sm:
print("YES")
else:
print("NO")
``` | instruction | 0 | 53,297 | 12 | 106,594 |
Yes | output | 1 | 53,297 | 12 | 106,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Submitted Solution:
```
for i in range(int(input())):
n,m = map(int,input().split())
li = list(map(int,input().split()))
li.sort()
s = 0
for i in range(len(li)):
for j in range(i,len(li)):
s += li[j]/(j+1)
if round(s)==m:
print('YES')
else:
print('NO')
``` | instruction | 0 | 53,298 | 12 | 106,596 |
Yes | output | 1 | 53,298 | 12 | 106,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Submitted Solution:
```
import sys, os.path
from collections import*
from heapq import *
from copy import*
import math
mod=10**9+7
read = lambda: map(int, input().split())
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r")
sys.stdout = open("output.txt","w")
for t in range(int(input())):
summ=0
n,m=read()
a=list(read())
a.sort()
for i in range(n):
for j in range(i,n):
summ+=(a[j]/(j+1))
if(summ==m):
print('YES')
else:
print('NO')
``` | instruction | 0 | 53,299 | 12 | 106,598 |
No | output | 1 | 53,299 | 12 | 106,599 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Submitted Solution:
```
# #include<bits/stdc++.h>
# #include<libraries.h>
# #include<atcoder/all>
# int main(){
# }
T = int(input())
for _ in range(T):
n, m = map(int, input().split())
num_list = list(map(int, input().split()))
ans = 0
for n1 in range(n):
for n2 in range(n1, n):
ans += num_list[n2]/(n2+1)
if ans < m + 1e-12 and ans > m - 1e-12:
print("YES")
else :
print("NO")
``` | instruction | 0 | 53,300 | 12 | 106,600 |
No | output | 1 | 53,300 | 12 | 106,601 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Submitted Solution:
```
t=int(input())
c=0
for i in range(t):
s=input().split()
n=int(s[0])
m=int(s[1])
a=input().split()
a.sort(key=int)
for j in range(1,n):
for k in range(1,n):
c+=float(a[j])/k
if (c==m):
print('YES')
else:
print('NO')
``` | instruction | 0 | 53,301 | 12 | 106,602 |
No | output | 1 | 53,301 | 12 | 106,603 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Submitted Solution:
```
# cook your dish here
import sys
import math
input = lambda: sys.stdin.readline().rstrip()
for _ in range(int(input())):
n,m=map(int,input().split())
l=[int(x) for x in input().split()]
l.sort()
a1=0
for i in range(n):
for j in range(i,n):
a1+=l[j]/(j+1)
l=l[::-1]
a2=0
for i in range(n):
for j in range(i,n):
a2+=l[j]/(j+1)
if(m>=a2 and m<=a1):
print("YES")
else:
print("NO")
``` | instruction | 0 | 53,302 | 12 | 106,604 |
No | output | 1 | 53,302 | 12 | 106,605 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array of integers b_1, b_2, …, b_n.
An array a_1, a_2, …, a_n of integers is hybrid if for each i (1 ≤ i ≤ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = ∑_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, …, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5).
The second line of each test case contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, …, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1]. | instruction | 0 | 53,303 | 12 | 106,606 |
Tags: combinatorics, data structures, dp, sortings
Correct Solution:
```
from collections import defaultdict;mod = 10 ** 9 + 7
for _ in range(int(input())):
n = int(input());B = list(map(int, input().split()));dp = defaultdict(int);cur, ans = 0, 0
for i, b in enumerate(B):v = 1 if not i else (ans - dp[cur]) % mod;ans = (ans + v) % mod;dp[cur] = (dp[cur] + v) % mod;cur += b
print(ans)
``` | output | 1 | 53,303 | 12 | 106,607 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array of integers b_1, b_2, …, b_n.
An array a_1, a_2, …, a_n of integers is hybrid if for each i (1 ≤ i ≤ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = ∑_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, …, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5).
The second line of each test case contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, …, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1]. | instruction | 0 | 53,304 | 12 | 106,608 |
Tags: combinatorics, data structures, dp, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import defaultdict
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
dp = defaultdict(int)
dp[0] = 1
ans = 1
cur = 0
for a in A:
extra = dp[cur]
dp[cur] = ans
cur -= a
ans = (2 * ans - extra) % mod
print(ans)
``` | output | 1 | 53,304 | 12 | 106,609 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array of integers b_1, b_2, …, b_n.
An array a_1, a_2, …, a_n of integers is hybrid if for each i (1 ≤ i ≤ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = ∑_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, …, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5).
The second line of each test case contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, …, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1]. | instruction | 0 | 53,305 | 12 | 106,610 |
Tags: combinatorics, data structures, dp, sortings
Correct Solution:
```
p=lambda:list(map(int,input().split()))
for t in range(p()[0]):
N=p()[0]
B=p()
C=[0]*(N+1)
for i in range(N):C[i+1]=C[i]+B[i]
S=1
D=dict()
D[0]=1
for i in range(1,N):D[C[i]],S=S,(2*S-D.get(C[i],0))%(10**9+7)
print(S)
``` | output | 1 | 53,305 | 12 | 106,611 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array of integers b_1, b_2, …, b_n.
An array a_1, a_2, …, a_n of integers is hybrid if for each i (1 ≤ i ≤ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = ∑_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, …, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5).
The second line of each test case contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, …, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1]. | instruction | 0 | 53,306 | 12 | 106,612 |
Tags: combinatorics, data structures, dp, sortings
Correct Solution:
```
T = int(input())
for t in range(T):
n = int(input())
bb = [int(x) for x in input().split()]
sums_bef_offset = {bb[0]: 1}
offset = 0
all_sums = 1
base = 1000000007
result = 0
for i in range(1, n):
b = bb[i]
sums_0 = sums_bef_offset.get(-offset, 0)
# If we consider the aa[i] as equal to bb[i], then all the sum of aa[i] will be added by bb[i]
# in this case we just need to change the offset
offset += b
# Another case is where we consider aa[i] as the sum of aa[i] equal to bb[i]. In this case, the sums[bb[i]] will become previous all_sums (then we can update all_sums by adding previous all_sums - previous sums[0])
prev_all_sums = all_sums
sums_bef_offset[b - offset] = prev_all_sums
# Update all_sums
all_sums = (2*prev_all_sums - sums_0) % base
# print("[{}] all_sums: {}, sums_bef_offset: {}, offset: {}".format(i, all_sums, sums_bef_offset, offset))
print(all_sums)
``` | output | 1 | 53,306 | 12 | 106,613 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array of integers b_1, b_2, …, b_n.
An array a_1, a_2, …, a_n of integers is hybrid if for each i (1 ≤ i ≤ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = ∑_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, …, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5).
The second line of each test case contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, …, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1]. | instruction | 0 | 53,307 | 12 | 106,614 |
Tags: combinatorics, data structures, dp, sortings
Correct Solution:
```
import sys
import math
def II():
return int(sys.stdin.readline())
def LI():
return list(map(int, sys.stdin.readline().split()))
def MI():
return map(int, sys.stdin.readline().split())
def SI():
return sys.stdin.readline().strip()
def FACT(n, mod):
s = 1
facts = [1]
for i in range(1,n+1):
s*=i
s%=mod
facts.append(s)
return facts[n]
def C(n, k, mod):
return (FACT(n,mod) * pow((FACT(k,mod)*FACT(n-k,mod))%mod,mod-2, mod))%mod
def lcm(a, b):
return abs(a*b) // math.gcd(a, b)
mod = 10**9+7
for _ in range(II()):
n = II()
b = LI()
d = {}
dp = [0 for i in range(n+1)]
dp[0] = 1
d[0] = 1
s = 0
for i in range(n):
dp[i+1] = (dp[i]*2 - d.get(s,0)+mod)%mod
d[s] = dp[i]
s+=b[i]
print(dp[-1])
``` | output | 1 | 53,307 | 12 | 106,615 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array of integers b_1, b_2, …, b_n.
An array a_1, a_2, …, a_n of integers is hybrid if for each i (1 ≤ i ≤ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = ∑_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, …, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5).
The second line of each test case contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, …, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1]. | instruction | 0 | 53,308 | 12 | 106,616 |
Tags: combinatorics, data structures, dp, sortings
Correct Solution:
```
p=lambda:list(map(int,input().split()))
for t in range(p()[0]):
N=p()[0];B=p();C=[0]*(N+1)
for i in range(N):C[i+1]=C[i]+B[i]
S=1;D=dict();D[0]=1
for i in range(1,N):D[C[i]],S=S,(2*S-D.get(C[i],0))%(10**9+7)
print(S)
``` | output | 1 | 53,308 | 12 | 106,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array of integers b_1, b_2, …, b_n.
An array a_1, a_2, …, a_n of integers is hybrid if for each i (1 ≤ i ≤ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = ∑_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, …, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5).
The second line of each test case contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, …, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1]. | instruction | 0 | 53,309 | 12 | 106,618 |
Tags: combinatorics, data structures, dp, sortings
Correct Solution:
```
import collections
import string
import math
import copy
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)
def input(): return sys.stdin.readline().rstrip("\r\n")
# n = 0
# m = 0
# n = int(input())
# li = [int(i) for i in input().split()]
# s = sorted(li)
mo = int(1e9+7)
def exgcd(a, b):
if not b:
return 1, 0
y, x = exgcd(b, a % b)
y -= a//b * x
return x, y
def getinv(a, m):
x, y = exgcd(a, m)
return -(-1) if x == 1 else x % m
def comb(n, b):
res = 1
b = min(b, n-b)
for i in range(b):
res = res*(n-i)*getinv(i+1, mo) % mo
# res %= mo
return res % mo
def quickpower(a, n):
res = 1
while n:
if n & 1:
res = res * a % mo
n >>= 1
a = a*a % mo
return res
def dis(a, b):
return abs(a[0]-b[0]) + abs(a[1]-b[1])
def getpref(x):
if x > 1:
return (x)*(x-1) >> 1
else:
return 0
def orafli(upp):
primes = []
marked = [False for i in range(upp+3)]
for i in range(2, upp):
if not marked[i]:
primes.append(i)
for j in primes:
if i*j >= upp:
break
marked[i*j] = True
if i % j == 0:
break
return primes
def solve():
n = int(input())
l = [int(i) for i in input().split()]
d = {0:1}
tt = 1
k = 0
for i in range(1,1+n):
a = d.setdefault(k, 0)
d[k] = tt
k -= l[i-1]
tt = (2 * tt - a) % mo
print(tt)
t = int(input())
for ti in range(t):
solve()
``` | output | 1 | 53,309 | 12 | 106,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array of integers b_1, b_2, …, b_n.
An array a_1, a_2, …, a_n of integers is hybrid if for each i (1 ≤ i ≤ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = ∑_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, …, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2 ⋅ 10^5).
The second line of each test case contains n integers b_1, b_2, …, b_n (-10^9 ≤ b_i ≤ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, …, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1]. | instruction | 0 | 53,310 | 12 | 106,620 |
Tags: combinatorics, data structures, dp, sortings
Correct Solution:
```
import sys
#input=sys.stdin.buffer.readline
mod=10**9+7
for t in range(int(input())):
N=int(input())
B=list(map(int,input().split()))
C=[0]*(N+1)
for i in range(N):
C[i+1]=C[i]+B[i]
'''
DP=[0]*(N+1)
DP[0]=1
for i in range(N):
for j in range(i+1):
if C[j]!=C[i+1] or i==N-1:
DP[i+1]+=DP[j]%mod
'''
S=1
D=dict()
D[0]=1
for i in range(N-1):
T=(S+S-D.get(C[i+1],0))%mod
D[C[i+1]]=S
S=T
print(S)
``` | output | 1 | 53,310 | 12 | 106,621 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.