message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.
Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.
Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
Input
The first line of the input contains a single integer n (1 β€ n β€ 5000) β then number of Famil Door's friends.
Then follow n lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers ai and bi (1 β€ ai β€ bi β€ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
Output
Print the maximum number of people that may come to Famil Door's party.
Examples
Input
4
M 151 307
F 343 352
F 117 145
M 24 128
Output
2
Input
6
M 128 130
F 128 131
F 131 140
F 131 141
M 131 200
M 140 200
Output
4
Note
In the first sample, friends 3 and 4 can come on any day in range [117, 128].
In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
Submitted Solution:
```
n = int(input())
males = [0]*368
females = [0]*368
for i in range(n):
pol, a, b = input().split()
a = int(a)
b = int(b)
if pol == 'M':
for i in range(a,b+1):
males[i]+=1
else:
for i in range(a,b+1):
females[i]+=1
ans = 0
for i in range(368):
if (males[i] == females[i]) and (males[i]>ans):
ans = males[i]
print(ans*2)
``` | instruction | 0 | 15,508 | 14 | 31,016 |
No | output | 1 | 15,508 | 14 | 31,017 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends. | instruction | 0 | 16,130 | 14 | 32,260 |
Tags: greedy, math, sortings, two pointers
Correct Solution:
```
t = int(input())
for i in range(t):
n, k = list(map(int, input().split()))
s = list(map(int, input().split()))
f = list(map(int, input().split()))
s.sort(reverse=True)#sorted in reverse order
f.sort()
start=0
end=k-1
result=0
for j in f:
if(j==1):
result+=s[start]*2
else:
end+=j-1
result+=s[start]+s[end]
start+=1
print(result)
``` | output | 1 | 16,130 | 14 | 32,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends. | instruction | 0 | 16,131 | 14 | 32,262 |
Tags: greedy, math, sortings, two pointers
Correct Solution:
```
t=int(input())
while t:
t=t-1
n,k=map(int,input().split())
li=list(map(int,input().split()))
li.sort()
frnds_count_li=list(map(int,input().split()))
frnds_count_li.sort()
frnds_li=[]
for i in range(k): #O(k)
frnds_li.append([li.pop()])
frnds_count_li[i]-=1
for ind,count in enumerate(frnds_count_li): #O(n) bcz w1+w2+...=n
x=count
while(x>0):
frnds_li[ind].append(li.pop())
x-=1
sum=0
for i in frnds_li:
sum+=max(i)
sum+=min(i)
print(sum)
``` | output | 1 | 16,131 | 14 | 32,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends. | instruction | 0 | 16,132 | 14 | 32,264 |
Tags: greedy, math, sortings, two pointers
Correct Solution:
```
for _ in range(int(input())):
n,p=map(int,input().split())
ar=list(map(int,input().split()))
sr = list(map(int, input().split()))
ans=0
ar.sort()
sr.sort()
ee=0;k=n-1
for m in range(p):
if(sr[m]!=1):
break
else:
ee+=1
ans+=(2*ar[k])
k-=1
if(ee!=n-1):
sr=sr[ee:]
sr.reverse()
b=0
for j in sr:
ans+=ar[k]
k-=1
ans+=ar[b]
b+=(j-1)
print(ans)
``` | output | 1 | 16,132 | 14 | 32,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends. | instruction | 0 | 16,133 | 14 | 32,266 |
Tags: greedy, math, sortings, two pointers
Correct Solution:
```
for _ in range(int(input())):
n,k=map(int,input().split())
l=list(map(int,input().split()))
w=list(map(int,input().split()))
l.sort(reverse=True)
w.sort(reverse=True)
t=w.count(1)
ans=2*sum(l[:t])
b=n-1
for i in range(k-t):
ans+=l[t]+l[b]
t+=1
b=b-w[i]+1
print(ans)
``` | output | 1 | 16,133 | 14 | 32,267 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends. | instruction | 0 | 16,134 | 14 | 32,268 |
Tags: greedy, math, sortings, two pointers
Correct Solution:
```
def find_happ(lst,w):
happ=[[] for _ in range(len(w))]
w=sorted(w)
lst=sorted(lst,reverse=True)
sm=sum(lst[:len(w)])
i=len(w)
j=0
while(j<len(w)):
f=w[j]-1
#print(f,end=" ")
if(f>0):
sm+=lst[i+f-1]
i+=f
else:
sm+=lst[j]
j+=1
return sm
def main():
for _ in range(int(input())):
n,k=map(int,input().split())
lst=list(map(int,input().split()))
w=list(map(int,input().split()))
print(find_happ(lst,w))
if __name__=="__main__":
main()
``` | output | 1 | 16,134 | 14 | 32,269 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends. | instruction | 0 | 16,135 | 14 | 32,270 |
Tags: greedy, math, sortings, two pointers
Correct Solution:
```
import io, os
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
R = lambda : map(int, input().split())
for _ in range(int(input())) :
n, k = R()
a = list(R())
w = list(R())
w.sort()
a.sort(reverse=True)
now = k
one = 0
ans = sum(a[:k])
for i in w :
if i == 1 :
ans += a[one]
one += 1
else :
ans += a[now + i - 2]
now += i - 1
print(ans)
``` | output | 1 | 16,135 | 14 | 32,271 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends. | instruction | 0 | 16,136 | 14 | 32,272 |
Tags: greedy, math, sortings, two pointers
Correct Solution:
```
t = int(input())
resposta = []
for k in range(t):
n, m = map(str,input().split())
n = int(n)
m = int(m)
a = input().split()
vec_a = []
vec_a2 = []
for i in range(n):
vec_a.append(int(a[i]))
vec_a2.append(int(a[i]))
vec_a = sorted(vec_a)
vec_a2 = sorted(vec_a2) # el que queda revertit
w = input().split()
vec_w = []
vec_w2 = []
for i in range(m):
vec_w.append(int(w[i]))
vec_w2.append(int(w[i]))
vec_w = sorted(vec_w)
vec_w2 = sorted(vec_w2)
for i in range(m):
vec_w2[i] = vec_w[m - 1 - i]
for i in range(n):
vec_a[i] = vec_a2[n - 1 - i]
punt = 0
n1 = vec_w.count(1)
nelse = m - n1
for i in range(n1):
punt += 2 * vec_a[i]
for i in range(n1, n1 + nelse):
punt += vec_a[i]
compt = 0
for i in range(m):
if vec_w2[i] > 1:
punt += vec_a2[compt]
compt += vec_w2[i] - 1
resposta.append(punt)
for i in range(t):
print(resposta[i])
``` | output | 1 | 16,136 | 14 | 32,273 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends. | instruction | 0 | 16,137 | 14 | 32,274 |
Tags: greedy, math, sortings, two pointers
Correct Solution:
```
from sys import stdin, stdout
t=int(stdin.readline())
for _ in range(t):
#n=int(stdin.readline() )
n,k=(map(int, stdin.readline().strip().split()))
a=list(map(int, stdin.readline() .strip().split()))
w=list(map(int, stdin.readline() .strip().split()))
#s=input()
w=sorted(w, reverse=True)
a=sorted(a)
i=0
j=n-1
ans=0
temp=len(w)-1
while(temp>=0 and w[temp]==1):
ans=ans+(a[j]*2)
j-=1
temp-=1
for ii in range(temp+1):
x=w[ii]
ans+=a[j]
ans+=a[i]
j=j-1
i=i+(x-1)
print(ans)
``` | output | 1 | 16,137 | 14 | 32,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
Submitted Solution:
```
for z in range(int(input())):
n,k = list(map(int,input().split()))
arr = list(map(int,input().split()))
ak = list(map(int,input().split()))
arr.sort()
arr = arr[::-1]
lis = [[] for i in range(k)]
ak.sort()
ind = 0
for i in range(k):
lis[i].append(arr[ind])
ind+=1
for i in range(k):
j = 1
while(j<ak[i]):
lis[i].append(arr[ind])
ind+=1
j+=1
ans = 0
# print(lis)
for i in range(k):
ans += lis[i][0]+lis[i][-1]
print(ans)
``` | instruction | 0 | 16,138 | 14 | 32,276 |
Yes | output | 1 | 16,138 | 14 | 32,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
Submitted Solution:
```
t=int(input())
while t>0:
n,k=map(int,input().split())
inp=list(map(int, input().split()))
kinp=list(map(int,input().split()))
end=n-1
start=0
ans=0
inp.sort()
for i in range(k):
if kinp[i]==1:
ans+=(2*inp[end])
end-=1
kinp.sort(reverse=True)
for i in range(k):
if kinp[i]!=1:
ans+=inp[start]
start+=(kinp[i]-1)
ans+=inp[end]
end-=1
print(ans)
t-=1
``` | instruction | 0 | 16,139 | 14 | 32,278 |
Yes | output | 1 | 16,139 | 14 | 32,279 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
Submitted Solution:
```
for test in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
w = list(map(int, input().split()))
a.sort()
w.sort(reverse=True)
s = 0
i = 0
while len(w) > 0 and w[-1] == 1:
s += a.pop() * 2
w.pop()
for ln in w:
s += a.pop()
ln -= 1
s += a[i]
i += ln
print(s)
``` | instruction | 0 | 16,140 | 14 | 32,280 |
Yes | output | 1 | 16,140 | 14 | 32,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
Submitted Solution:
```
for _ in range(int(input())):
n,k=map(int,input().split())
arr=list(map(int,input().split()))
arr.sort()
fri=list(map(int,input().split()))
s1=0
l=[]
c=0
for i in fri:
if i==1:
s1+=1
else:
l.append(i-1)
c+=i-1
l.sort(reverse=True)
ans=0
if s1>0:
for i in range(s1):
ans+=arr[n-i-1]*2
del arr[n-i-1]
d=0
i=0
n=len(arr)
while(d<len(l)):
ans+=arr[i]+arr[n-d-1]
i+=l[d]
d+=1
print(ans)
``` | instruction | 0 | 16,141 | 14 | 32,282 |
Yes | output | 1 | 16,141 | 14 | 32,283 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
Submitted Solution:
```
for _ in range(int(input())):
n,k=map(int,input().split())
l=list(map(int,input().split()))
friends=list(map(int,input().split()))
l.sort()
friends.sort()
a=0
b=len(l)-1
summ=0
for i in friends:
start=i-1
end=1
if(start>0):
summ+=l[a]
a+=start
else:
summ+=l[b]
if(end>0):
summ+=l[b]
b-=1
print(summ)
``` | instruction | 0 | 16,142 | 14 | 32,284 |
No | output | 1 | 16,142 | 14 | 32,285 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
Submitted Solution:
```
t = int(input())
for tc in range(t):
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
w = sorted(list(map(int, input().split())), reverse=True)
p = 0
sol = sum(a[-k:])
for i in range(k):
sol += a[p]
p += w[i]-1
print(sol)
``` | instruction | 0 | 16,143 | 14 | 32,286 |
No | output | 1 | 16,143 | 14 | 32,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
Submitted Solution:
```
import sys
input=sys.stdin.buffer.readline
import os
from math import*
t=int(input())
while t:
t-=1
n,k=map(int,input().split())
arr=list(map(int,input().split()))
arr.sort()
brr=list(map(int,input().split()))
brr.sort()
ans=0
ip=0
fp=n-1
for x in brr:
if x==1:
ans+=arr[fp]+arr[fp]
fp-=1
else:
ans+=arr[fp]+arr[ip]
ip+=x-1
fp-=1
print(ans)
``` | instruction | 0 | 16,144 | 14 | 32,288 |
No | output | 1 | 16,144 | 14 | 32,289 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...
Lee has n integers a_1, a_2, β¦, a_n in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly w_i integers and each integer will be handed over to exactly one friend.
Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.
Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
Next 3t lines contain test cases β one per three lines.
The first line of each test case contains two integers n and k (1 β€ n β€ 2 β
10^5; 1 β€ k β€ n) β the number of integers Lee has and the number of Lee's friends.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (-10^9 β€ a_i β€ 10^9) β the integers Lee has.
The third line contains k integers w_1, w_2, β¦, w_k (1 β€ w_i β€ n; w_1 + w_2 + β¦ + w_k = n) β the number of integers Lee wants to give to each friend.
It's guaranteed that the sum of n over test cases is less than or equal to 2 β
10^5.
Output
For each test case, print a single integer β the maximum sum of happiness Lee can achieve.
Example
Input
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
Output
48
42
8000000000
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17 + 17) and remaining integers to the second friend (his happiness will be 13 + 1).
In the second test case, Lee should give \{10, 10, 11\} to the first friend and to the second friend, so the total happiness will be equal to (11 + 10) + (11 + 10)
In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
Submitted Solution:
```
t = int(input())
from collections import Counter
for _ in range(t):
n,k = map(int,input().split())
arr2 = list(map(int,input().strip().split()))[:n]
arr1 = list(map(int,input().strip().split()))[:k]
arr1.sort();arr2.sort()
ans = 0;i=0
for num in arr1:
if num == 1:
ans+= arr2[-1]*2
arr2.pop()
else:
ans += arr2[-1]+arr2[i]
arr2.pop()
i+= (num-2)
print(ans)
``` | instruction | 0 | 16,145 | 14 | 32,290 |
No | output | 1 | 16,145 | 14 | 32,291 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y β₯ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x β€ bj β€ ai + y.
Input
The first input line contains four integers n, m, x and y (1 β€ n, m β€ 105, 0 β€ x, y β€ 109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 β€ bj β€ 109) in non-decreasing order, separated by single spaces β the sizes of the available vests.
Output
In the first line print a single integer k β the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Examples
Input
5 3 0 0
1 2 3 3 4
1 3 5
Output
2
1 1
3 2
Input
3 3 2 2
1 5 9
3 5 7
Output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | instruction | 0 | 16,226 | 14 | 32,452 |
Tags: binary search, brute force, greedy, two pointers
Correct Solution:
```
# Dress'em in Vests!
datain = True
if datain:
n, m, x, y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
else:
n, m, x, y = 3, 3, 2, 2
a = [1, 5, 9]
b = [3, 5, 7]
a.sort()
b.sort()
ia = 0
ib = 0
equi = []
while (ia < n) and (ib < m):
if (a[ia] - x <= b[ib]) and (b[ib] <= a[ia] + y):
equi.append(str(ia+1) + ' ' + str(ib+1))
ia += 1
ib += 1
else:
if (ia < n) and (ib < m) and (a[ia] - x > b[ib]):
ib += 1
if (ia < n) and (ib < m) and (a[ia] + y < b[ib]):
ia += 1
print(len(equi))
for sol in equi:
print(sol)
###########
# Blue_02 #
###########
# Array
# https://codeforces.com/problemset/problem/224/B
# Books
# https://codeforces.com/problemset/problem/279/B
# George and Round
# https://codeforces.com/problemset/problem/387/B
# Dress'em in Vests!
# https://codeforces.com/problemset/problem/161/A
# Sereja and Dima
# https://codeforces.com/problemset/problem/381/A
# Approximating a Constant Range
# https://codeforces.com/problemset/problem/602/B
``` | output | 1 | 16,226 | 14 | 32,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y β₯ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x β€ bj β€ ai + y.
Input
The first input line contains four integers n, m, x and y (1 β€ n, m β€ 105, 0 β€ x, y β€ 109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 β€ bj β€ 109) in non-decreasing order, separated by single spaces β the sizes of the available vests.
Output
In the first line print a single integer k β the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Examples
Input
5 3 0 0
1 2 3 3 4
1 3 5
Output
2
1 1
3 2
Input
3 3 2 2
1 5 9
3 5 7
Output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | instruction | 0 | 16,227 | 14 | 32,454 |
Tags: binary search, brute force, greedy, two pointers
Correct Solution:
```
if __name__ == "__main__":
n, m, x, y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = 0
j = 0
res = []
while i < n:
while j < m and a[i] - x > b[j]:
j += 1
if j < m and a[i] - x <= b[j] and a[i] + y >= b[j]:
res.append((i + 1, j + 1))
j += 1
i += 1
print(len(res))
for first, second in res:
print(first, second)
``` | output | 1 | 16,227 | 14 | 32,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y β₯ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x β€ bj β€ ai + y.
Input
The first input line contains four integers n, m, x and y (1 β€ n, m β€ 105, 0 β€ x, y β€ 109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 β€ bj β€ 109) in non-decreasing order, separated by single spaces β the sizes of the available vests.
Output
In the first line print a single integer k β the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Examples
Input
5 3 0 0
1 2 3 3 4
1 3 5
Output
2
1 1
3 2
Input
3 3 2 2
1 5 9
3 5 7
Output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | instruction | 0 | 16,228 | 14 | 32,456 |
Tags: binary search, brute force, greedy, two pointers
Correct Solution:
```
# http://codeforces.com/problemset/problem/161/A
n, m, x, y = list(map(int, input().split()))
a_s = list(map(int, input().split()))
b_s = list(map(int, input().split()))
count = 0
my_result = []
j = 0
for i in range(n):
while j <= m-1:
if a_s[i] - x <= b_s[j] <= a_s[i] + y:
# we find mapping between i and j => increase i and j
count += 1
my_result.append([i + 1, j + 1])
j += 1
break
elif b_s[j] < a_s[i] - x:
j += 1
# j is lower than i => should increase j
continue
else:
# increase i
break
print(count)
for item in my_result:
print('{} {}'.format(item[0], item[1]))
``` | output | 1 | 16,228 | 14 | 32,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y β₯ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x β€ bj β€ ai + y.
Input
The first input line contains four integers n, m, x and y (1 β€ n, m β€ 105, 0 β€ x, y β€ 109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 β€ bj β€ 109) in non-decreasing order, separated by single spaces β the sizes of the available vests.
Output
In the first line print a single integer k β the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Examples
Input
5 3 0 0
1 2 3 3 4
1 3 5
Output
2
1 1
3 2
Input
3 3 2 2
1 5 9
3 5 7
Output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | instruction | 0 | 16,229 | 14 | 32,458 |
Tags: binary search, brute force, greedy, two pointers
Correct Solution:
```
def inp():
return map(int, input().split())
n, m, x, y = inp()
a = list(inp())
b = list(inp())
V = []
j = 0
for i in range(m):
while j < n and a[j] + y < b[i]:
j += 1
if j == n:
break
if a[j] - x <= b[i] <= a[j] + y:
V += [[j + 1, i + 1]]
j += 1
print(len(V))
for x in V:
print(x[0], x[1])
``` | output | 1 | 16,229 | 14 | 32,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y β₯ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x β€ bj β€ ai + y.
Input
The first input line contains four integers n, m, x and y (1 β€ n, m β€ 105, 0 β€ x, y β€ 109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 β€ bj β€ 109) in non-decreasing order, separated by single spaces β the sizes of the available vests.
Output
In the first line print a single integer k β the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Examples
Input
5 3 0 0
1 2 3 3 4
1 3 5
Output
2
1 1
3 2
Input
3 3 2 2
1 5 9
3 5 7
Output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | instruction | 0 | 16,230 | 14 | 32,460 |
Tags: binary search, brute force, greedy, two pointers
Correct Solution:
```
#### 161A-sua bai >> da ap dung two pointers nhung van bi time limit tai test 11
# n: num of solder
# m: num of vest
# x: margin duoi
# y: margin tren
n,m,x,y = map(int,input().split())
a = list(map(int,input().split())) # list size soldier
b = list(map(int,input().split())) # list cac size ao
j=0
u = []
for i in range (n):
if (j==m):
break
# print (b[j])
# print (a[i]-x)
while ((b[j]<(a[i]-x))):
# print ('i=',i)
# print ('j=',j)
j+=1
if (j==m):
break
if (j==m):
break
if (b[j]<=(a[i]+y)):
u.append([i+1,j+1])
j+=1
print (len(u))
for i in range (len(u)):
print ('%d %d'%(u[i][0],u[i][1]))
``` | output | 1 | 16,230 | 14 | 32,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y β₯ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x β€ bj β€ ai + y.
Input
The first input line contains four integers n, m, x and y (1 β€ n, m β€ 105, 0 β€ x, y β€ 109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 β€ bj β€ 109) in non-decreasing order, separated by single spaces β the sizes of the available vests.
Output
In the first line print a single integer k β the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Examples
Input
5 3 0 0
1 2 3 3 4
1 3 5
Output
2
1 1
3 2
Input
3 3 2 2
1 5 9
3 5 7
Output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | instruction | 0 | 16,231 | 14 | 32,462 |
Tags: binary search, brute force, greedy, two pointers
Correct Solution:
```
n,m,x,y = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
j = cnt = 0
l = []
r = []
for i in range(n):
while j < m and b[j] < a[i] - x:
j += 1
if j == m:
break
if a[i] - x <= b[j] <= a[i] + y:
l.append(i+1)
r.append(j+1)
cnt += 1
j += 1
print(cnt)
for i in range(cnt):
print('{} {}'.format(l[i],r[i]))
``` | output | 1 | 16,231 | 14 | 32,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y β₯ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x β€ bj β€ ai + y.
Input
The first input line contains four integers n, m, x and y (1 β€ n, m β€ 105, 0 β€ x, y β€ 109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 β€ bj β€ 109) in non-decreasing order, separated by single spaces β the sizes of the available vests.
Output
In the first line print a single integer k β the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Examples
Input
5 3 0 0
1 2 3 3 4
1 3 5
Output
2
1 1
3 2
Input
3 3 2 2
1 5 9
3 5 7
Output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | instruction | 0 | 16,232 | 14 | 32,464 |
Tags: binary search, brute force, greedy, two pointers
Correct Solution:
```
n, m, x, y = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = []
j = 0
for i in range(n):
while j < m and b[j] <= a[i] +y:
if a[i] - x <= b[j]:
j += 1
c += [(i+1, j)]
break
j += 1
print(len(c))
for i, j in c:
print(i, j)
``` | output | 1 | 16,232 | 14 | 32,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the i-th soldier indicated size ai. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from ai - x to ai + y, inclusive (numbers x, y β₯ 0 are specified).
The Two-dimensional kingdom has m vests at its disposal, the j-th vest's size equals bj. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The i-th soldier can put on the j-th vest, if ai - x β€ bj β€ ai + y.
Input
The first input line contains four integers n, m, x and y (1 β€ n, m β€ 105, 0 β€ x, y β€ 109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains m integers b1, b2, ..., bm (1 β€ bj β€ 109) in non-decreasing order, separated by single spaces β the sizes of the available vests.
Output
In the first line print a single integer k β the maximum number of soldiers equipped with bulletproof vests.
In the next k lines print k pairs, one pair per line, as "ui vi" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them.
Examples
Input
5 3 0 0
1 2 3 3 4
1 3 5
Output
2
1 1
3 2
Input
3 3 2 2
1 5 9
3 5 7
Output
3
1 1
2 2
3 3
Note
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | instruction | 0 | 16,233 | 14 | 32,466 |
Tags: binary search, brute force, greedy, two pointers
Correct Solution:
```
n, m, x, y = map(int, input().split())
sizes = list(map(int, input().split()))
vests = list(map(int, input().split()))
answers = []
i = 0
j = 0
while i < n and j < m:
if sizes[i] - x <= vests[j] <= sizes[i] + y:
answers.append((i, j))
i += 1
j += 1
elif vests[j] < sizes[i] - x:
j += 1
else:
i += 1
print(len(answers))
for answer in answers:
print(answer[0] + 1, answer[1] + 1)
``` | output | 1 | 16,233 | 14 | 32,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 β€ n β€ 100) β the number of USB flash drives.
The second line contains positive integer m (1 β€ m β€ 105) β the size of Sean's file.
Each of the next n lines contains positive integer ai (1 β€ ai β€ 1000) β the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Examples
Input
3
5
2
1
3
Output
2
Input
3
6
2
3
2
Output
3
Input
2
5
5
10
Output
1
Note
In the first example Sean needs only two USB flash drives β the first and the third.
In the second example Sean needs all three USB flash drives.
In the third example Sean needs only one USB flash drive and he can use any available USB flash drive β the first or the second. | instruction | 0 | 16,408 | 14 | 32,816 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n = int(input())
m = int(input())
a = [0] * n
for i in range(n):
a[i] = int(input())
a = sorted(a, reverse = True)
solution = 0
for i in a:
if m > 0:
m -= i
solution += 1
else:
break
print(solution)
``` | output | 1 | 16,408 | 14 | 32,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 β€ n β€ 100) β the number of USB flash drives.
The second line contains positive integer m (1 β€ m β€ 105) β the size of Sean's file.
Each of the next n lines contains positive integer ai (1 β€ ai β€ 1000) β the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Examples
Input
3
5
2
1
3
Output
2
Input
3
6
2
3
2
Output
3
Input
2
5
5
10
Output
1
Note
In the first example Sean needs only two USB flash drives β the first and the third.
In the second example Sean needs all three USB flash drives.
In the third example Sean needs only one USB flash drive and he can use any available USB flash drive β the first or the second. | instruction | 0 | 16,409 | 14 | 32,818 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n=int(input())
m=int(input())
a=list((int(input())) for i in range(n))
a.sort()
s=0
flag=0
while s<m:
if (m-s)>=a[-1]:
s+=a[-1]
a.remove(a[-1])
flag+=1
elif (m-s)<a[-1]:
flag+=1
break
print(flag)
``` | output | 1 | 16,409 | 14 | 32,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 β€ n β€ 100) β the number of USB flash drives.
The second line contains positive integer m (1 β€ m β€ 105) β the size of Sean's file.
Each of the next n lines contains positive integer ai (1 β€ ai β€ 1000) β the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Examples
Input
3
5
2
1
3
Output
2
Input
3
6
2
3
2
Output
3
Input
2
5
5
10
Output
1
Note
In the first example Sean needs only two USB flash drives β the first and the third.
In the second example Sean needs all three USB flash drives.
In the third example Sean needs only one USB flash drive and he can use any available USB flash drive β the first or the second. | instruction | 0 | 16,410 | 14 | 32,820 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n=int(input())
m=int(input())
k=sorted([int(input()) for i in range(n)])[::-1]
b=k[0]
s=1
for i in range(1,n+1):
if b>m or b==m:
print(s)
break
else:
b+=k[i]
s+=1
``` | output | 1 | 16,410 | 14 | 32,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 β€ n β€ 100) β the number of USB flash drives.
The second line contains positive integer m (1 β€ m β€ 105) β the size of Sean's file.
Each of the next n lines contains positive integer ai (1 β€ ai β€ 1000) β the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Examples
Input
3
5
2
1
3
Output
2
Input
3
6
2
3
2
Output
3
Input
2
5
5
10
Output
1
Note
In the first example Sean needs only two USB flash drives β the first and the third.
In the second example Sean needs all three USB flash drives.
In the third example Sean needs only one USB flash drive and he can use any available USB flash drive β the first or the second. | instruction | 0 | 16,411 | 14 | 32,822 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n = int(input())
k = int(input())
l = []
for i in range(n):
l.append(int(input()))
l.sort(reverse=True)
i=0
while k>0:
k = k-l[i]
i+=1
print(i)
``` | output | 1 | 16,411 | 14 | 32,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 β€ n β€ 100) β the number of USB flash drives.
The second line contains positive integer m (1 β€ m β€ 105) β the size of Sean's file.
Each of the next n lines contains positive integer ai (1 β€ ai β€ 1000) β the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Examples
Input
3
5
2
1
3
Output
2
Input
3
6
2
3
2
Output
3
Input
2
5
5
10
Output
1
Note
In the first example Sean needs only two USB flash drives β the first and the third.
In the second example Sean needs all three USB flash drives.
In the third example Sean needs only one USB flash drive and he can use any available USB flash drive β the first or the second. | instruction | 0 | 16,412 | 14 | 32,824 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n=int(input())
m=int(input())
x=[]
for i in range(n):
x.append(int(input()))
x=list(reversed(sorted(x)))
s=0
for i in range(len(x)) :
s+=x[i]
if s>=m:
print(i+1)
break
if s<m:
print(-1)
``` | output | 1 | 16,412 | 14 | 32,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 β€ n β€ 100) β the number of USB flash drives.
The second line contains positive integer m (1 β€ m β€ 105) β the size of Sean's file.
Each of the next n lines contains positive integer ai (1 β€ ai β€ 1000) β the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Examples
Input
3
5
2
1
3
Output
2
Input
3
6
2
3
2
Output
3
Input
2
5
5
10
Output
1
Note
In the first example Sean needs only two USB flash drives β the first and the third.
In the second example Sean needs all three USB flash drives.
In the third example Sean needs only one USB flash drive and he can use any available USB flash drive β the first or the second. | instruction | 0 | 16,413 | 14 | 32,826 |
Tags: greedy, implementation, sortings
Correct Solution:
```
# import sys
# sys.stdin=open("input.in",'r')
# sys.stdout=open("outp.out",'w')
n=int(input())
m=int(input())
a=[int(input()) for i in range(n)]
a.sort(reverse=True)
s=0
c=0
for x in a:
s+=x
c+=1
if s>=m:
break
print(c)
``` | output | 1 | 16,413 | 14 | 32,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 β€ n β€ 100) β the number of USB flash drives.
The second line contains positive integer m (1 β€ m β€ 105) β the size of Sean's file.
Each of the next n lines contains positive integer ai (1 β€ ai β€ 1000) β the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Examples
Input
3
5
2
1
3
Output
2
Input
3
6
2
3
2
Output
3
Input
2
5
5
10
Output
1
Note
In the first example Sean needs only two USB flash drives β the first and the third.
In the second example Sean needs all three USB flash drives.
In the third example Sean needs only one USB flash drive and he can use any available USB flash drive β the first or the second. | instruction | 0 | 16,414 | 14 | 32,828 |
Tags: greedy, implementation, sortings
Correct Solution:
```
n=int(input())
m=int(input())
l=[]
while n>0:
l.append(int(input()))
n=n-1
l.sort(reverse=True)
s=0
z=0
for i in l:
if s<m:
s=s+i
z=z+1
if s>m:
break
print(z)
``` | output | 1 | 16,414 | 14 | 32,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes.
Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.
Input
The first line contains positive integer n (1 β€ n β€ 100) β the number of USB flash drives.
The second line contains positive integer m (1 β€ m β€ 105) β the size of Sean's file.
Each of the next n lines contains positive integer ai (1 β€ ai β€ 1000) β the sizes of USB flash drives in megabytes.
It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.
Output
Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.
Examples
Input
3
5
2
1
3
Output
2
Input
3
6
2
3
2
Output
3
Input
2
5
5
10
Output
1
Note
In the first example Sean needs only two USB flash drives β the first and the third.
In the second example Sean needs all three USB flash drives.
In the third example Sean needs only one USB flash drive and he can use any available USB flash drive β the first or the second. | instruction | 0 | 16,415 | 14 | 32,830 |
Tags: greedy, implementation, sortings
Correct Solution:
```
# import sys
# sys.stdin=open('input.in','r')
# sys.stdout=open('output.out','w')
n=int(input())
size=int(input())
l=[]
s=count=0
for x in range(n):
l.append(int(input()))
l.sort()
for x in range(len(l)-1,-1,-1):
s=s+l[x]
count+=1
if s>=size:
print(count)
break
``` | output | 1 | 16,415 | 14 | 32,831 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints. | instruction | 0 | 16,486 | 14 | 32,972 |
Tags: constructive algorithms
Correct Solution:
```
from itertools import permutations
def read():
return [int(v) for v in input().split()]
def main():
n = read()[0]
a = read()
b = read()
s = set(range(1, n+1))
c = 0
indexes = []
for i, (a1, b1) in enumerate(zip(a, b)):
if a1 == b1:
s.remove(a1)
else:
indexes.append(i)
if len(indexes) == 1:
a[indexes[0]] = s.pop()
else:
for p in permutations(s):
cnt1 = 0
cnt2 = 0
for i, v in enumerate(p):
if a[indexes[i]] != v:
cnt1 += 1
if b[indexes[i]] != v:
cnt2 += 1
if cnt1 == cnt2 == 1:
for i, v in enumerate(p):
a[indexes[i]] = v
for i in range(n):
print(a[i], end=' ')
if __name__ == '__main__':
main()
``` | output | 1 | 16,486 | 14 | 32,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints. | instruction | 0 | 16,487 | 14 | 32,974 |
Tags: constructive algorithms
Correct Solution:
```
def test(ans,a,b):
ca=0
cb=0
for i in range(len(ans)):
if ans[i]!=a[i]:
ca+=1
if ans[i]!=b[i]:
cb+=1
if ca==1 and cb==1:
return True
return False
n=int(input())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
ans=[0]*n
t=[]
for i in range(n):
if a[i]==b[i]:
ans[i]=b[i]
else:
t.append(i)
l=set(list(range(1,n+1)))
k=set(ans)
k=list(l-k)
while 0 in k:
k.remove(0)
if len(t)==1:
ans[t[0]]=k[0]
print(*ans)
elif len(t)==2:
ans[t[0]]=k[0]
ans[t[1]]=k[1]
if test(ans,a,b):
print(*ans)
else:
ans[t[0]]=k[1]
ans[t[1]]=k[0]
print(*ans)
``` | output | 1 | 16,487 | 14 | 32,975 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints. | instruction | 0 | 16,488 | 14 | 32,976 |
Tags: constructive algorithms
Correct Solution:
```
n, a, b = int(input()), list(map(int, input().split())), list(map(int, input().split()))
c = [1] + [0] * n
for ai in a:
c[ai] += 1
d, e = c.index(0), c.index(2)
ei1 = a.index(e)
ei2 = a.index(e, ei1 + 1)
a[ei1 if b[ei1] == d or b[ei2] == e else ei2] = d
print(' '.join(map(str, a)))
``` | output | 1 | 16,488 | 14 | 32,977 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints. | instruction | 0 | 16,489 | 14 | 32,978 |
Tags: constructive algorithms
Correct Solution:
```
n=int(input())
l = input()
l=[int(i) for i in l.split()]
x= input()
x=[int(i) for i in x.split()]
t=0
a=set(range(1,n+1)) - set(x)
for i in range(n):
if l[i]==x[i]:
t+=1
if t==n-1:
s=""
for i in range(n):
if l[i]==x[i]:
s=s+str(l[i])+" "
else:
s+=str(list(a)[0]) + " "
print (s)
else:
t1,t2,s,s2=1,0," "," "
f = 0
f2=0
n1,n2 = 1,0
for i in range(n):
if l[i]==x[i]:
if " "+str(l[i])+" " in s:
f=1
if " "+str(l[i])+" " in s2:
f2=1
if f==0:
s=s+str(l[i])+" "
if f2==0:
s2=s2+str(l[i])+" "
else:
if t2==1:
if " "+str(x[i])+" " in s:f=1
if t1==1:
if " "+str(l[i])+" " in s:f=1
if n1==1:
if " "+str(x[i])+" " in s2:f2=1
if n2==1:
if " "+str(l[i])+" " in s2:f2=1
if t1==1 and f==0:
s=s+str(l[i])+" "
t1=0
t2=1
elif t2==1 and f==0:
s=s+str(x[i])+" "
t2=0
if n1==1 and f2==0:
s2=s2+str(x[i])+" "
n1=0
n2=1
elif n2==1 and f2==0:
s2=s2+str(l[i])+" "
n2=0
if f==0:
print(s[1:])
elif f2==0:
print(s2[1:])
``` | output | 1 | 16,489 | 14 | 32,979 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints. | instruction | 0 | 16,490 | 14 | 32,980 |
Tags: constructive algorithms
Correct Solution:
```
import sys
# sys.setrecursionlimit(300000)
# Get out of main functoin
def main():
pass
# decimal to binary
def binary(n):
return (bin(n).replace("0b", ""))
# binary to decimal
def decimal(s):
return (int(s, 2))
# power of a number base 2
def pow2(n):
p = 0
while n > 1:
n //= 2
p += 1
return (p)
# if number is prime in βn time
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
# list to string ,no spaces
def lts(l):
s = ''.join(map(str, l))
return s
# String to list
def stl(s):
# for each character in string to list with no spaces -->
l = list(s)
# for space in string -->
# l=list(s.split(" "))
return l
# Returns list of numbers with a particular sum
def sq(a, target, arr=[]):
s = sum(arr)
if (s == target):
return arr
if (s >= target):
return
for i in range(len(a)):
n = a[i]
remaining = a[i + 1:]
ans = sq(remaining, target, arr + [n])
if (ans):
return ans
# Sieve for prime numbers in a range
def SieveOfEratosthenes(n):
cnt = 0
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
for p in range(2, n + 1):
if prime[p]:
cnt += 1
# print(p)
return (cnt)
# for positive integerse only
def nCr(n, r):
f = math.factorial
return f(n) // f(r) // f(n - r)
# 1000000007
mod = int(1e9) + 7
def ssinp(): return sys.stdin.readline().strip()
# s=input()
def iinp(): return int(input())
# n=int(input())
def nninp(): return map(int, sys.stdin.readline().strip().split())
# n, m, a=[int(x) for x in input().split()]
def llinp(): return list(map(int, sys.stdin.readline().strip().split()))
# a=list(map(int,input().split()))
def p(xyz): print(xyz)
def p2(a, b): print(a, b)
import math
# d1.setdefault(key, []).append(value)
# ASCII of A-Z= 65-90
# ASCII of a-z= 97-122
import random
from collections import OrderedDict
from fractions import Fraction
#for __ in range(iinp()):
n=iinp()
a=llinp()
b=llinp()
ans=[0]*n
s1=set()
s2=set()
for i in range(n):
if(a[i]==b[i]):
ans[i]=a[i]
s1.add(a[i])
s2.add(i+1)
s2=s2-s1
if(len(s2)==1):
c=s2.pop()
ans[ans.index(0)]=c
else:
ind1=ind2=-1
for i in range(0,n):
if(ans[i]==0 and ind1==-1):
ind1=i
elif(ans[i]==0):
ind2=i
if(a[ind1] in s2 and b[ind1] in s2):
if(a[ind2] in s2):
ans[ind2]=a[ind2]
s2.remove(a[ind2])
c=s2.pop()
ans[ind1]=c
else:
ans[ind2] = b[ind2]
s2.remove(b[ind2])
c = s2.pop()
ans[ind1] = c
elif(a[ind1] in s2):
ans[ind1] = a[ind1]
s2.remove(a[ind1])
c = s2.pop()
ans[ind2] = c
else:
ans[ind1] = b[ind1]
s2.remove(b[ind1])
c = s2.pop()
ans[ind2] = c
print(*ans)
""" Stuff you should look for
int overflow, array bounds
special cases (n=1?)
do something instead of nothing and stay organized
WRITE STUFF DOWN
DON'T GET STUCK ON ONE APPROACH """
``` | output | 1 | 16,490 | 14 | 32,981 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints. | instruction | 0 | 16,491 | 14 | 32,982 |
Tags: constructive algorithms
Correct Solution:
```
I = lambda: map(int, input().split())
n = int(input())
a, b = list(I()), list(I())
ch = []
s2 = set(range(1, n + 1))
for i in range(n):
if a[i] != b[i]:
ch.append(i)
if len(ch) == 2:
t = list(a)
t[ch[0]] = b[ch[0]]
if sorted(t) != list(range(1,n+1)):
a[ch[1]] = b[ch[1]]
else:
a = list(t)
else:
s = set(a).union(b)
a[ch[0]] = list(s2 - s)[0]
print(*a)
``` | output | 1 | 16,491 | 14 | 32,983 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints. | instruction | 0 | 16,492 | 14 | 32,984 |
Tags: constructive algorithms
Correct Solution:
```
n = int(input())
aa, bb = l = [list(map(int, input().split())) for _ in "ab"]
u, v = ((set(range(1, n + 1)) - set(ab)).pop() for ab in l)
t = [i for i in range(n) if aa[i] != bb[i]]
for i, j in [(i, j) for i in t for j in t]:
a, b, aa[i], bb[j] = aa[i], bb[j], u, v
if all(aa[k] == bb[k] for k in t):
break
aa[i], bb[j] = a, b
print(*aa)
``` | output | 1 | 16,492 | 14 | 32,985 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints. | instruction | 0 | 16,493 | 14 | 32,986 |
Tags: constructive algorithms
Correct Solution:
```
s=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in A]
CC=[int(i) for i in A]
R=[]
X=[]
for k in range(len(A)):
if A[k]!=B[k]:
R.append(k)
if len(R)==2:
CC[R[1]]=B[R[1]]
C[R[0]]=B[R[0]]
for k in range(1,len(A)+1):
if (C.count(k)>1):
C=CC
break
else:
for k in range(1,len(A)+1):
if (A.count(k)==0):
C[R[0]]=k
break
print ((' ').join(map(str,C)))
``` | output | 1 | 16,493 | 14 | 32,987 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def check(ans,b):
pos = 0
n = len(b)
for i in range(0,n):
if(ans[i]!=b[i]):
pos+=1
if(pos>=2):
return False
return True
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
dd ={}
idx ={}
for i in range(1,n+1):
dd[i] = 0
q = -1
r = -1
for i in range(0,n):
dd[a[i]]+=1
if(dd[a[i]]==2):
q = idx[a[i]]
r = i
idx[a[i]] = i
val = -1
for i in range(1,n+1):
if(dd[i]==0):
val = i
break
#print(val)
ansa = a.copy()
ansb = a.copy()
ansa[q] = val
ansb[r] = val
#print(ansa)
#print(ansb)
if(check(ansa,b)):
#print("hj")
for ii in ansa:
print(ii,end=" ")
else:
for ii in ansb:
print(ii,end=" ")
``` | instruction | 0 | 16,494 | 14 | 32,988 |
Yes | output | 1 | 16,494 | 14 | 32,989 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
Submitted Solution:
```
# -*- coding: utf-8 -*-
def reveries(size,seqA,seqB):
answer = [0]*int(size)
ref = set([int(i) for i in range(1,int(size)+1)])
for i in range(int(size)):
if seqA[i] == seqB[i]:
answer[i] = int(seqA[i])
ref.remove(int(seqA[i]))
for i in range(int(size)):
if seqA[i]!=seqB[i]:
if int(seqA[i]) not in ref:
if int(seqB[i]) in ref:
answer[i]=int(seqB[i])
ref.remove(int(seqB[i]))
if int(seqB[i]) not in ref:
if int(seqA[i]) in ref:
answer[i]=int(seqA[i])
ref.remove(int(seqA[i]))
i = 0
for j in ref :
while int (answer[i]) != 0:
i+=1
answer[i] = j
i+=1
return answer
size = input()
seqA= input().strip().split()
seqB = input().strip().split()
set_Union = reveries(size,seqA,seqB)
ans = str(set_Union).replace("[","").replace("]","").replace(",","")
print(ans)
``` | instruction | 0 | 16,495 | 14 | 32,990 |
Yes | output | 1 | 16,495 | 14 | 32,991 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
Submitted Solution:
```
import sys
def main():
n=int(sys.stdin.readline().rstrip())
a=list(map(int,sys.stdin.readline().split()))
b=list(map(int,sys.stdin.readline().split()))
items=set(range(1,n+1))
coords=[]
p=[-1]*n
for i in range(n):
if a[i]==b[i]:
items.remove(a[i])
p[i]=a[i]
else:
coords.append(i)
items=list(items)
if len(coords)==1:
p[coords[0]]=items[0]
else:
adelta=0
bdelta=0
for k,coord in enumerate(coords):
if items[k]!=a[coord]: adelta+=1
if items[k]!=b[coord]: bdelta+=1
p[coord]=items[k]
if not (adelta==1 and bdelta==1):
adelta=0
bdelta=0
for k,coord in enumerate(reversed(coords)):
if items[k]!=a[coord]: adelta+=1
if items[k]!=b[coord]: bdelta+=1
p[coord]=items[k]
sys.stdout.write(' '.join(map(str,p))+'\n')
main()
``` | instruction | 0 | 16,496 | 14 | 32,992 |
Yes | output | 1 | 16,496 | 14 | 32,993 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
Submitted Solution:
```
I = lambda : list(map(int, input().split()))
I()
x = I()
y = I()
z = []
nf = list(range(1, len(x)+1))
indx = []
for i in range(len(x)):
if x[i] == y[i]:
z.append(x[i])
nf.remove(z[i])
else:
z.append(-1)
indx.append(i)
if len(nf) == 1:
z[indx[0]] = nf[0]
else:
z[indx[0]] = nf[0]
z[indx[1]] = nf[1]
if (z[indx[0]] != x[indx[0]] and z[indx[1]] != x[indx[1]]) or (z[indx[0]] != y[indx[0]] and z[indx[1]] != y[indx[1]]):
z[indx[0]] = nf[1]
z[indx[1]] = nf[0]
print(*z)
``` | instruction | 0 | 16,497 | 14 | 32,994 |
Yes | output | 1 | 16,497 | 14 | 32,995 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
Submitted Solution:
```
def parse():
return list(map(int, input().split()))
n = int(input())
a = parse()
b = parse()
ans = [0 for i in range(n)]
err = []
vis = set(range(1,n+1))
for i in range(n):
if a[i] == b[i]:
ans[i] = a[i]
vis.remove(a[i])
else:
err.append(i)
if len(err) == 1:
ans[err[0]] = list(vis)[0]
vis = list(vis)
if len(err) == 2:
if (a[err[0]] == vis[0] and a[err[1]] != vis[1] and b[err[0]] != vis[0] and b[err[0]] == vis[1]):
ans[err[0]] = vis[0]
ans[err[1]] = vis[1]
else:
ans[err[0]] = vis[1]
ans[err[1]] = vis[0]
print(' '.join([str(i) for i in ans]))
``` | instruction | 0 | 16,498 | 14 | 32,996 |
No | output | 1 | 16,498 | 14 | 32,997 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
Submitted Solution:
```
import sys
def main():
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
c = [0]*n
d = {}
x,y = -1,-1
for i in range(n):
if a[i] == b[i]:
c[i] = a[i]
d[a[i]] = True
else:
if x == -1:
x = i
else:
y = i
q,w = -1,-1
for i in range(1,n+1):
if i not in d:
if q == -1:
q = i
else:
w = i
if y != -1:
if (a[x]!=q and a[y] ==w) or (a[x]==q and a[y]!=w):
c[x] = q
c[y] = w
else:
c[x] = w
c[y] = q
else:
c[x] = q
print(" ".join(map(str,c)))
main()
``` | instruction | 0 | 16,499 | 14 | 32,998 |
No | output | 1 | 16,499 | 14 | 32,999 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
Submitted Solution:
```
s=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in A]
R=[]
X=[]
for k in range(len(A)):
if A[k]!=B[k]:
R.append(k)
if len(R)==2:
if A.count(A[R[0]])==1:
R=R
C[R[1]]=B[R[1]]
else:
C[R[0]]=B[R[0]]
else:
for k in range(1,len(A)+1):
if (A.count(k)==0):
C[R[0]]=k
break
print ((' ').join(map(str,C)))
``` | instruction | 0 | 16,500 | 14 | 33,000 |
No | output | 1 | 16,500 | 14 | 33,001 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Well, she almost had it all β each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 β€ i β€ n) such that ai β pi, and exactly one j (1 β€ j β€ n) such that bj β pj.
For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.
Input
The first line of input contains a positive integer n (2 β€ n β€ 1 000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of colours in the first meteor outburst.
The third line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ n) β the sequence of colours in the second meteor outburst. At least one i (1 β€ i β€ n) exists, such that ai β bi holds.
Output
Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists.
Examples
Input
5
1 2 3 4 3
1 2 5 4 5
Output
1 2 5 4 3
Input
5
4 4 2 3 1
5 4 5 3 1
Output
5 4 2 3 1
Input
4
1 1 3 4
1 4 3 4
Output
1 2 3 4
Note
In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.
In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.
Submitted Solution:
```
n = int(input())
if n ==1:
print(1)
elif n == 2:
print(1,2)
else:
l=[]
x=1
for i in range(n):
l.append(x)
x+=1
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
if a.count(a[i]) > 1 and a[i] != b[i]:
for j in range(n):
if l[j] not in a:
a[i] = l[j]
break;
print(*a)
``` | instruction | 0 | 16,501 | 14 | 33,002 |
No | output | 1 | 16,501 | 14 | 33,003 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home.
Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
Input
The first line of input data contains two space-separated integers n and k (1 β€ n β€ 105, 0 β€ k β€ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 β€ ai β€ 109).
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
Output
If the doctor will overall carry out less than k examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers β number of animals in the order in which they stand in the queue.
Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
Examples
Input
3 3
1 2 1
Output
2
Input
4 10
3 3 2 1
Output
-1
Input
7 10
1 3 3 1 2 3 1
Output
6 2 3
Note
In the first sample test:
* Before examination: {1, 2, 3}
* After the first examination: {2, 3}
* After the second examination: {3, 2}
* After the third examination: {2}
In the second sample test:
* Before examination: {1, 2, 3, 4, 5, 6, 7}
* After the first examination: {2, 3, 4, 5, 6, 7}
* After the second examination: {3, 4, 5, 6, 7, 2}
* After the third examination: {4, 5, 6, 7, 2, 3}
* After the fourth examination: {5, 6, 7, 2, 3}
* After the fifth examination: {6, 7, 2, 3, 5}
* After the sixth examination: {7, 2, 3, 5, 6}
* After the seventh examination: {2, 3, 5, 6}
* After the eighth examination: {3, 5, 6, 2}
* After the ninth examination: {5, 6, 2, 3}
* After the tenth examination: {6, 2, 3} | instruction | 0 | 16,502 | 14 | 33,004 |
Tags: binary search, math, sortings
Correct Solution:
```
from bisect import bisect_right
from itertools import accumulate
def rest(k, a, q):
for i in range(k):
a[q[0] - 1] -= 1
if a[q[0] - 1] <= 0:
del q[0]
else:
q = q[1:] + [q[0]]
return q
def rest1(k, a, q):
return q[k:] + list(filter(lambda x: a[x - 1] > 1, q[:k]))
def cycle3(a, k):
r = t = p = i = 0
s = sorted(a)
while t + p <= k:
t += p
p = len(s) - i
r += 1
while i < len(s) and s[i] <= r:
i += 1
return r - 1 # , t
def cycle4(a, k):
n = len(a)
s = sorted(a)
sm = list(accumulate(s))
b = [(sm[i - 1] if i > 0 else 0) + s[i] * (n - i) for i in range(n)]
# print(s)
# print(sm)
# print(b)
r = bisect_right(b, k)
# print(s[r - 1])
if r == 0:
return k // n
else:
return s[r - 1] + (k - b[r - 1]) // (n - r)
def f1(k, a, q):
d = sum(a)
if k > d:
return [-1]
if k == d:
return []
if k < len(q):
return rest1(k, a, q)
r = cycle4(a, k)
# print(r)
s = sorted(a)
w = bisect_right(s, r)
p = sum(s[:w]) + r * (len(a) - w)
a = [max(i - r, 0) for i in a]
q = list(filter(lambda x: a[x - 1] > 0, q))
# print(k - p, a, q)
return rest1(k - p, a, q)
n, k = map(int, input().split())
a = list(map(int, input().split()))
q = list(range(1, n + 1))
print(*f1(k, a, q))
``` | output | 1 | 16,502 | 14 | 33,005 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.