message stringlengths 2 433k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution. | instruction | 0 | 50,561 | 12 | 101,122 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
n = int(input())
elements = list(map(int,input().split()))
if len(set(elements)) == 1:
print("-1")
else:
elements.sort()
for i in elements:
print(i,end=" ")
``` | output | 1 | 50,561 | 12 | 101,123 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution. | instruction | 0 | 50,562 | 12 | 101,124 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
a.sort()
cnt = 0
for i in range(n):
cnt += a[i]
if cnt != sum(a) - cnt:
print(*a)
else:
print(-1)
``` | output | 1 | 50,562 | 12 | 101,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution. | instruction | 0 | 50,563 | 12 | 101,126 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
n= int(input())
a = list(map(int,input().split()))
b = set(a)
if (len(b)==1):
print(-1)
exit(0)
else:
a = sorted(a)
for i in a:
print(i, end=" ")
``` | output | 1 | 50,563 | 12 | 101,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution. | instruction | 0 | 50,564 | 12 | 101,128 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
def main():
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
if a[0] == a[-1]:
print(-1)
else:
for ai in a:
print(ai, end=" ")
if __name__ == "__main__":
main()
``` | output | 1 | 50,564 | 12 | 101,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution. | instruction | 0 | 50,565 | 12 | 101,130 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
a.sort()
#print(a[:n],a[n:])
if(sum(a[:n])==sum(a[n:])):
print(-1)
else:
print(' '.join(str(x) for x in a))
``` | output | 1 | 50,565 | 12 | 101,131 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution. | instruction | 0 | 50,566 | 12 | 101,132 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
n=int(input())
A=list(map(int,input().split()))
A.sort()
if sum(A)==2*sum(A[:n]):
print(-1)
else:
print(*A)
``` | output | 1 | 50,566 | 12 | 101,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution.
Submitted Solution:
```
k=int(input())
z=[*map(int,input().split())]
if len(set(z))==1 :
print(-1)
else:
print(' '.join(str(x) for x in sorted(z)))
``` | instruction | 0 | 50,567 | 12 | 101,134 |
Yes | output | 1 | 50,567 | 12 | 101,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution.
Submitted Solution:
```
n=int(input())
lis=list(map(int,input().split()))
lis.sort()
if sum(lis[:n])==sum(lis[n:]):
print(-1)
else:
for i in lis:
print(i,end=" ")
print()
``` | instruction | 0 | 50,568 | 12 | 101,136 |
Yes | output | 1 | 50,568 | 12 | 101,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution.
Submitted Solution:
```
n=int(input())
mylist = [int(x) for x in input().split()]
mylist.sort()
if(mylist[0]==mylist[len(mylist)-1]):
print('-1')
else:
mylist = [str(x) for x in mylist]
print(' '.join(mylist))
``` | instruction | 0 | 50,569 | 12 | 101,138 |
Yes | output | 1 | 50,569 | 12 | 101,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
a.sort()
A = 0
B = 0
for i in range(0,n,1):
A += a[i]
for j in range(n,2*n,1):
B += a[j]
if A == B:
print(-1)
exit()
else:
for i in range(len(a)):
print(a[i],end=' ')
``` | instruction | 0 | 50,570 | 12 | 101,140 |
Yes | output | 1 | 50,570 | 12 | 101,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution.
Submitted Solution:
```
def func(n, a):
first_half = [i for i in a[:n]]
second_half = [i for i in a[n:]]
if sum(first_half) != sum(second_half):
return a
if len(list(set(a))) == 1:
return -1
else:
return a
n = int(input())
a = [int(i) for i in input().split()]
print(func(n, a))
``` | instruction | 0 | 50,571 | 12 | 101,142 |
No | output | 1 | 50,571 | 12 | 101,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution.
Submitted Solution:
```
a=int(input())
b=list(map(int,input().split()))
k=b[0:a]
g=b[a::]
p=0
for i in range(0,a):
if(k[i]!=g[i]):
t=g[i]
g[i]=k[i]
k[i]=t
p=1
break
if(p==1):
e=k+g
for i in range(0,len(e)):
print(e[i],end=" ")
print()
else:
print(-1)
``` | instruction | 0 | 50,572 | 12 | 101,144 |
No | output | 1 | 50,572 | 12 | 101,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution.
Submitted Solution:
```
from collections import deque
N = int(input())
A = [int(x) for x in input().split()]
if len(set(A))==1:
print(-1)
elif len(A)==2:
print(A[1],A[0])
else:
L1=list(A[1:N+1])
L2=list(A[N+1:])
L2.append(A[0])
L1 = deque(L1)
L2 = deque(L2)
#print(L1)
#print(L2)
cnt = 0
s1 = sum(L1)
s2 = sum(L2)
for i in range(N-1):
s1 = s1 + L2[0] - L1[0]
s2 = s2 + L1[0] - L2[0]
if s1!=s2:
break
else:
x = L1[0]
L2.append(x)
L1.popleft()
L1.append(L2[0])
L2.popleft()
cnt += 1
if cnt == N-1:
print(-1)
else:
for i in range(N):
print(L1[i],end=" ")
for i in range(N):
print(L2[i],end=" ")
print()
#print(L1)
#print(L2)
``` | instruction | 0 | 50,573 | 12 | 101,146 |
No | output | 1 | 50,573 | 12 | 101,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You're given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn't equal to the sum of the last n elements?
Input
The first line contains an integer n (1 β€ n β€ 1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a_1, a_2, β¦, a_{2n} (1 β€ a_i β€ 10^6) β the elements of the array a.
Output
If there's no solution, print "-1" (without quotes). Otherwise, print a single line containing 2n space-separated integers. They must form a reordering of a. You are allowed to not change the order.
Examples
Input
3
1 2 2 1 3 1
Output
2 1 3 1 1 2
Input
1
1 1
Output
-1
Note
In the first example, the first n elements have sum 2+1+3=6 while the last n elements have sum 1+1+2=4. The sums aren't equal.
In the second example, there's no solution.
Submitted Solution:
```
input()
l = [*map(int,input().split())]
l.sort()
if l[0] == l[1]:
print(-1)
else:
print(*l)
``` | instruction | 0 | 50,574 | 12 | 101,148 |
No | output | 1 | 50,574 | 12 | 101,149 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,602 | 12 | 101,204 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
from collections import defaultdict
(n, k)=[int(x) for x in input().split()]
# res =-1
# dp = [1] * (n+1)
# for i in range(n,0,-1):
# if dp[i] >= k:
# res = i
# break
# imod2 = i % 2
# if imod2 == 0:
# dp[i//2] += dp[i]
# else:
# dp[i-1] += dp[i]
#
# print(dp)
# print(res)
# res =-1
# dp = defaultdict(lambda:1)
# for i in range(n,0,-1):
# val = dp[i]
# if val >= k:
# res = i
# break
# imod2 = i % 2
# idx =-1
# if imod2 == 0:
# idx = i//2
# else:
# idx = i-1
# dp[idx] += val
# del dp[i]
# print(dp)
# print(res)
#import snoop
#@snoop
def f(x):
res = 1
if x % 2 == 1:
adj = 1
else:
adj = 2
res = adj
i = 2
while i*x <= n:
if i*x + adj * i - 1 <=n:
res+= i * adj
i*=2
else:
res+= (n- i*x + 1)
break
return res
#import snoop
#@snoop
def find():
lo=1
hi=n//2
re=1
while lo<=hi:
m=(lo+hi)//2;
if f(2*m)>=k:
re=2*m
lo=m+1
else:
hi=m-1
lo=1
hi=n//2 + n % 2
ro=1
while lo<=hi:
m=(lo+hi)//2;
if f(2*m - 1)>=k:
ro=2*m -1
lo=m+1
else:
hi=m-1
return max(re,ro)
print(find())
``` | output | 1 | 50,602 | 12 | 101,205 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,603 | 12 | 101,206 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
n, k = map(int, input().split())
s = bin(n)[2:]
ans = 1
if k == 1:
ans = n
else:
f = len(s)
for d in range(1, f):
rgt = int(s[-d:], 2)
lft = int(s[:-d], 2)
c = 2**d
# print(d, lft, rgt+c, 2*c-1)
if rgt+c >= k:
if rgt+c > k:
ans = max(lft*2, ans)
else:
ans = max(lft, ans)
if 2*c-1 >= k:
if 2*c-1 > k:
ans = max((lft-1)*2, ans)
else:
ans = max(lft-1, ans)
print(ans)
``` | output | 1 | 50,603 | 12 | 101,207 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,604 | 12 | 101,208 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
n,k=map(int,input().split())
j=n
odd=3
even=6
if j%2==0:
n1=1
n2=1
elif j%2!=0:
n1=1
n2=2
preodd=1
preeven=2
if k==n:
print(1)
elif k==1:
print(j)
elif k==2:
if j%2==0:
print(j-2)
else:
print(j-1)
else:
pre=n
j=n//2
while (j >= 1):
if j % 2 == 0:
if pre % 2 == 0:
if (n1 + preodd+1) >= k:
ans = j
break
elif odd >= k:
ans = j - 1
break
elif even >= k and j > 4:
ans = j - 2
break
else:
n1 = (n1 + preodd + 1)
n2 = odd
else:
if (n2 + preodd+1) >= k:
ans = j
break
elif odd >= k:
ans = j - 1
break
elif even >= k and j > 4:
ans = j - 2
break
else:
n1 = (n2 + preodd + 1)
n2 = odd
else:
if pre % 2 != 0:
if (n2 + 1) >= k:
ans = j
break
elif (preeven+n2+2) >= k:
ans = j - 1
break
elif odd >= k and j > 4:
ans = j - 2
break
elif even>=k and j>5:
ans=j-3
break
else:
n1 = (n2 + 1)
n2 = preeven+n2+2
else:
if (n1 + 1) >= k:
ans = j
break
elif (n1+preeven+2) >= k:
ans = j - 1
break
elif odd >= k and j > 5:
ans = j - 2
break
elif even>=k and j>4:
ans=j-3
break
else:
n1 = (n1 + 1)
n2 = n1+preeven+2
preodd = odd
preeven = even
odd = even + 1
even = even + odd + 1
pre = j
j=j//2
print(ans)
``` | output | 1 | 50,604 | 12 | 101,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,605 | 12 | 101,210 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
def check(md, n, k):
s=0
temp, i = md, 1
while temp<=n:
s += min(n-temp+1, i)
temp *= 2
i *= 2
if md%2==0:
temp, i=md+1, 1
while temp<=n:
s += min(n-temp+1, i)
temp *= 2
i *= 2
if s>=k:
return True
return False
n, k = map(int, input().split())
l, r = 0, n // 2 + 1
while r-l>1:
md=(l+r) // 2
if check(2*md, n, k)==True:
l=md
else:
r=md
even=2*l
l, r = 0, n // 2 + 1
while r-l>1:
md=(l+r) // 2
if check(2*md+1, n, k)==True:
l=md
else:
r=md
odd=2*l+1
print(max(even, odd))
``` | output | 1 | 50,605 | 12 | 101,211 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,606 | 12 | 101,212 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
import sys
import math
from collections import defaultdict,Counter,deque
# input=sys.stdin.readline
# def print(x):
# sys.stdout.write(str(x)+"\n")
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")
# sys.stdout=open("CP2/output.txt",'w')
# sys.stdin=open("CP2/input.txt",'r')
def find1(x):
cur=0
for j in range(60):
if pow(2,j)*x+pow(2,j+1)-1<=n:
cur+=pow(2,j+1)
else:
cur+=max(0,n-pow(2,j)*x+1)
return cur
def find2(x):
cur=0
for j in range(60):
if pow(2,j)*x+pow(2,j)-1<=n:
cur+=pow(2,j)
else:
cur+=max(0,n-pow(2,j)*x+1)
return cur
# mod=pow(10,9)+7
n,k=map(int,input().split())
low=1
high=n//2
# even
while low<=high:
mid=(low+high)//2
c=find1(2*mid)
if c>=k:
low=mid+1
else:
high=mid-1
# print(2*mid,c)
ans=2*high
# print()
low=1
high=(n+1)//2
# odd
while low<=high:
mid=(low+high)//2
c=find2(2*mid-1)
if c>=k:
low=mid+1
else:
high=mid-1
# print(2*mid-1,c)
ans=max(ans,2*high-1)
print(ans)
``` | output | 1 | 50,606 | 12 | 101,213 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,607 | 12 | 101,214 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
n,k = [int(x) for x in input().split()]
def DTB(num,x):
if num > 1:
DTB(num // 2,x)
x.append(num % 2)
nn=[]
kk=[]
DTB(n,nn)
DTB(k,kk)
if k==1:
print(n)
elif k==2:
if n==2:
print(1)
else:
if n%2==0:
print(n-2)
else:
print(n-1)
elif k==3:
if n%4==2:
print(n//2-1)
else:
print(n//2)
else:
a = len(kk)-2
m = len(nn)
p = 2**(a+1)-1 + (n%(2**a))
if nn[m-a-1]==1:
p += 2**(a)
if p>=k:
print(int(''.join([str(x) for x in nn[0:(m-a-1)]])+'0', 2))
else:
N = nn[0:m-a-1]
if sum(N)>1:
if k==(2**(a+2)-1):
if sum(nn[m-a-1:m])==a+1:
print(int(''.join([str(x) for x in nn[0:m-a-1]]), 2))
else:
print(int(''.join([str(x) for x in nn[0:m-a-2]])+'0', 2))
else:
N.reverse()
ind = N.index(1)
N[ind]=0
for i in range(ind):
N[i]=1
N.reverse()
N.append(0)
print(int(''.join([str(x) for x in N]), 2))
else:
if m-a-1==1:
print(1)
else:
if k!= (2**(a+2)-1):
print(2**(m-a-1)-2)
else:
print(2**(m-a-2))
``` | output | 1 | 50,607 | 12 | 101,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,608 | 12 | 101,216 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
def search(odd_num):
c = 0
upper = odd_num
down = odd_num
while upper <= N:
c += (upper-down+1)
upper = 2*upper+1
down = 2*down
if down <= N:
c += (N-down+1)
return c
def binary_search(maximum, odd):
l = 0
r = maximum+2
while r-l > 1:
m = (r+l)//2
num = 2*m+1 if odd else 2*m
if odd:
count = search(num)
else:
count = search(num//2) - 1
if count >= K:
l = m
else:
r = m
return 2*l+1 if odd else 2*l
a1 = binary_search((N+1)//2, odd=True)
a2 = binary_search((N+1)//2, odd=False)
print(max(a1, a2))
``` | output | 1 | 50,608 | 12 | 101,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,609 | 12 | 101,218 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
# even
l=0;r=n
while r-l>1:
mid=(l+r)//2
x=mid*2
cnt=0
if x<=n:
cnt+=1
if x+1<=n:
cnt+=1
v=2
while v*x<=n:
if v*x+2*v-1<=n:
cnt+=2*v
v*=2
else:
cnt+=n-v*x+1
break
if cnt>=k and x<=n:
l=mid
else:
r=mid
ans=l*2
l=1;r=n
while r-l>1:
mid=(l+r)//2
x=mid*2-1
cnt=0
if x<=n:
cnt+=1
v=2
while v*x<=n:
if v*x+v-1<=n:
cnt+=v
v*=2
else:
cnt+=n-v*x+1
break
if cnt>=k and x<=n:
l=mid
else:
r=mid
ans=max(ans,2*l-1)
print(ans)
``` | output | 1 | 50,609 | 12 | 101,219 |
Provide tags and a correct Python 2 solution for this coding contest problem.
At first, let's define function f(x) as follows: $$$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} x/2 & \mbox{if } x is even \\\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$$
We can see that if we choose some value v and will apply function f to it, then apply f to f(v), and so on, we'll eventually get 1. Let's write down all values we get in this process in a list and denote this list as path(v). For example, path(1) = [1], path(15) = [15, 14, 7, 6, 3, 2, 1], path(32) = [32, 16, 8, 4, 2, 1].
Let's write all lists path(x) for every x from 1 to n. The question is next: what is the maximum value y such that y is contained in at least k different lists path(x)?
Formally speaking, you need to find maximum y such that \left| \{ x ~|~ 1 β€ x β€ n, y β path(x) \} \right| β₯ k.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 10^{18}).
Output
Print the only integer β the maximum value that is contained in at least k paths.
Examples
Input
11 3
Output
5
Input
11 6
Output
4
Input
20 20
Output
1
Input
14 5
Output
6
Input
1000000 100
Output
31248
Note
In the first example, the answer is 5, since 5 occurs in path(5), path(10) and path(11).
In the second example, the answer is 4, since 4 occurs in path(4), path(5), path(8), path(9), path(10) and path(11).
In the third example n = k, so the answer is 1, since 1 is the only number occuring in all paths for integers from 1 to 20. | instruction | 0 | 50,610 | 12 | 101,220 |
Tags: binary search, combinatorics, dp, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
pr=stdout.write
raw_input = stdin.readline
def ni():
return int(raw_input())
def li():
return list(map(int,raw_input().split()))
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return (map(int,stdin.read().split()))
range = xrange # not for python 3.0+
# main code
def fun(x,n):
ans=0
if x%2:
p=x
c=1
else:
p=x
c=2
while p<=n:
ans+=(min(n,p+c-1)-p+1)
p*=2
c*=2
return ans
n,k=li()
l=1
r=n-(n/2)
odd=1
while l<=r:
mid=(l+r)/2
if fun((2*mid)-1,n)>=k:
odd=(2*mid)-1
l=mid+1
else:
r=mid-1
l=1
r=(n/2)
even=0
while l<=r:
mid=(l+r)/2
if fun(2*mid,n)>=k:
l=mid+1
even=2*mid
else:
r=mid-1
pn(max(odd,even))
``` | output | 1 | 50,610 | 12 | 101,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements. | instruction | 0 | 50,727 | 12 | 101,454 |
Tags: bitmasks, constructive algorithms, dp, math
Correct Solution:
```
# cook your dish here
from collections import defaultdict, OrderedDict, Counter
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
# import numpy as np
from queue import Queue, PriorityQueue
from heapq import *
from statistics import *
from math import sqrt, log10, log2, log, gcd
import fractions
import copy
from copy import deepcopy
import sys
import io
sys.setrecursionlimit(10000)
import math
import os
import bisect
import collections
mod = int(pow(10, 9)) + 7
import random
from random import *
from time import time;
def ncr(n, r, p=mod):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def normalncr(n, r):
r = min(r, n - r)
count = 1;
for i in range(n - r, n + 1):
count *= i;
for i in range(1, r + 1):
count //= i;
return count
inf = float("inf")
adj = defaultdict(set)
visited = defaultdict(int)
def addedge(a, b):
adj[a].add(b)
adj[b].add(a)
def bfs(v):
q = Queue()
q.put(v)
visited[v] = 1
while q.qsize() > 0:
s = q.get_nowait()
print(s)
for i in adj[s]:
if visited[i] == 0:
q.put(i)
visited[i] = 1
def dfs(v, visited):
if visited[v] == 1:
return;
visited[v] = 1
print(v)
for i in adj[v]:
dfs(i, visited)
# a9=pow(10,6)+10
# prime = [True for i in range(a9 + 1)]
# def SieveOfEratosthenes(n):
# p = 2
# while (p * p <= n):
# if (prime[p] == True):
# for i in range(p * p, n + 1, p):
# prime[i] = False
# p += 1
# SieveOfEratosthenes(a9)
# prime_number=[]
# for i in range(2,a9):
# if prime[i]:
# prime_number.append(i)
def reverse_bisect_right(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if x > a[mid]:
hi = mid
else:
lo = mid + 1
return lo
def reverse_bisect_left(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if x >= a[mid]:
hi = mid
else:
lo = mid + 1
return lo
def get_list():
return list(map(int, input().split()))
def get_str_list_in_int():
return [int(i) for i in list(input())]
def get_str_list():
return list(input())
def get_map():
return map(int, input().split())
def input_int():
return int(input())
def matrix(a, b):
return [[0 for i in range(b)] for j in range(a)]
def swap(a, b):
return b, a
def find_gcd(l):
a = l[0]
for i in range(len(l)):
a = gcd(a, l[i])
return a;
def is_prime(n):
sqrta = int(sqrt(n))
for i in range(2, sqrta + 1):
if n % i == 0:
return 0;
return 1;
def prime_factors(n):
while n % 2 == 0:
return [2] + prime_factors(n // 2)
sqrta = int(sqrt(n))
for i in range(3, sqrta + 1, 2):
if n % i == 0:
return [i] + prime_factors(n // i)
return [n]
def p(a):
if type(a) == str:
print(a + "\n")
else:
print(str(a) + "\n")
def ps(a):
if type(a) == str:
print(a)
else:
print(str(a))
def kth_no_not_div_by_n(n, k):
return k + (k - 1) // (n - 1)
def forward_array(l):
n = len(l)
stack = []
forward = [0] * n
for i in range(len(l) - 1, -1, -1):
while len(stack) and l[stack[-1]] < l[i]:
stack.pop()
if len(stack) == 0:
forward[i] = len(l);
else:
forward[i] = stack[-1]
stack.append(i)
return forward;
def backward_array(l):
n = len(l)
stack = []
backward = [0] * n
for i in range(len(l)):
while len(stack) and l[stack[-1]] < l[i]:
stack.pop()
if len(stack) == 0:
backward[i] = -1;
else:
backward[i] = stack[-1]
stack.append(i)
return backward
def char(a):
return chr(a + 97)
def get_length(a):
return 1 + int(log10(a))
def issq(n):
sqrta = int(n ** 0.5)
return sqrta ** 2 == n;
nc = "NO"
yc = "YES"
ns = "No"
ys = "Yes"
# import math as mt
# MAXN=10**7
# spf = [0 for i in range(MAXN)]
# def sieve():
# spf[1] = 1
# for i in range(2, MAXN):
# # marking smallest prime factor
# # for every number to be itself.
# spf[i] = i
#
# # separately marking spf for
# # every even number as 2
# for i in range(4, MAXN, 2):
# spf[i] = 2
#
# for i in range(3, mt.ceil(mt.sqrt(MAXN))):
#
# # checking if i is prime
# if (spf[i] == i):
#
# # marking SPF for all numbers
# # divisible by i
# for j in range(i * i, MAXN, i):
#
# # marking spf[j] if it is
# # not previously marked
# if (spf[j] == j):
# spf[j] = i
# def getFactorization(x):
# ret = list()
# while (x != 1):
# ret.append(spf[x])
# x = x // spf[x]
#
# return ret
# sieve()
# if(os.path.exists('input.txt')):
# sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
# input=stdin.readline
# print=stdout.write
def equal_sum_partition(arr,n):
sum = 0
for i in range(n):
sum += arr[i]
if sum % 2 != 0:
return False
part = [[True for i in range(n + 1)]
for j in range(sum // 2 + 1)]
for i in range(0, n + 1):
part[0][i] = True
for i in range(1, sum // 2 + 1):
part[i][0] = False
for i in range(1, sum // 2 + 1):
for j in range(1, n + 1):
part[i][j] = part[i][j - 1]
if i >= arr[j - 1]:
part[i][j] = (part[i][j] or
part[i - arr[j - 1]][j - 1])
return part[sum // 2][n]
def get_number(arr,n):
for i in range(n):
if arr[i]%2==1:
return i+1
binarray=[list(reversed(bin(i)[2:])) for i in arr]
new_array=[0 for i in range(32)]
for i in binarray:
for j in range(len(i)):
if i[j]=='1':
new_array[j]+=1
for i in range(32):
if new_array[i]==0:
continue
else:
for j in range(len(binarray)):
if binarray[j][i]=='1':
return j+1
for i in range(1):
n=int(input())
arr=get_list()
if equal_sum_partition(arr,n):
print(1)
print(get_number(arr,n))
else:
print(0)
``` | output | 1 | 50,727 | 12 | 101,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements. | instruction | 0 | 50,728 | 12 | 101,456 |
Tags: bitmasks, constructive algorithms, dp, math
Correct Solution:
```
import sys
import math
def solve(N, A):
g = A[0]
for a in A:
g = math.gcd(g, a)
A2 = [a//g for a in A]
# print(A, A2, sum(A2))
s = sum(A2)
if s % 2:
return []
h = s // 2
S = [0] * (h+1)
S[0] = 1
for a in A2:
for i in range(h, a-1, -1):
if not S[i-a]:
continue
S[i] = 1
# print(S)
if not S[-1]:
return []
for i in range(N):
if A2[i] % 2:
return [i+1]
raise ValueError
def main():
input = sys.stdin.readline
n, = map(int, input().split())
A = list(map(int, input().split()))
B = solve(n, A)
print(len(B))
if B:
print(*B)
main()
``` | output | 1 | 50,728 | 12 | 101,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements. | instruction | 0 | 50,729 | 12 | 101,458 |
Tags: bitmasks, constructive algorithms, dp, math
Correct Solution:
```
def zip_sorted(a,b):
# sorted by a
a,b = zip(*sorted(zip(a,b)))
# sorted by b
sorted(zip(a, b), key=lambda x: x[1])
return a,b
def number_to_list(a):
b = []
while a>=1:
c = a%10
a = int(a/10)
b.append(c)
return list(reversed(b))
def str_list_to_int_list(a):
a = list(map(int,a))
return a
def make_1d_array(n,inti= 0):
a = [inti for _ in range(n)]
return a
def make_2d_array(n,m,inti= 0):
a = [[inti for _ in range(m)] for _ in range(n)]
return a
def make_3d_array(n,m,k,inti= 0):
a = [[[inti for _ in range(k)] for _ in range(m)] for _ in range(n)]
return a
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b);
import sys
input = sys.stdin.readline
I = lambda : list(map(int,input().split()))
S = lambda : list(map(str,input()))
# def solve(sum1,dp):
# prev = [[] for i in range(sum1+1)]
# for i in range(n):
# dp = [prev[j] for j in range(sum1+1)]
# for j in range(sum1+1):
'''
#########################
# Optimizations
#
# 1) loop from backwards thus only one loop required
# 2) memeroization of only required index of ans
#
#########################
n, = I()
a = I()
sum1 = sum(a)
if sum1%2==0:
sum1 = sum1//2
dp = [False]*(sum1+1)
dp[0] = True
# ans = [[] for j in range(sum1+1)]
from collections import defaultdict
ans = defaultdict(list)
for i in range(n):
for j in range(sum1,-1,-1):
if j-a[i]>=0:
if dp[j-a[i]]:
if dp[j]:
if len(ans[j])>(len(ans[j-a[i]])+1):
ans[j] = ans[j-a[i]]+[i+1]
else:
ans[j] = ans[j-a[i]]+[i+1]
dp[j] = dp[j] or dp[j-a[i]]
# print(ans)
idx = -1
# print(dp[sum1],ans[sum1])
if dp[sum1]:
min1 = 10**10
for i in ans:
if dp[i] and (i%2!=0 or dp[(2*sum1-i)//2]==False):
if len(ans[i])<min1:
min1 = len(ans[i])
idx = i
print(len(ans[idx]))
print(*ans[idx])
else:
print(0)
else:
print(0)
'''
#########################
# Optimizations
#
# 2) memeroization of only required index of ans
#
#########################
# n, = I()
# a = I()
# sum1 = sum(a)
# if sum1%2==0:
# sum1 = sum1//2
# prev = [False]*(sum1+1)
# prev[0] = True
# # ans = [[] for j in range(sum1+1)]
# from collections import defaultdict
# # ans = defaultdict(list)
# ans = defaultdict(list)
# for i in range(n):
# dp = prev.copy()
# ans1= copy.deepcopy(ans)
# for j in range(sum1+1):
# if j-a[i]>=0:
# if prev[j-a[i]]:
# if dp[j]:
# if len(ans[j])>(len(ans[j-a[i]])+1):
# ans1[j] = ans[j-a[i]]+[i+1]
# else:
# ans1[j] = ans[j-a[i]]+[i+1]
# dp[j] = dp[j] or prev[j-a[i]]
# prev = dp.copy()
# ans = ans1.copy()
# # print(ans)
# idx = -1
# # print(dp[sum1],ans[sum1])
# if dp[sum1]:
# min1 = 10**10
# for i in ans:
# if dp[i] and (i%2!=0 or dp[(2*sum1-i)//2]==False):
# if len(ans[i])<min1:
# min1 = len(ans[i])
# idx = i
# print(len(ans[idx]))
# print(*ans[idx])
# else:
# print(0)
# else:
# print(0)
#########################
# Optimizations
#
# 1) None
#
#########################
# n, = I()
# a = I()
# sum1 = sum(a)
# if sum1%2==0:
# sum1 = sum1//2
# prev = [False]*(sum1+1)
# prev[0] = True
# prev_a = [[] for j in range(sum1+1)]
# # from collections import defaultdict
# for i in range(n):
# dp = prev.copy()
# # dp_a = [prev_a[j] for j in range(len(prev_a))] #PASS
# # dp_a = [prev_a[j].copy() for j in range(len(prev_a))] #TLE
# # dp_a = [[prev_a[j][k] for k in range(len(prev_a[j]))] for j in range(len(prev_a))] #TLE
# # dp_a = [prev_a[j][:] for j in range(len(prev_a))] #TLE
# for j in range(sum1+1):
# if j-a[i]>=0:
# if prev[j-a[i]]:
# if dp[j]:
# if len(dp_a[j])>(len(prev_a[j-a[i]])+1):
# dp_a[j] = prev_a[j-a[i]]+[i+1]
# else:
# dp_a[j] = prev_a[j-a[i]]+[i+1]
# dp[j] = dp[j] or prev[j-a[i]]
# prev = dp.copy()
# # prev_a = [dp_a[j] for j in range(len(dp_a))]
# # prev_a = [dp_a[j].copy() for j in range(len(dp_a))]
# # prev_a = [[dp_a[j][k] for k in range(len(dp_a[j]))] for j in range(len(dp_a))]
# # prev_a = [dp_a[j][:] for j in range(len(dp_a))]
# # print(ans)
# idx = -1
# # print(dp[sum1],ans[sum1])
# if dp[sum1]:
# min1 = 10**10
# for i in range(sum1+1):
# if dp[i] and (i%2!=0 or dp[(2*sum1-i)//2]==False):
# if len(dp_a[i])<min1:
# min1 = len(dp_a[i])
# idx = i
# print(len(dp_a[idx]))
# print(*dp_a[idx])
# else:
# print(0)
# else:
# print(0)
#########################
#
# WA
#
#########################
# n, = I()
# a = I()
# sum1 = sum(a)
# if sum1%2==0:
# sum1 = sum1//2
# dp = [False]*(sum1+1)
# dp[0] = True
# dp_a = [[] for j in range(sum1+1)]
# # from collections import defaultdict
# for i in range(n):
# for j in range(sum1,-1,-1):
# if j-a[i]>=0:
# if dp[j-a[i]]:
# if dp[j]:
# if len(dp_a[j])>(len(dp_a[j-a[i]])+1):
# dp_a[j] = dp_a[j-a[i]]+[i+1]
# else:
# dp_a[j] = dp_a[j-a[i]]+[i+1]
# dp[j] = dp[j] or dp[j-a[i]]
# # print(ans)
# idx = -1
# # print(dp[sum1],ans[sum1])
# if dp[sum1]:
# min1 = 10**10
# for i in range(sum1+1):
# if dp[i] and (i%2!=0 or dp[(2*sum1-i)//2]==False):
# if len(dp_a[i])<min1:
# min1 = len(dp_a[i])
# idx = i
# print(len(dp_a[idx]))
# print(*dp_a[idx])
# else:
# print(0)
# else:
# print(0)
#########################
#
# WA of contest
# after changing it gives TLE
#########################
# import sys
# input = sys.stdin.readline
# I = lambda : list(map(int,input().split()))
# S = lambda : list(map(str,input()))
# n, = I()
# a = I()
# sum1 = sum(a)
# if sum1%2==0:
# sum1 = sum1//2
# prev = [[False,10**10] for i in range(sum1+1)]
# ans = [[] for i in range(sum1+1)]
# prev[0] = [True,0]
# for i in range(n):
# dp = [[prev[j][k] for k in range(len(prev[j]))] for j in range(sum1+1)]
# ans1 = [[ans[j][k] for k in range(len(ans[j]))] for j in range(sum1+1)]
# for j in range(sum1+1):
# if (j-a[i])>=0:
# if prev[j-a[i]][0]:
# if dp[j][0]:
# if (prev[j-a[i]][1]+1)<dp[j][1]:
# ans1[j] = ans[j-a[i]]+[i+1]
# else:
# ans1[j] = ans[j-a[i]]+[i+1]
# dp[j][1] = min(dp[j][1],prev[j-a[i]][1]+1)
# dp[j][0] = True
# prev = [[dp[j][k] for k in range(len(dp[j]))] for j in range(sum1+1)]
# ans = [[ans1[j][k] for k in range(len(ans1[j]))] for j in range(sum1+1)]
# if prev[sum1][0]:
# min1 = prev[sum1][1]
# idx = sum1
# for i in range(sum1+1):
# if prev[i][0] and ((2*sum1-i)%2!=0 or prev[((2*sum1)-i)//2][0]==False):
# if min1>prev[i][1]:
# idx = i
# min1 = min(min1,prev[i][1])
# print(len(ans[idx]))
# print(*ans[idx])
# else:
# print(0)
# else:
# print(0)
#########################
#
# WA of contest
# pass
#########################
# n, = I()
# a = I()
# sum1 = sum(a)
# if sum1%2==0:
# sum1 = sum1//2
# prev = [False for i in range(sum1+1)]
# ans = [[] for i in range(sum1+1)]
# prev[0] = True
# for i in range(n):
# dp = prev.copy()
# ans1 = [ans[j] for j in range(sum1+1)]
# for j in range(sum1+1):
# if (j-a[i])>=0:
# if prev[j-a[i]]:
# if dp[j]:
# if (len(ans[j-a[i]])+1)<len(ans1[j]):
# ans1[j] = ans[j-a[i]]+[i+1]
# else:
# ans1[j] = ans[j-a[i]]+[i+1]
# dp[j] = True
# prev = dp.copy()
# ans = [ans1[j] for j in range(sum1+1)]
# if prev[sum1]:
# min1 = 10**10
# idx = -1
# for i in range(sum1+1):
# if prev[i] and ((2*sum1-i)%2!=0 or prev[((2*sum1)-i)//2]==False):
# if min1>len(ans[i]):
# idx = i
# min1 = min(min1,len(ans[i]))
# print(len(ans[idx]))
# print(*ans[idx])
# else:
# print(0)
# else:
# print(0)
# 1) less loop while copying
# 2) always intilize with -1
# 3)
#########################
#
# WA of contest
# pass
#########################
n, = I()
a = I()
sum1 = sum(a)
if sum1%2==0:
sum1 = sum1//2
prev = [False for i in range(sum1+1)]
prev1 = [0 for i in range(sum1+1)]
ans = [[] for i in range(sum1+1)]
prev[0] = (True,0)
for i in range(n):
dp = prev.copy()
dp1 = prev1.copy()
ans1 = [ans[j] for j in range(sum1+1)]
for j in range(sum1+1):
if (j-a[i])>=0:
if prev[j-a[i]]:
if dp[j]:
if (len(ans[j-a[i]])+1)<len(ans1[j]):
ans1[j] = ans[j-a[i]]+[i+1]
else:
ans1[j] = ans[j-a[i]]+[i+1]
dp[j] = True
prev = dp.copy()
prev1 = dp1.copy()
ans = [ans1[j] for j in range(sum1+1)]
if prev[sum1]:
min1 = 10**10
idx = -1
for i in range(sum1+1):
if prev[i] and ((2*sum1-i)%2!=0 or prev[((2*sum1)-i)//2]==False):
if min1>len(ans[i]):
idx = i
min1 = min(min1,len(ans[i]))
print(len(ans[idx]))
print(*ans[idx])
else:
print(0)
else:
print(0)
``` | output | 1 | 50,729 | 12 | 101,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements. | instruction | 0 | 50,730 | 12 | 101,460 |
Tags: bitmasks, constructive algorithms, dp, math
Correct Solution:
```
def isSubsetSum(arr, n, sm):
subset = [ [False for j in range(sm + 1)] for i in range(3) ]
for i in range(n + 1):
for j in range(sm + 1):
if (j == 0):
subset[i % 2][j] = True
elif (i == 0):
subset[i % 2][j] = False
elif (arr[i - 1] <= j):
subset[i % 2][j] = subset[(i + 1) % 2][j - arr[i - 1]] or subset[(i + 1) % 2][j]
else:
subset[i % 2][j] = subset[(i + 1) % 2][j]
return subset[n % 2][sm]
# 2 4 10 12
def solve(n,arr):
s=sum(arr)
if s%2!=0:
print(0)
return
sn=s//2
if isSubsetSum(arr,n,sn)==False:
print(0)
return
for i in range(n):
if arr[i]%2!=0:
print(1)
print(i+1)
return
while True:
for i in range(n):
arr[i]=arr[i]//2
if arr[i]%2!=0:
print(1)
print(i+1)
return
if __name__=="__main__":
n=int(input())
arr=list(map(int,input().split(" ")))
solve(n,arr)
``` | output | 1 | 50,730 | 12 | 101,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements. | instruction | 0 | 50,731 | 12 | 101,462 |
Tags: bitmasks, constructive algorithms, dp, math
Correct Solution:
```
import functools, math, sys
input = sys.stdin.readline
def knapsack(capacity, utility, weight):
backpack = [[0 for i in range(capacity + 1)] for j in range(2)]
for i in range(len(weight)):
for j in range(capacity + 1):
if weight[i] <= j: # 0 if i odd, 1 if even
backpack[(i + 1) % 2][j] = max(utility[i] + backpack[i % 2][j - weight[i]], backpack[i % 2][j])
else:
backpack[(i + 1) % 2][j] = backpack[i % 2][j]
return backpack[len(weight) % 2][capacity]
n, values = int(input()), [int(i) for i in input().split()]
tot, tar = sum(values), sum(values) // 2
if tot % 2 or knapsack(tar, values, values) != tar:
print(0)
sys.exit()
gcd = functools.reduce(math.gcd, values)
values = [values[i] // gcd for i in range(n)]
for i in range(n): # remove the odd one out
if values[i] % 2:
print(1)
print(i + 1)
sys.exit()
``` | output | 1 | 50,731 | 12 | 101,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements. | instruction | 0 | 50,732 | 12 | 101,464 |
Tags: bitmasks, constructive algorithms, dp, math
Correct Solution:
```
N = int(input())
aa = [int(x) for x in input().split()]
def get_max_pow2_factor(x):
result = 0
cur_a = x
while cur_a > 0 and cur_a % 2 == 0:
cur_a //= 2
result += 1
return result
shared_pow2_factor = min([get_max_pow2_factor(x) for x in aa])
if shared_pow2_factor > 0:
aa = [x // (2**shared_pow2_factor) for x in aa]
odd_a = None
odd_a_index = None
for i in range(len(aa)):
if aa[i] % 2 != 0:
odd_a = aa[i]
odd_a_index = i
break
if sum(aa) % 2 == 1:
print(0)
else:
# Do dp
sum_a = sum(aa)
target = sum_a // 2
aa = sorted(aa, key=lambda x: -x)
dp = set()
have_solution = False
for a in aa:
new_add = set()
for num in dp:
new_add.add(num + a)
new_add.add(a)
dp = dp | new_add
if (target in dp):
have_solution = True
break
if have_solution:
print(1)
print(odd_a_index+1)
else:
print(0)
``` | output | 1 | 50,732 | 12 | 101,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements. | instruction | 0 | 50,733 | 12 | 101,466 |
Tags: bitmasks, constructive algorithms, dp, math
Correct Solution:
```
def possible(nums,target):
reach = {0}
for num in nums:
reach |= {i+num for i in reach if i+num <= target}
if target in reach:
return True
return False
n = int(input())
arr = list(map(int,input().split()))
s = sum(arr)
if s%2==1 or not possible(arr,s//2):
print(0)
else:
print(1)
mi = 100
res = -1
for i in range(n):
A = arr[i] & (-arr[i])
cnt = 0
while A :
cnt += 1
A >>= 1
if cnt < mi:
mi = cnt
res = i+1
print(res)
``` | output | 1 | 50,733 | 12 | 101,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements. | instruction | 0 | 50,734 | 12 | 101,468 |
Tags: bitmasks, constructive algorithms, dp, math
Correct Solution:
```
import math
import sys
import collections
import bisect
import time
import random
from itertools import permutations
def get_ints():return map(int, sys.stdin.readline().strip().split())
def get_list():return list(map(int, sys.stdin.readline().strip().split()))
def get_string():return sys.stdin.readline().strip()
def findPartition(arr, n):
Sum = 0
for i in range(n):
Sum += arr[i]
if (Sum % 2 != 0):
return 0
part = [0] * ((Sum // 2) + 1)
for i in range((Sum // 2) + 1):
part[i] = 0
for i in range(n):
for j in range(Sum // 2, arr[i] - 1, -1):
if (part[j - arr[i]] == 1 or j == arr[i]):
part[j] = 1
return part[Sum // 2]
for t in range(1):
n=int(input())
arr=get_list()
if findPartition(arr, n) == False:
print(0)
continue
p=0
for i in range(n):
if arr[i]%2==1:
print(1)
print(i+1)
p+=1
break
if p>0:
continue
power_of_2=100
pos=0
for i in range(n):
ele=arr[i]
c=0
while ele%2==0:
ele//=2
c+=1
if c<power_of_2:
pos=i+1
power_of_2=c
print(1)
print(pos)
``` | output | 1 | 50,734 | 12 | 101,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
Submitted Solution:
```
from sys import stdin, stdout
from collections import defaultdict as dd
from collections import deque
import math
input = stdin.readline
flush = stdout.flush
def f(set, n, sum):
subset =([[False for i in range(sum + 1)]
for i in range(n + 1)])
for i in range(n + 1):
subset[i][0] = True
for i in range(1, sum + 1):
subset[0][i]= False
for i in range(1, n + 1):
for j in range(1, sum + 1):
if j<set[i-1]:
subset[i][j] = subset[i-1][j]
if j>= set[i-1]:
subset[i][j] = (subset[i-1][j] or subset[i - 1][j-set[i-1]])
return subset[n][sum]
tc = 1
#tc = int(input())
for _ in range(tc):
n = int(input())
a = list(map(int, input().split()))
x = sum(a)
if x % 2== 1:
print(0)
continue
if f(a, len(a), x//2) == 0:
print(0)
continue
x = 2
y = 1
print(1)
b = 0
while (True):
for i in range(n):
if a[i] % x == y:
print(i+1)
b = 1
break
if b == 1:
break
x = x << 1
y = y << 1
``` | instruction | 0 | 50,735 | 12 | 101,470 |
Yes | output | 1 | 50,735 | 12 | 101,471 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
Submitted Solution:
```
#######puzzleVerma#######
import sys
import math
LI=lambda:[int(k) for k in input().split()]
input = lambda: sys.stdin.readline().rstrip()
IN=lambda:int(input())
S=lambda:input()
n=IN()
a=LI()
s=sum(a)
if s%2==1:
print(0)
sys.exit()
s//=2
dp=[[False for i in range((s)+1)] for j in range(n+1)]
for i in range(n+1):
dp[i][0]=True
for i in range(s+1):
dp[0][i]=False
for i in range(1,n+1):
for j in range(1,s+1):
if a[i-1]>j:
dp[i][j]=dp[i-1][j]
else:
dp[i][j]=dp[i-1][j] or dp[i-1][j-a[i-1]]
if dp[n][s]:
flag=1
while flag:
for i in range(n):
if a[i]%2==1:
print(1)
print(i+1)
flag=0
break
else:
a[i]/=2
else:
print(0)
``` | instruction | 0 | 50,736 | 12 | 101,472 |
Yes | output | 1 | 50,736 | 12 | 101,473 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
Submitted Solution:
```
import sys
n = int(input())
a = [int(x) for x in input().split()]
z = sum(a)
if z % 2 == 0:
s = 1
for x in a:
s |= s << x
if s & (1 << (z // 2)):
b = [x & -x for x in a]
i = b.index(min(b))
print(f"1\n{i + 1}")
sys.exit()
print(0)
``` | instruction | 0 | 50,737 | 12 | 101,474 |
Yes | output | 1 | 50,737 | 12 | 101,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
Submitted Solution:
```
from math import gcd
n = int(input())
A = list(map(int,input().split()))
s = sum(A)
M = 2*10**5+1
dp = [0]*(M+5)
dp[0] = 1
g = A[0]
ind = 0
for a in A:
g = gcd(a,g)
for j in range(M)[::-1]:
if j+a <= M:
dp[j+a] |= dp[j]
if s%2 or dp[s//2] == 0:
print(0)
exit()
for i,a in enumerate(A,1):
if (a//g)%2:
print(1)
print(i)
exit()
``` | instruction | 0 | 50,738 | 12 | 101,476 |
Yes | output | 1 | 50,738 | 12 | 101,477 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
Submitted Solution:
```
def solve(arr):
s = sum(arr)
if s & 1:
print(0)
return
half = s // 2
dp = [False] * (half + 1)
dp[0] = True
for a in arr:
if a > half:
print(0)
return
for j in range(half, a - 1, -1):
dp[j] = dp[j] or dp[j - a]
if not dp[-1]:
print(0)
return
found = [None] * 13
for a in arr:
for i in range(13):
if not found[i] and (a >> i) & 1:
found[i] = a
for i in range(13):
if found[i]:
print(1)
print(found[i])
return
# t = int(input())
# for _ in range(t):
# n, k = [int(x) for x in input().split()]
n = int(input())
arr = [int(x) for x in input().split()]
solve(arr)
``` | instruction | 0 | 50,739 | 12 | 101,478 |
No | output | 1 | 50,739 | 12 | 101,479 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
Submitted Solution:
```
def solve(arr):
s = sum(arr)
found = [None] * 13
if s & 1:
print(0)
return
half = s // 2
dp = [False] * (half + 1)
dp[0] = True
for a in arr:
if a > half:
print(0)
return
for j in range(half, a - 1, -1):
dp[j] = dp[j] or dp[j - a]
if not dp[-1]:
print(0)
return
for a in arr:
for i in range(13):
if not found[i] and (a >> i) & 1:
found[i] = a
for i in range(13):
if (s >> i) & 1:
print(0)
return
if found[i]:
print(1)
print(found[i])
return
# t = int(input())
# for _ in range(t):
# n, k = [int(x) for x in input().split()]
n = int(input())
arr = [int(x) for x in input().split()]
solve(arr)
``` | instruction | 0 | 50,740 | 12 | 101,480 |
No | output | 1 | 50,740 | 12 | 101,481 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
Submitted Solution:
```
def solve(arr):
total = sum(arr)
if total & 1: return 0
target = total // 2
f = [False] * (target+1)
f[0] = True
for a in arr:
if a <= target and f[target-a]:
f[-1] = True
break
for x in range(target,a-1,-1):
f[x] |= f[x-a]
if f[-1]:
print(1)
mask = 0
for a in arr: mask |= a
d = 0
while mask >> d == 0: d += 1
for i,a in enumerate(arr):
if a & (1<<d): return i
return 0
input()
arr = list(map(int,input().split()))
print(solve(arr))
``` | instruction | 0 | 50,741 | 12 | 101,482 |
No | output | 1 | 50,741 | 12 | 101,483 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there's no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer n (2 β€ n β€ 100) β the length of the array a.
The second line contains n integers a_1, a_2, β¦, a_{n} (1 β€ a_i β€ 2000) β the elements of the array a.
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
Input
4
6 3 9 12
Output
1
2
Input
2
1 2
Output
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
Submitted Solution:
```
import sys
import os.path
from collections import *
import math
import bisect
if (os.path.exists('input.txt')):
sys.stdin = open("input.txt", "r")
sys.stdout = open("output.txt", "w")
else:
input = sys.stdin.readline
############## Code starts here ##########################
n = int(input())
arr = [int(x) for x in input().split()]
x = sum(arr)
p = 2
while True:
if x % p:
print(0)
break
else:
y = 0
for i in range(n):
if arr[i] % p:
y = i + 1
break
if y:
print(1)
print(i + 1)
break
else:
p *= 2
############## Code ends here ############################
``` | instruction | 0 | 50,742 | 12 | 101,484 |
No | output | 1 | 50,742 | 12 | 101,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times. | instruction | 0 | 51,320 | 12 | 102,640 |
Tags: binary search, sortings
Correct Solution:
```
#15:40
def check(m, z):
i = 0
for j in range(z):
i += poj[j] // m
if i < k:
return False
else:
return True
n, k = map(int, input().split())
s = list(map(int, input().split()))
z = max(s)
poj = [0 for i in range(z)]
rj = []
for i in range(n):
poj[s[i] - 1] += 1
l = 1
r = n
while r > l + 1:
m = (l + r) // 2
if check(m, z) == True:
l = m
else:
r = m
for i in range(z):
for j in range(poj[i] // l):
rj.append(i)
if len(rj) >= k:
break
for i in range(0, k):
print(rj[i] + 1, end = ' ')
``` | output | 1 | 51,320 | 12 | 102,641 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times. | instruction | 0 | 51,321 | 12 | 102,642 |
Tags: binary search, sortings
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
from collections import Counter
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")
def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint1(): return list(map(lambda x: int(x) - 1, input().split()))
def count(copies,countdict,k):
K = 0
for key in countdict:
K += countdict[key]//copies
return K>=k
def main():
###CODE
n, k = getints()
s = Counter(getints())
l, h = 1, 2*(10**5)
while l<h:
mid = (l+h)//2
bk = count(mid,s,k)
if bk:
l = mid
else:
h = mid-1
if l+1 == h:
mid = h
bk = count(mid, s, k)
if bk:
l = mid
break
ans = []
for i in s:
ans.extend((s[i]//l)*[i])
print(*ans[:k])
if __name__ == "__main__":
main()
``` | output | 1 | 51,321 | 12 | 102,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times. | instruction | 0 | 51,322 | 12 | 102,644 |
Tags: binary search, sortings
Correct Solution:
```
MAX = 200 * 1000 + 1
n, k = map(int,input().split())
arr = [int(x) for x in input().split()]
cnts = {}
t = []
def can(cnt):
del t[:]
for key,value in cnts.items():
need = min(int(value / cnt), k - len(t))
for j in range(need):
t.append(key)
return len(t) == k
for item in arr:
if item in cnts:
cnts[item] +=1
else:
cnts[item] = 1
l = 0
r = n
while r - l > 1:
mid = l+r >> 1
if can(mid):
l = mid
else:
r = mid
if not can(r):
can(l)
print(*t)
``` | output | 1 | 51,322 | 12 | 102,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times. | instruction | 0 | 51,323 | 12 | 102,646 |
Tags: binary search, sortings
Correct Solution:
```
# -*- coding: utf-8 -*-
import collections
"""
created by shhuan at 2018/11/16 23:01
"""
N, K = map(int, input().split())
S = [int(x) for x in input().split()]
wc = [(c, w) for w, c in collections.Counter(S).items()]
wc.sort(reverse=True)
lo, hi = 1, wc[0][0]
while lo <= hi:
md = (lo + hi) // 2
count = 0
for c, w in wc:
if c < md:
break
else:
count += c // md
if count >= K:
break
if count >= K:
lo = md + 1
else:
hi = md - 1
lo -= 1
ans = []
for c, w in wc:
if c < lo:
break
else:
ans.extend([w] * (c//lo))
if len(ans) >= K:
break
print(' '.join(map(str, ans[:K])))
``` | output | 1 | 51,323 | 12 | 102,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times. | instruction | 0 | 51,324 | 12 | 102,648 |
Tags: binary search, sortings
Correct Solution:
```
import sys
from math import floor
from itertools import repeat
input = sys.stdin.readline
n, k = map(int, input().split())
a = list(map(int, input().split()))
count = {}
maxcount = 0
for i in range(n):
if a[i] not in count.keys():
count[a[i]] = 0
count[a[i]] += 1
maxcount = max(count[a[i]], maxcount)
flipcount = {}
for key in count.keys():
if count[key] not in flipcount:
flipcount[count[key]] = []
flipcount[count[key]].append(key)
sortedKeys = sorted(flipcount.keys(), reverse = True)
ans = []
for mid in range(maxcount+1, 0, -1):
ans = []
for key in sortedKeys:
timesToAdd = key // mid
for x in flipcount[key]:
ans.extend(repeat(x, timesToAdd))
if len(ans) >= k:
break
print(" ".join([str(x) for x in ans[:k]]))
``` | output | 1 | 51,324 | 12 | 102,649 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times. | instruction | 0 | 51,325 | 12 | 102,650 |
Tags: binary search, sortings
Correct Solution:
```
# coding=utf-8
from heapq import heappop, heappush
n, k = map(int, input().split())
w = {}
for i in [int(i) for i in input().split()]:
w[i] = w.get(i, 0) + 1
heap = []
for i in sorted(w.items(), key=lambda x: -x[1]):
heappush(heap, [-i[1], i[0], 1])
ans = []
for i in range(k):
tmp_ans = heappop(heap)
ans.append(tmp_ans[1])
heappush(heap, [-(abs(w[tmp_ans[1]]) // (tmp_ans[2] + 1)), tmp_ans[1], tmp_ans[2] + 1])
print(*ans)
``` | output | 1 | 51,325 | 12 | 102,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times. | instruction | 0 | 51,326 | 12 | 102,652 |
Tags: binary search, sortings
Correct Solution:
```
'''input
8 4
1 1 1 1 1 1 2 2
'''
from sys import stdin
import math
import heapq
import copy
def sort_by_frequency(n, arr):
count= [0] * ((2 * (10 ** 5 ))+ 1)
for i in range(n):
count[arr[i]] += 1
freq = []
for i in range((len(count))):
if count[i] != 0:
freq.append([-count[i], i])
return freq
# main start
n, k = list(map(int, stdin.readline().split()))
arr = list(map(int, stdin.readline().split()))
if n == k:
print(*arr)
exit()
freq = sort_by_frequency(n, arr)
pq = []
heapq.heapify(pq)
for i in range(len(freq)):
heapq.heappush(pq, freq[i])
for j in range(2, -freq[i][0] + 1):
heapq.heappush(pq, [-(-freq[i][0] // j), freq[i][1]])
for _ in range(k):
print(heapq.heappop(pq)[1], end = ' ')
``` | output | 1 | 51,326 | 12 | 102,653 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times. | instruction | 0 | 51,327 | 12 | 102,654 |
Tags: binary search, sortings
Correct Solution:
```
import sys
input=sys.stdin.readline
def f(z):
ans=0
for i in l:
ans+=(i[0]//z)
if(ans>=k):
return 1
return 0
def bsearch(l,r):
m=(l+r)//2
if(f(m)):
if(f(m+1)==0):
return m
return bsearch(m+1,r)
return bsearch(l,m-1)
l=input().split()
n=int(l[0])
k=int(l[1])
l=input().split()
li=[int(i) for i in l]
hashi=dict()
for i in li:
if i not in hashi:
hashi[i]=0
hashi[i]+=1
l=[]
ok=[]
for i in hashi:
l.append((hashi[i],i))
l.sort()
l.reverse()
z=bsearch(1,10**9)
rem=k
for i in range(len(l)):
maxa=(l[i][0]//z)
if(rem>maxa):
for k in range(maxa):
print(l[i][1],end=" ")
rem-=maxa
else:
for j in range(rem):
print(l[i][1],end=" ")
break
``` | output | 1 | 51,327 | 12 | 102,655 |
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 s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times.
Submitted Solution:
```
def ell(a, b): ###ellenorzo fv
i = 0
for j in range(b):
i += p[j] // a
if i < k:
return False
else:
return True
n, k = map(int, input().split())
s = list(map(int, input().split()))
z = max(s)
p = [0 for i in range(z)]
q = []
for i in range(n):
p[s[i] - 1] += 1
l = 1
r = n
while r > l + 1:
m = (l + r) // 2
if ell(m, z) == True:
l = m
else:
r = m
for i in range(z):
for j in range(p[i] // l):
q.append(i)
if len(q) >= k:
break
for i in range(0, k):
print(q[i] + 1, end = ' ')
``` | instruction | 0 | 51,328 | 12 | 102,656 |
Yes | output | 1 | 51,328 | 12 | 102,657 |
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 s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times.
Submitted Solution:
```
n,k = map(int,input().split())
arr = list(map(int,input().split()))
occ= dict()
for v in arr:
if v not in occ:
occ[v]=0
occ[v]+=1
left,right,ans=1,200000,list()
def canGen():
cnt=0
for value in occ:
occs=occ[value]
cnt+=occs//mid
return cnt>=k
while left<right:
mid=(left+right+1)//2
if canGen():
left=mid
else:
right=mid-1
for value in occ:
occs=occ[value]
ans.extend((occs//right)*[value])
print(*ans[:k])
``` | instruction | 0 | 51,329 | 12 | 102,658 |
Yes | output | 1 | 51,329 | 12 | 102,659 |
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 s consisting of n integers.
You have to find any array t of length k such that you can cut out maximum number of copies of array t from array s.
Cutting out the copy of t means that for each element t_i of array t you have to find t_i in s and remove it from s. If for some t_i you cannot find such element in s, then you cannot cut out one more copy of t. The both arrays can contain duplicate elements.
For example, if s = [1, 2, 3, 2, 4, 3, 1] and k = 3 then one of the possible answers is t = [1, 2, 3]. This array t can be cut out 2 times.
* To cut out the first copy of t you can use the elements [1, \underline{2}, 3, 2, 4, \underline{3}, \underline{1}] (use the highlighted elements). After cutting out the first copy of t the array s can look like [1, 3, 2, 4].
* To cut out the second copy of t you can use the elements [\underline{1}, \underline{3}, \underline{2}, 4]. After cutting out the second copy of t the array s will be [4].
Your task is to find such array t that you can cut out the copy of t from s maximum number of times. If there are multiple answers, you may choose any of them.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5) β the number of elements in s and the desired number of elements in t, respectively.
The second line of the input contains exactly n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2 β
10^5).
Output
Print k integers β the elements of array t such that you can cut out maximum possible number of copies of this array from s. If there are multiple answers, print any of them. The required array t can contain duplicate elements. All the elements of t (t_1, t_2, ..., t_k) should satisfy the following condition: 1 β€ t_i β€ 2 β
10^5.
Examples
Input
7 3
1 2 3 2 4 3 1
Output
1 2 3
Input
10 4
1 3 1 3 10 3 7 7 12 3
Output
7 3 1 3
Input
15 2
1 2 1 1 1 2 1 1 2 1 2 1 1 1 1
Output
1 1
Note
The first example is described in the problem statement.
In the second example the only answer is [7, 3, 1, 3] and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to 2.
In the third example the array t can be cut out 5 times.
Submitted Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
cnt = [0] * (200005)
for x in a:
cnt[x] += 1
def check(x):
ans = 0
for i in range(200005):
ans += cnt[i] // x
return ans >= k
l, r, ans = 1, n, 0
while (l <= r):
m = (l + r) // 2
if (check(m)):
ans = m
l = m + 1
else:
r = m - 1
for i in range(200005):
while (cnt[i] >= ans and k > 0):
print(i, end = ' ')
k -= 1
cnt[i] -= ans
``` | instruction | 0 | 51,330 | 12 | 102,660 |
Yes | output | 1 | 51,330 | 12 | 102,661 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.