message stringlengths 2 30.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 237 109k | cluster float64 10 10 | __index_level_0__ int64 474 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93. | instruction | 0 | 81,808 | 10 | 163,616 |
Tags: binary search, greedy
Correct Solution:
```
q=int(input())
out=[]
for i in range(q):
n=int(input())
p=[]
cont={}
for x in input().split():
el=int(x)//100
p.append(el)
p.sort(reverse = True)
x,a=[int(f) for f in input().split()]
y,b=[int(f) for f in input().split()]
k=int(input())
if x<y:
y,x=x,y
a,b=b,a
a1,b1=a,b
while a1*b1!=0:
if a1>b1:
a1%=b1
else:
b1%=a1
nok=a*b//(a1+b1)
'''
for t in range(ko):
d+=(p[t]*(x+y))
for t in range(ko,ko+kx):
d+=(p[t]*(x))
for t in range(ko+kx,ko+kx+ky):
d+=(p[t]*y)
cont.append(d)'''
##print(cont)
left=0
right=n
##ans-right
while right-left>1:
m=(right+left)//2
d=0
ko=m//nok
kx=(m//a)-ko
ky=(m//b)-ko
for t in range(ko):
d+=(p[t]*(x+y))
for t in range(ko,ko+kx):
d+=(p[t]*(x))
for t in range(ko+kx,ko+kx+ky):
d+=(p[t]*y)
cont[m]=d
if cont[m]<k:
left=m
else:
right=m
if right in cont.keys():
if cont[right]>=k:
out.append(right)
else:
out.append(-1)
else:
m=right
d=0
ko=m//nok
kx=(m//a)-ko
ky=(m//b)-ko
for t in range(ko):
d+=(p[t]*(x+y))
for t in range(ko,ko+kx):
d+=(p[t]*(x))
for t in range(ko+kx,ko+kx+ky):
d+=(p[t]*y)
if d>=k:
out.append(right)
else:
out.append(-1)
for el in out:
print(el)
``` | output | 1 | 81,808 | 10 | 163,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93. | instruction | 0 | 81,809 | 10 | 163,618 |
Tags: binary search, greedy
Correct Solution:
```
import sys,math,string,bisect
input=sys.stdin.readline
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : map(int,input().split())
I=lambda :int(input())
def lcm(a,b):
return (a*b)//math.gcd(a,b)
q=I()
for _ in range(q):
n=I()
l=L()
x,p1=M()
y,p2=M()
if(x<y):
t=y
t1=p2
y=x
p2=p1
x=t
p1=t1
k=I()
current=min(p1,p2)
l.sort(reverse=True)
#print(*l)
d=[0]
d.append(l[0])
for i in range(1,n):
d.append(d[-1]+l[i])
#print(*d)
flag=True
for i in range(1,n+1):
common=i//lcm(p1,p2)
onlyx=(i//p1)-common
onlyy=(i//p2)-common
#print("comm",common)
#print(onlyx,onlyy)
index=common
cost=(d[index]*(x+y))//100
#print(cost)
cost+=((d[index+onlyx]-d[index])*x)//100
#print(cost,(d[index+onlyx]-d[index]))
index+=onlyx
cost+=((d[index+onlyy]-d[index])*y)//100
#print(cost,(d[index+onlyy]-d[index]))
if(cost>=k):
print(i)
flag=False
break
if(flag):
print(-1)
``` | output | 1 | 81,809 | 10 | 163,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93. | instruction | 0 | 81,810 | 10 | 163,620 |
Tags: binary search, greedy
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
import math
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint1(): return list(map(lambda x: int(x) - 1, input().split()))
def count(len,x,a,y,b,lcm,cs):
# calculate total price
cx = len//a
cy = len//b
cxy = len//lcm
cx-=cxy
cy-=cxy
# return sum([(x+y)*i for i in p[:cxy]]+[x*i for i in p[cxy:cxy+cx]]+[y*i for i in p[cxy+cx:cxy+cx+cy]])
return (cs[cxy]-cs[0])*(x+y)+(cs[cxy+cx]-cs[cxy])*x+(cs[cxy+cx+cy]-cs[cxy+cx])*y
def main():
###CODE
q = getint()
for t in range(q):
n = getint()
p = sorted(getints(),reverse=True)
p = [i//100 for i in p]
cs = [0]*(n+1)
for i in range(n):
cs[i+1] = cs[i]+p[i]
x,a = getints()
y,b = getints()
if x<y:
x,a,y,b = y,b,x,a
k = getint()
lcm = (a*b)//math.gcd(a,b)
low, high = 1, n+1
f = 0
while low<high:
mid = (low+high)//2
if count(mid,x,a,y,b,lcm,cs)>=k:
high = mid
f = 1
else:
low = mid+1
if f == 0:
print(-1)
else:
print(low)
if __name__ == "__main__":
main()
``` | output | 1 | 81,810 | 10 | 163,621 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93. | instruction | 0 | 81,811 | 10 | 163,622 |
Tags: binary search, greedy
Correct Solution:
```
import math
global n, p, x, y, a, b, k
#############################################
def check(m):
lst, res = 0, 0
lcm = (a * b) // math.gcd(a, b)
t1 = m // lcm
for i in range(t1):
if res >= k:
return True
if lst >= m:
return False
res += (p[lst] // 100) * (x + y)
lst += 1
t2 = (m // a) - (m // lcm)
for i in range(t2):
if res >= k:
return True
if lst >= m:
return False
res += (p[lst] // 100) * x
lst += 1
t3 = (m // b) - (m // lcm)
for i in range(t3):
if res >= k:
return True
if lst >= m:
return False
res += (p[lst] // 100) * y
lst += 1
if res >= k:
return True
#################################################
for _ in range(int(input())):
n = int(input())
p = list(map(int, input().split()))
x, a = map(int, input().split())
y, b = map(int, input().split())
k = int(input())
p.sort(reverse=True)
if x < y:
x, a, y, b = y, b, x, a
l, r = 0, n
if not check(n):
print(-1)
continue
while r - l > 1:
m = (r + l) // 2
#print(m)
if not check(m):
l = m
else:
r = m
was = True
print(r)
``` | output | 1 | 81,811 | 10 | 163,623 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93. | instruction | 0 | 81,812 | 10 | 163,624 |
Tags: binary search, greedy
Correct Solution:
```
from math import gcd
q = int(input())
for i in range(q):
n = int(input())
p = sorted(map(int, input().split()), key=int)[::-1]
x, a = map(int, input().split())
y, b = map(int, input().split())
k = int(input())
#preprocessing
c = a*b//gcd(a,b)
if y>x:
x, y = y, x
a, b = b, a
#aux fxn to find whether m tickets suffice
def check(m):
add = 0
hc = m//c
ha = m//a - hc
hb = m//b - hc
#print("aajo")
#print(m,hc,ha,hb)
for j in range(hc):
add += (x+y)*p[j]//100
for j in range(hc, hc + ha):
add += x*p[j]//100
for j in range(hc + ha, hc + ha + hb):
add += y*p[j]//100
return add
#lower bound using check fxn
lo, hi = 1, n
ans = -1
while(lo <= hi):
mid = (lo + hi)//2
val = check(mid)
#print("check")
#print(mid, val)
if val >= k:
ans = mid
hi = mid - 1
#elif val > k:
# ans = mid
# hi = mid - 1
else:
lo = mid + 1
print(ans)
``` | output | 1 | 81,812 | 10 | 163,625 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93. | instruction | 0 | 81,813 | 10 | 163,626 |
Tags: binary search, greedy
Correct Solution:
```
def max_contrib(p, amount, n, both, xlist, ylist, a, b):
collection = j = 0
for i in both:
if i > n:
break
collection += (x+y)*p[j]
j += 1
for i in xlist:
if i > n:
break
collection += x*p[j]
j += 1
for i in ylist:
if i > n:
break
collection += y*p[j]
j += 1
return collection//100 >= amount
q = int(input())
for _ in range(q):
n = int(input())
p = sorted(list(map(int, input().split())), reverse=True)
x, a = list(map(int, input().split()))
y, b = list(map(int, input().split()))
if x < y:
x, a, y, b = y, b, x, a
k = int(input())
both, xlist, ylist = [], [], []
for i in range(1, n+1):
div_x, div_y = (i % a == 0), (i % b == 0)
if div_x and div_y:
both.append(i)
elif div_x:
xlist.append(i)
elif div_y:
ylist.append(i)
l, r = 1, len(p)+1
while l < r:
mid = l + (r-l)//2
if max_contrib(p, k, mid, both, xlist, ylist, a, b):
r = mid
else:
l = mid + 1
result = -1 if l > len(p) else l
print(result)
``` | output | 1 | 81,813 | 10 | 163,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93.
Submitted Solution:
```
import sys
input=sys.stdin.buffer.readline
import math
def count(len, x, a, y, b, lcm, cs):
# calculate total price
cx = len // a
cy = len // b
cxy = len // lcm
cx -= cxy
cy -= cxy
# return sum([(x+y)*i for i in p[:cxy]]+[x*i for i in p[cxy:cxy+cx]]+[y*i for i in p[cxy+cx:cxy+cx+cy]])
return (cs[cxy] - cs[0]) * (x + y) + (cs[cxy + cx] - cs[cxy]) * x + (cs[cxy + cx + cy] - cs[cxy + cx]) * y
def main():
###CODE
q = int(input())
for t in range(q):
n = int(input())
p = sorted(list(map(int, input().split())), reverse=True)
p = [i // 100 for i in p]
cs = [0] * (n + 1)
for i in range(n):
cs[i + 1] = cs[i] + p[i]
x, a = list(map(int, input().split()))
y, b = list(map(int, input().split()))
if x < y:
x, a, y, b = y, b, x, a
k = int(input())
lcm = (a * b) // math.gcd(a, b)
low, high = 1, n + 1
f = 0
while low < high:
mid = (low + high) // 2
if count(mid, x, a, y, b, lcm, cs) >= k:
high = mid
f = 1
else:
low = mid + 1
if f == 0:
print(-1)
else:
print(low)
if __name__ == "__main__":
main()
``` | instruction | 0 | 81,814 | 10 | 163,628 |
Yes | output | 1 | 81,814 | 10 | 163,629 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def gcd(a, b):
while b:
a, b = b, a%b
return a
def lcm(a, b):
return a//gcd(a, b)*b
def judge(m):
ab_cnt = m//L
a_cnt = m//a-ab_cnt
b_cnt = m//b-ab_cnt
per = [x+y]*ab_cnt+[x]*a_cnt+[y]*b_cnt
s = 0
for i in range(len(per)):
s += per[i]*p[i]
return s>=100*k
def binary_search():
l, r = 1, n
while l<=r:
mid = (l+r)//2
if judge(mid):
r = mid-1
else:
l = mid+1
return l
q = int(input())
for _ in range(q):
n = int(input())
p = list(map(int, input().split()))
p.sort(reverse=True)
x, a = map(int, input().split())
y, b = map(int, input().split())
if x<y:
x, y = y, x
a, b = b, a
L = lcm(a, b)
k = int(input())
ans = binary_search()
if ans==n+1:
print(-1)
else:
print(ans)
``` | instruction | 0 | 81,815 | 10 | 163,630 |
Yes | output | 1 | 81,815 | 10 | 163,631 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93.
Submitted Solution:
```
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
q = int(input())
for _ in range(q):
n = int(input())
prices = list(map(int, input().split()))
x, a = map(int, input().split())
y, b = map(int, input().split())
k = int(input())
if x < y:
x, y = y, x
a, b = b, a
prices.sort(reverse=True)
ab_lcm = (a*b) // gcd(a, b)
ans_found = False
m_variants = []
l = 1
r = n
while l <= r:
m = (l+r) // 2
count_xy = m // ab_lcm
count_x = m // a - count_xy
count_y = m // b - count_xy
ans = (x+y) * sum(prices[ : count_xy])
ans += x * sum(prices[count_xy : count_xy + count_x])
ans += y * sum(prices[count_xy + count_x : count_xy + count_x + count_y])
ans /= 100
if ans >= k:
ans_found = True
m_variants.append(m)
r = m-1
else:
l = m+1
if ans_found:
print(m_variants[-1])
else:
print(-1)
``` | instruction | 0 | 81,816 | 10 | 163,632 |
Yes | output | 1 | 81,816 | 10 | 163,633 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93.
Submitted Solution:
```
n = int(input())
def ans(x, y, a, b, k) :
if ch(x, y, a, b, len(v)) < k : return -1
l, r = -1, len(v)
while l + 1 != r :
m = (l+r)//2
if ch(x, y, a, b, m) >= k : r = m
else : l = m
return r
def nok(a, b) :
s, d = a, b
while b : a, b = b, a % b
return (s*d)//a
def ch(x, y, a, b, m) :
ab = m//nok(a, b)
a = m//a - ab
b = m//b - ab
if x > y : s = sum(v[0:ab])*(x+y) + sum(v[ab:ab+a])*x + sum(v[ab+a:ab+a+b])*y
else : s = sum(v[0:ab])*(x+y) + sum(v[ab:ab+b])*y + sum(v[ab+b:ab+a+b])*x
return s
for _ in range(n) :
input()
v = sorted(list(map(lambda x : int(x)//100, input().split())), key=lambda x: -x)
x, a = map(lambda x : int(x), input().split())
y, b = map(lambda x : int(x), input().split())
k = int(input())
print(ans(x, y, a, b, k))
``` | instruction | 0 | 81,817 | 10 | 163,634 |
Yes | output | 1 | 81,817 | 10 | 163,635 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93.
Submitted Solution:
```
from math import gcd
def countDivisibles(A, B, M):
# Add 1 explicitly as A is divisible by M
if (A % M == 0):
return (B // M) - (A // M)+1
# A is not divisible by M
return ((B // M) - (A // M))
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
xx, a = list(map(int, input().split()))
y, b = list(map(int, input().split()))
if a>b:
xx,a,y,b=y,b,xx,a
k = int(input())
l = (a * b) // gcd(a, b)
arr.sort(reverse=True)
s = [0] * n
s[0] = arr[0]
for i in range(1,n):
s[i]=s[i-1]+arr[i]
ind=-1
ans=0
# print(s)
for i in range(1, n + 1):
x = countDivisibles(1, i, l)
x1 = countDivisibles(1, i, b)
x2 = countDivisibles(1, i, a)
# print(x,x1,x2)
# ans=(s[x-1]*(xx+y)+(s[x+(x1-x)-1]-s[x-1])*xx+(s[x+(x1-x)+(x2-x)-1]-s[x+(x1-x)-1])*y)
if x==0 and x1!=0 and x2!=0:
ans=max((s[x1 - 1] ) * xx + (s[x1 + (x2 - x) - 1] - s[x1 - 1]) * y,ans)
ans=max((s[x2 - 1] ) * y + (s[x2 + (x1 - x) - 1] - s[x2 - 1]) * xx,ans)
elif x==0 and x1==0 and x2!=0:
ans = max(
(s[x2- 1]) * xx , ans)
# print(ans,i)
elif x!=0 and x1!=0 and x2!=0:
ans = max(
s[x-1]*(xx+y)+(s[x + (x1 - x) - 1] - s[x - 1]) * y + (s[x + (x1 - x) + (x2 - x) - 1] - s[x + (x1 - x) - 1]) * xx, ans)
ans = max(
s[x-1]*(xx+y)+(s[x + (x2 - x) - 1] - s[x - 1]) * xx + (s[x + (x2 - x) + (x1 - x) - 1] - s[x + (x2 - x) - 1]) * y, ans)
# print(i,ans,s[x + (x2 - x) - 1] - s[x - 1],s[x + (x2 - x) + (x1 - x) - 1] - s[x + (x2 - x) - 1])
if ans>=k*100:
ind=i
break
print(ind)
``` | instruction | 0 | 81,818 | 10 | 163,636 |
No | output | 1 | 81,818 | 10 | 163,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93.
Submitted Solution:
```
def cheaker(mid):
#print(p)
p1,p2,p3=mid//(a*b),mid//a-mid//(a*b),mid//b-mid//(a*b)
c=0
#print(p1,p2,p3)
xx=0
while( c<n):
#print(c)
if p1:
xx+=(x+y)*p[c]
c+=1
p1-=1
elif p2:
xx +=x * p[c]
c += 1
p2 -= 1
elif p3:
xx += y * p[c]
c += 1
p3 -= 1
else:
c+=1
break
if xx>=k:
return True
return False
for _ in range(int(input())):
n=int(input())
p=[int(x )//100 for x in input().split()]
p=sorted(p,reverse=True)
x,a=map(int,input().split())
y,b=map(int,input().split())
if y>x:
a,b=b,a
x,y=y,x
k=int(input())
do=1
up=n
an=-1
while (up>=do):
mid=(do+up)//2
# print(mid,k)
if cheaker(mid):
up=mid-1
an=mid
else:
do=mid+1
print(an)
``` | instruction | 0 | 81,819 | 10 | 163,638 |
No | output | 1 | 81,819 | 10 | 163,639 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93.
Submitted Solution:
```
def gcd(a,b):
if a>b:
return gcd(b,a)
if a == 0:
return b
return gcd(b%a,a)
q = int(input())
for i in range(q):
n = int(input())
lol = list(map(int,input().split()))
lol.sort(reverse = True)
x,a = map(int,input().split())
y,b = map(int,input().split())
if x<y:
x,y = y,x
a,b = b,a
k = int(input())
ans = 0
l,r = 0,n
baaa = a*b//gcd(a,b)
m = n
kol = 0
for i in range(m // baaa):
ans += lol[i] * (x + y) // 100
for j in range(m // baaa, m // a):
ans += lol[j] * x // 100
kol += 1
for j in range(m // baaa+kol, m // b):
ans += lol[j] * y // 100
if k > ans:
print(-1)
else:
l, r = 0, n
while r-l>1:
kol = 0
m = (l+r)//2
ans = 0
for i in range(m//baaa):
ans+=lol[i]*(x+y)//100
for j in range(m//baaa,m//a):
ans+=lol[j]*x//100
kol += 1
for p in range(m//baaa+kol,m//b+kol):
ans+=lol[p]*y//100
if k>ans:
l = m
else:
r = m
print(r)
``` | instruction | 0 | 81,820 | 10 | 163,640 |
No | output | 1 | 81,820 | 10 | 163,641 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something!
You have n tickets to sell. The price of the i-th ticket is p_i. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose:
* The x\% of the price of each the a-th sold ticket (a-th, 2a-th, 3a-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources.
* The y\% of the price of each the b-th sold ticket (b-th, 2b-th, 3b-th and so on) in the order you chose is aimed for pollution abatement.
If the ticket is in both programs then the (x + y) \% are used for environmental activities. Also, it's known that all prices are multiples of 100, so there is no need in any rounding.
For example, if you'd like to sell tickets with prices [400, 100, 300, 200] and the cinema pays 10\% of each 2-nd sold ticket and 20\% of each 3-rd sold ticket, then arranging them in order [100, 200, 300, 400] will lead to contribution equal to 100 β
0 + 200 β
0.1 + 300 β
0.2 + 400 β
0.1 = 120. But arranging them in order [100, 300, 400, 200] will lead to 100 β
0 + 300 β
0.1 + 400 β
0.2 + 200 β
0.1 = 130.
Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least k in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least k.
Input
The first line contains a single integer q (1 β€ q β€ 100) β the number of independent queries. Each query consists of 5 lines.
The first line of each query contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of tickets.
The second line contains n integers p_1, p_2, ..., p_n (100 β€ p_i β€ 10^9, p_i mod 100 = 0) β the corresponding prices of tickets.
The third line contains two integers x and a (1 β€ x β€ 100, x + y β€ 100, 1 β€ a β€ n) β the parameters of the first program.
The fourth line contains two integers y and b (1 β€ y β€ 100, x + y β€ 100, 1 β€ b β€ n) β the parameters of the second program.
The fifth line contains single integer k (1 β€ k β€ 10^{14}) β the required total contribution.
It's guaranteed that the total number of tickets per test doesn't exceed 2 β
10^5.
Output
Print q integers β one per query.
For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least k if you can sell tickets in any order.
If the total contribution can not be achieved selling all the tickets, print -1.
Example
Input
4
1
100
50 1
49 1
100
8
100 200 100 200 100 200 100 100
10 2
15 3
107
3
1000000000 1000000000 1000000000
50 1
50 1
3000000000
5
200 100 100 100 100
69 5
31 2
90
Output
-1
6
3
4
Note
In the first query the total contribution is equal to 50 + 49 = 99 < 100, so it's impossible to gather enough money.
In the second query you can rearrange tickets in a following way: [100, 100, 200, 200, 100, 200, 100, 100] and the total contribution from the first 6 tickets is equal to 100 β
0 + 100 β
0.1 + 200 β
0.15 + 200 β
0.1 + 100 β
0 + 200 β
0.25 = 10 + 30 + 20 + 50 = 110.
In the third query the full price of each ticket goes to the environmental activities.
In the fourth query you can rearrange tickets as [100, 200, 100, 100, 100] and the total contribution from the first 4 tickets is 100 β
0 + 200 β
0.31 + 100 β
0 + 100 β
0.31 = 62 + 31 = 93.
Submitted Solution:
```
def fu(k):
cxy=0
cx=0
cy=0
for i in range(k):
if (i+1)%a==0 and (i+1)%b==0:
cxy+=1
elif (i+1)%a==0:
cx+=1
elif (i+1)%b==0:
cy+=1
s=(sum(p[:cxy])*(x+y))//100
s+=(sum(p[cxy:cxy+cx])*x)//100
s+=(sum(p[cxy+cx:cxy+cx+cy])*y)//100
return s
import sys
input=sys.stdin.readline
for _ in range(int(input())):
n=int(input())
p=list(map(int,input().split()))
x,a=list(map(int,input().split()))
y,b=list(map(int,input().split()))
if x<y:
x,y=y,x
a,b=b,a
k=int(input())
p.sort(reverse=True)
st=0
end=n
e=0
while st<=end:
mid=(st+end)//2
if fu(mid)==k:
ans=mid
e=1
break
elif fu(mid)<k:
st=mid+1
else:
end=mid-1
if st>n:
print(-1)
else:
if e==0:
ans=st
print(ans)
``` | instruction | 0 | 81,821 | 10 | 163,642 |
No | output | 1 | 81,821 | 10 | 163,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap. | instruction | 0 | 81,902 | 10 | 163,804 |
Tags: binary search, constructive algorithms, greedy, sortings
Correct Solution:
```
n=int(input())
a=[int(x) for x in input().split()]
if n<3:
print(0)
print(*a)
else:
a.sort()
b=[-1]*n
b[-1]=a[-1]
j=0
for z in range(1,n-1,2):
b[z]=a[j]
j+=1
ans=j
for z in range(n):
if b[z]==-1:
b[z]=a[j]
j+=1
print(ans)
print(*b)
``` | output | 1 | 81,902 | 10 | 163,805 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap. | instruction | 0 | 81,903 | 10 | 163,806 |
Tags: binary search, constructive algorithms, greedy, sortings
Correct Solution:
```
import sys
def inp():
return sys.stdin.readline().rstrip('\n').encode('utf8')
def mpint():
return map(int, sys.stdin.readline().split(' '))
def itg():
return int(sys.stdin.readline())
# ############################## import
# ############################## main
# for __ in range(itg()):
n = itg()
arr = sorted(mpint())
a1 = arr[n >> 1:] # big
a2 = arr[:n >> 1] # small
ans = []
for i in range(n):
if i & 1: # small
ans.append(a2.pop())
else:
ans.append(a1.pop())
for i in range(n - 2):
if ans[i] == ans[i + 1]:
ans[i + 1], ans[i + 2] = ans[i + 2], ans[i + 1]
ans1 = 0
for i in range(1, n - 1):
if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
ans1 += 1
print(ans1)
print(*ans)
# Please check!
``` | output | 1 | 81,903 | 10 | 163,807 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap. | instruction | 0 | 81,904 | 10 | 163,808 |
Tags: binary search, constructive algorithms, greedy, sortings
Correct Solution:
```
def winner(nums,n):
nums = sorted(nums)
ans = []
i = 0
j = n//2
count = 0
while j<n:
ans.append(nums[j])
if i<(n//2):
ans.append(nums[i])
if j!=n-1 and nums[i]!=nums[j]:
count+=1
j+=1
i+=1
return ans,count
n = int(input())
nums,count = winner([int(i) for i in input().split()],n)
print(count)
for num in nums:
print(num,end=" ")
#t = int(input())
#while t:
# t-=1
# n = int(input())
# num = input()
# print(winner(num,n))
``` | output | 1 | 81,904 | 10 | 163,809 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap. | instruction | 0 | 81,905 | 10 | 163,810 |
Tags: binary search, constructive algorithms, greedy, sortings
Correct Solution:
```
n = int(input())
arr = [int(num) for num in input().split(' ')]
arr.sort()
ans = [-1]*n
if n%2!=0:
i = 1
j = 0
while i<n:
ans[i] = arr[j]
i = i + 2
j = j + 1
i = 2
while i<n:
ans[i] = arr[j]
i = i + 2
j = j + 1
ans[0] = arr[-1]
print(n//2)
for item in ans:
print(item,end=' ')
else:
i = 0
j = 0
while i<n:
ans[i] = arr[j]
i = i + 2
j = j + 1
i = 1
while i<n:
ans[i] = arr[j]
i = i + 2
j = j + 1
print(n//2 -1)
for item in ans:
print(item,end=' ')
``` | output | 1 | 81,905 | 10 | 163,811 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap. | instruction | 0 | 81,906 | 10 | 163,812 |
Tags: binary search, constructive algorithms, greedy, sortings
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split()))
arr.sort()
point=0
temp=[0]*n
curr=1
while curr<n:
temp[curr]=arr[point]
curr+=2
point+=1
for i in range(n):
if temp[i]==0:
temp[i]=arr[point]
point+=1
count=0
for i in range(1,n-1):
if temp[i]<temp[i-1] and temp[i]<temp[i+1]:
count+=1
print(count)
for i in range(n):
print(temp[i],end=" ")
``` | output | 1 | 81,906 | 10 | 163,813 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap. | instruction | 0 | 81,907 | 10 | 163,814 |
Tags: binary search, constructive algorithms, greedy, sortings
Correct Solution:
```
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
ans = [0]*n
if n<3:
print(0)
print(*arr)
else:
for i in range(1,n,2):
ans[i] = arr.pop(0)
for i in range(0,n,2):
ans[i] = arr.pop(0)
cnt = 0
for i in range(1,n-1):
if ans[i]<ans[i-1] and ans[i]<ans[i+1]:
cnt += 1
print(cnt)
print(*ans)
``` | output | 1 | 81,907 | 10 | 163,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap. | instruction | 0 | 81,908 | 10 | 163,816 |
Tags: binary search, constructive algorithms, greedy, sortings
Correct Solution:
```
n = int(input())
l = list(map(int,input().split()))
l.sort()
print((n-1)//2)
if n<3:
print(*l)
else:
arr = [0]*n
j = 0
for i in range(1,n,2):
arr[i] = l[j]
j+=1
for i in range(0,n,2):
arr[i] = l[j]
j+=1
print(*arr)
``` | output | 1 | 81,908 | 10 | 163,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap. | instruction | 0 | 81,909 | 10 | 163,818 |
Tags: binary search, constructive algorithms, greedy, sortings
Correct Solution:
```
"""
pppppppppppppppppppp
ppppp ppppppppppppppppppp
ppppppp ppppppppppppppppppppp
pppppppp pppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppp
ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp
pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp
ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp
pppppppppppppppppppppppp
pppppppppppppppppppppppppppppppp
pppppppppppppppppppppp pppppppp
ppppppppppppppppppppp ppppppp
ppppppppppppppppppp ppppp
pppppppppppppppppppp
"""
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = 10 ** 9 + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+end)
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
n = int(data())
arr = l()
arr.sort()
arr = deque(arr)
answer = []
while arr:
if not answer:
answer.append(arr.pop())
continue
answer.append(arr.popleft())
if arr:
answer.append(arr.pop())
result = 0
for i in range(1, n-1):
if answer[i-1] > answer[i] and answer[i + 1] > answer[i]:
result += 1
out(result)
outa(*answer)
``` | output | 1 | 81,909 | 10 | 163,819 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap.
Submitted Solution:
```
n=int(input())
A=sorted([int(_) for _ in input().split()])
i=0
j=n//2
X=[]
for _ in range(n):
if _%2==0:
X.append(A[j])
j+=1
else:
X.append(A[i])
i+=1
print((n-1)//2)
print(*X)
``` | instruction | 0 | 81,910 | 10 | 163,820 |
Yes | output | 1 | 81,910 | 10 | 163,821 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap.
Submitted Solution:
```
def inp():
return int(input())
def linp():
return list(map(int, input().split()))
def minp():
return map(int, input().split())
n = inp()
res = [0 for i in range(n)]
a = linp()
a.sort()
if n & 1:
res[-1] = a[-1]
i = 0
e = n // 2
t = 0
while i < e:
res[t] = a[e + i]
t += 1
res[t] = a[i]
t += 1
i += 1
i = 1
c = 0
while i <= n - 2:
if res[i - 1] > res[i]:
if res[i + 1] > res[i]:
c += 1
i += 1
print(c)
print(*res)
``` | instruction | 0 | 81,911 | 10 | 163,822 |
Yes | output | 1 | 81,911 | 10 | 163,823 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap.
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
if n < 3:
print(0)
for num in A:
print(num, end=" ")
else:
Max_arr = [0 for i in range(n)]
A = sorted(A)
Max_arr[-1] = A[-1]
Max_arr[-2] = A[-2] if n % 2 == 0 else 0
k = n - 1 if n % 2 != 0 else n - 2
for i in range(k):
if i % 2 == 0:
Max_arr[i] = A[i+1]
else:
Max_arr[i] = A[i-1]
print(n//2 - 1) if n % 2 == 0 else print(n//2)
for num in Max_arr:
print(num, end=" ")
``` | instruction | 0 | 81,912 | 10 | 163,824 |
Yes | output | 1 | 81,912 | 10 | 163,825 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
#<fast I/O>
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
#</fast I/O>
#<I/O>
def scanf(datatype = str):
return datatype(sys.stdin.readline().rstrip())
def printf(answer = ''):
return sys.stdout.write(str(answer) + "\n")
def prints(answer):
return sys.stdout.write(str(answer) + " ")
def map_input(datatype = str):
return map(datatype, sys.stdin.readline().split())
def list_input(datatype = str):
return list(map(datatype, sys.stdin.readline().split()))
def testcase(number: int, solve_function):
for _ in range(number):
solve_function()
# solve_function()
#</I/O>
#<solution>
def solve():
n = scanf(int)
arr = list_input(int)
if(n <= 2):
printf(0)
for i in arr:
prints(i)
return
arr = sorted(arr, reverse = True)
i = 1
while(i < n - 1):
arr[i], arr[i + 1] = arr[i + 1], arr[i]
i += 2
printf(n // 2) if(n & 1) else printf((n - 1) // 2)
for i in arr:
prints(i)
solve()
# t = scanf(int)
# testcase(t,solve)
#</solution>
``` | instruction | 0 | 81,913 | 10 | 163,826 |
Yes | output | 1 | 81,913 | 10 | 163,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap.
Submitted Solution:
```
from collections import Counter, defaultdict, deque
import sys
num_spheres = int(sys.stdin.readline().strip())
spheres = list(map(int, sys.stdin.readline().split()))
sorted_spheres = sorted(spheres)
left, right = 0, num_spheres - 1
optimal_order = [0 for _ in range(num_spheres)]
index_to_fill = 0
while left < right:
optimal_order[index_to_fill] = sorted_spheres[right]
right -= 1
index_to_fill += 1
optimal_order[index_to_fill] = sorted_spheres[left]
index_to_fill += 1
left += 1
if left == right:
optimal_order[index_to_fill] = sorted_spheres[left]
if num_spheres <= 2:
print(0)
for char in spheres:
print(char, end=' ')
else:
num_purchased = 0
for i in range(0, num_spheres-2):
if optimal_order[i] > optimal_order[i+1] and optimal_order[i+2] > optimal_order[i+1]:
num_purchased += 1
print(num_purchased)
for char in optimal_order:
print(char, end=' ')
``` | instruction | 0 | 81,914 | 10 | 163,828 |
No | output | 1 | 81,914 | 10 | 163,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap.
Submitted Solution:
```
n=int(input())
m=list(map(int,input().split()))
for i in range(0,n-1,1):
m[i],m[i+1]=m[i+1],m[i]
print((n-1)//2)
print(*m)
m.sort()
``` | instruction | 0 | 81,915 | 10 | 163,830 |
No | output | 1 | 81,915 | 10 | 163,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap.
Submitted Solution:
```
def main():
number = int(input())
if number<3:
if number==1:
a=int(input())
print(0)
print(a)
elif number==2:
a,b=input().split()
a=int(a)
b=int(b)
print(0)
print(a,b)
else:
initial = list(map(int,input().split()))
maxp=number//2
final = []
print(maxp)
low=initial[:maxp]
high = initial[maxp:]
for i in range(maxp):
final.append(high.pop())
final.append(low.pop())
final.append(high.pop())
for item in final[:-1]:
print(item,end=' ')
print(final[-1],end='')
if __name__ == "__main__":
main()
``` | instruction | 0 | 81,916 | 10 | 163,832 |
No | output | 1 | 81,916 | 10 | 163,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The difference between the versions is that in the easy version all prices a_i are different. You can make hacks if and only if you solved both versions of the problem.
Today is Sage's birthday, and she will go shopping to buy ice spheres. All n ice spheres are placed in a row and they are numbered from 1 to n from left to right. Each ice sphere has a positive integer price. In this version all prices are different.
An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.
You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of ice spheres in the shop.
The second line contains n different integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) β the prices of ice spheres.
Output
In the first line print the maximum number of ice spheres that Sage can buy.
In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.
Example
Input
5
1 2 3 4 5
Output
2
3 1 4 2 5
Note
In the example it's not possible to place ice spheres in any order so that Sage would buy 3 of them. If the ice spheres are placed like this (3, 1, 4, 2, 5), then Sage will buy two spheres: one for 1 and one for 2, because they are cheap.
Submitted Solution:
```
# ===============================================================================================
# importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter # Counter(list) return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
# If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
# If the element is already present in the list,
# the right most position where element has to be inserted is returned
# ==============================================================================================
# fast I/O region
BUFSIZE = 8192
from sys import stderr
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
# ===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
# ===============================================================================================
# some shortcuts
mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input
def out(var): sys.stdout.write(str(var)) # for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def zerolist(n): return [0] * n
def nextline(): out("\n") # as stdout.write always print sring.
def testcase(t):
for p in range(t):
solve()
def printlist(a):
for p in range(0, len(a)):
out(str(a[p]) + ' ')
def pow(x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is more , than or equal to p
if (x == 0):
return 0
while (y > 0):
if ((y & 1) == 1): # If y is odd, multiply, x with result
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
from functools import reduce
def factors(n):
return set(reduce(list.__add__,
([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
def gcd(a, b):
if a == b: return a
while b > 0: a, b = b, a % b
return a
# fact=[1]
# modi=[1]
# p=1
# N=10**9 + 7
# for i in range(1,2001):
# p=(p*i)%N
# fact.append(p)
# modi.append(pow(p,N-2,N))
# def ncr(n,r):
# return (fact[n]*modi[n-r]*modi[r])%N
# =========================================================================================
def solve():
n=int(inp())
ar=lis()
ar.sort(reverse=True)
ans=[]
k=(n+1)//2
ar1=ar[:k]
ar2=ar[k:]
k2=(n)//2
for i in range(k2):
ans.append(ar1[i])
ans.append(ar2[i])
if n%2==1:
ans.append(ar1[k2])
c=0
for i in range(1,n-1):
if ans[i]<ans[i-1] and ans[i] <ans[i+1]:
c+=1
b_c=c
b_ans=ans.copy()
bk=(n-1)//2
for i in range(30):
if b_c==bk:
print(b_c)
print(*b_ans)
quit()
ans = []
shuffle(ar1)
k2 = (n) // 2
for i in range(k2):
ans.append(ar1[i])
ans.append(ar2[i])
if n % 2 == 1:
ans.append(ar1[k2])
c = 0
for i in range(1, n - 1):
if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
c += 1
if c>b_c:
b_c=c
b_ans=ans.copy()
print(b_c)
print(*b_ans)
from random import shuffle
solve()
#testcase(int(inp()))
``` | instruction | 0 | 81,917 | 10 | 163,834 |
No | output | 1 | 81,917 | 10 | 163,835 |
Provide a correct Python 3 solution for this coding contest problem.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer. | instruction | 0 | 82,178 | 10 | 164,356 |
"Correct Solution:
```
n,p=[int(i)for i in input().split()]
a=["half"!=input() for _ in range(n)]
s=c=0
for x in a[::-1]:s+=c*p+x*p//2;c=c*2+x
print(s)
``` | output | 1 | 82,178 | 10 | 164,357 |
Provide a correct Python 3 solution for this coding contest problem.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer. | instruction | 0 | 82,179 | 10 | 164,358 |
"Correct Solution:
```
n,p = map(int, input().split())
s = []
plus = 0;
for i in range(0,n):
s.append(input());
if s[i] == 'halfplus':
plus+=1
x = 1
for i in range(n - 2, -1, -1):
if s[i] == 'halfplus':
x = 2*x + 1;
else:
x = 2*x
print( x * p - plus * (p//2))
``` | output | 1 | 82,179 | 10 | 164,359 |
Provide a correct Python 3 solution for this coding contest problem.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer. | instruction | 0 | 82,180 | 10 | 164,360 |
"Correct Solution:
```
n, p = map(int, input().split())
A = []
for i in range(n):
s = input()
A.append(2 if s == 'half' else 1)
A.pop()
A = A[::-1]
h = p // 2
ans = h
c = 1
for i in range(n - 1):
if A[i] == 1:
ans += p * c + h
c = c * 2 + 1
else:
ans += c * p
c *= 2
print(ans)
``` | output | 1 | 82,180 | 10 | 164,361 |
Provide a correct Python 3 solution for this coding contest problem.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer. | instruction | 0 | 82,181 | 10 | 164,362 |
"Correct Solution:
```
n, p = map(int, input().split())
s = sum(1 << i for i in range(n) if 's' in input())
print(sum(s >> i for i in range(n)) * p >> 1)
``` | output | 1 | 82,181 | 10 | 164,363 |
Provide a correct Python 3 solution for this coding contest problem.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer. | instruction | 0 | 82,182 | 10 | 164,364 |
"Correct Solution:
```
m,n = map(int,input().split())
lista = []
valor = 0.5
cont = 0
for i in range(m):
nome = input()
lista.append(nome)
for x in range(2,m+1):
if lista[-x]=='halfplus':
valor = (2*valor)+0.5
cont += valor
else:
valor = 2*valor
cont+= valor
print(int((cont+0.5)*n))
``` | output | 1 | 82,182 | 10 | 164,365 |
Provide a correct Python 3 solution for this coding contest problem.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer. | instruction | 0 | 82,183 | 10 | 164,366 |
"Correct Solution:
```
plus = 0
cnt = 0
a = []
n,p = map(int,input().split())
for i in range(n):
a.append(input())
for i in reversed(range(n)):
if a[i]=='halfplus':
plus+=0.5
cnt = cnt*2+1
else:cnt*=2
print(int((cnt-plus)*p))
``` | output | 1 | 82,183 | 10 | 164,367 |
Provide a correct Python 3 solution for this coding contest problem.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer. | instruction | 0 | 82,184 | 10 | 164,368 |
"Correct Solution:
```
n, p = map(int, input().split())
x = int(''.join('10'[input()=='half'] for _ in range(n))[::-1], 2)
k = 0
while x:
k += x/2
x //= 2
print(int(k*p))
``` | output | 1 | 82,184 | 10 | 164,369 |
Provide a correct Python 3 solution for this coding contest problem.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer. | instruction | 0 | 82,185 | 10 | 164,370 |
"Correct Solution:
```
from math import ceil
n, p = map(int, input().split())
bs = [input() for i in range(n)]
apples = 0
money = 0
for b in bs[::-1]:
apples *= 2
if b == 'halfplus':
apples += 0.5
apples = ceil(apples)
money += apples / 2 * p
print(int(money))
``` | output | 1 | 82,185 | 10 | 164,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Submitted Solution:
```
N, P = map(int, input().split())
S = [0 for _ in range(N)]
for i in range(N):
if input() == 'halfplus':
S[i] = 1
apple = 0
money = 0
for i in range(N)[::-1]:
money += apple * P
apple *= 2
if S[i]:
apple += 1
money += P // 2
print(money)
``` | instruction | 0 | 82,186 | 10 | 164,372 |
Yes | output | 1 | 82,186 | 10 | 164,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Submitted Solution:
```
n, p = map(int, input().split())
l = []
for i in range(n):
l.append(input())
apples = 0
welth = 0
for i in range(n - 1, -1, -1):
if l[i] == 'halfplus':
apples = apples * 2 + 1
else:
apples = apples * 2
welth += apples * p // 2
print(welth)
``` | instruction | 0 | 82,187 | 10 | 164,374 |
Yes | output | 1 | 82,187 | 10 | 164,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Submitted Solution:
```
n, price = map(int, input().split())
history = []
ans = 0
cnt = 0
for i in range(n):
history.append(input())
for s in history[::-1]:
if s == 'halfplus':
cnt = cnt * 2 + 1
else:
cnt *= 2
ans += cnt / 2 * price
print(int(ans))
``` | instruction | 0 | 82,188 | 10 | 164,376 |
Yes | output | 1 | 82,188 | 10 | 164,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Submitted Solution:
```
n, p = map(int, input().split())
a = []
# ignore last value because we know the last apple count
# needs to be 1 with a 'halfplus' sale to reach 0
for _ in range(n-1):
a.append(input())
input()
# find initial apple count
apples = 1
for x in reversed(a):
if x == 'half':
apples *= 2
elif x == 'halfplus':
apples *= 2
apples += 1
#print(apples)
# add again, we removed this at the start
a.append('halfplus')
ans = 0
for x in a:
if x == 'half':
ans += p*(apples/2)
apples /= 2
elif x == 'halfplus':
ans += p*(apples/2)
apples //= 2
assert apples == 0
print(int(ans))
``` | instruction | 0 | 82,189 | 10 | 164,378 |
Yes | output | 1 | 82,189 | 10 | 164,379 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Submitted Solution:
```
n, p = map(int, input().split())
b = []
for i in range(n):
r = input()
b.append(r)
start = 0
if b[0] == 'half':
if n%2==0: start = n
else: start = n+1
else:
if n%2: start = n
else: start = n+1
#print(start)
earned=0
cnt = 0
while cnt != n:
earned = 0
temp = start
cnt = 0
for i in b:
if i[-1] == 'f' and temp%2 == 1: break
if i[-1] == 'e' and temp%2 == 0: break
earned += (temp - temp/2) * p
temp //= 2
cnt += 1
if temp == 0: break
start += 1
#print(start-1)
print(int(earned))
``` | instruction | 0 | 82,190 | 10 | 164,380 |
No | output | 1 | 82,190 | 10 | 164,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Submitted Solution:
```
n, p = map(int, input().split(' '))
ps = [True if input() == "halfplus" else False for _ in range(n)]
t = 1
result = 0.5
for x in reversed(ps[1:]):
if x:
t += 0.5
result += t
t *= 2
print(int(result * p))
``` | instruction | 0 | 82,191 | 10 | 164,382 |
No | output | 1 | 82,191 | 10 | 164,383 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Submitted Solution:
```
n, p = map(int, input().split())
x = int(''.join('10'[input()=='half'] for _ in range(n))[::-1], 2)
k = 0
while x:
k += x/2
x //= 2
print(k*p)
``` | instruction | 0 | 82,192 | 10 | 164,384 |
No | output | 1 | 82,192 | 10 | 164,385 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.
She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.
So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).
For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).
Print the total money grandma should have at the end of the day to check if some buyers cheated her.
Input
The first line contains two integers n and p (1 β€ n β€ 40, 2 β€ p β€ 1000) β the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.
The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.
It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.
Output
Print the only integer a β the total money grandma should have at the end of the day.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Examples
Input
2 10
half
halfplus
Output
15
Input
3 10
halfplus
halfplus
halfplus
Output
55
Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.
Submitted Solution:
```
n, p = map(int, input().split())
b = []
for i in range(n):
r = input()
b.append(r)
#print(start)
earned=0
apples = 0
for i in b[::-1]:
if len(i) != 4:
apples += 1
earned += apples/2.0 * p
earned += apples//2 * p
apples *= 2
#print(start-1)
print(int(earned))
``` | instruction | 0 | 82,193 | 10 | 164,386 |
No | output | 1 | 82,193 | 10 | 164,387 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is constraints.
Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.
Each day (during the morning) Ivan earns exactly one burle.
There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly k_i microtransactions of the i-th type (he orders microtransactions during the evening).
Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.
There are also m special offers in the game shop. The j-th offer (d_j, t_j) means that microtransactions of the t_j-th type are on sale during the d_j-th day.
Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.
Input
The first line of the input contains two integers n and m (1 β€ n, m β€ 1000) β the number of types of microtransactions and the number of special offers in the game shop.
The second line of the input contains n integers k_1, k_2, ..., k_n (0 β€ k_i β€ 1000), where k_i is the number of copies of microtransaction of the i-th type Ivan has to order. It is guaranteed that sum of all k_i is not less than 1 and not greater than 1000.
The next m lines contain special offers. The j-th of these lines contains the j-th special offer. It is given as a pair of integers (d_j, t_j) (1 β€ d_j β€ 1000, 1 β€ t_j β€ n) and means that microtransactions of the t_j-th type are on sale during the d_j-th day.
Output
Print one integer β the minimum day when Ivan can order all microtransactions he wants and actually start playing.
Examples
Input
5 6
1 2 0 2 0
2 4
3 3
1 5
1 2
1 5
2 3
Output
8
Input
5 3
4 2 1 3 2
3 5
4 2
2 5
Output
20 | instruction | 0 | 82,660 | 10 | 165,320 |
Tags: binary search, greedy
Correct Solution:
```
import math
from collections import defaultdict
import sys
#input = sys.stdin.readline
def main():
n, m = map(int, input().split())
k = list(map(int, input().split()))
sales = [(0, 0)] * m
for i in range(m):
a, b = map(int, input().split())
sales[i] = (b, a)
def check(days):
last_sale = {}
for sale in sales:
if sale[1] <= days:
if sale[0] not in last_sale or sale[1] > last_sale[sale[0]]:
last_sale[sale[0]] = sale[1]
date_last_sales = {}
for t, d in last_sale.items():
if d not in date_last_sales:
date_last_sales[d] = [t]
else:
date_last_sales[d].append(t)
balance = 0
required = [0] + k.copy()
end = 0
for d in range(1, days+1):
balance += 1
if d in date_last_sales:
for t in date_last_sales[d]:
if required[t] > 0:
if required[t] > balance:
end += required[t] - balance
balance -= min(required[t], balance)
required[t] = 0
if d == days: # last day
for r in required:
if r > 0:
end += r
return 2*end <= balance
total = sum(k)
for i in range(1, 2*total+1):
if check(i):
print(i)
break
if __name__ == '__main__':
main()
``` | output | 1 | 82,660 | 10 | 165,321 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is constraints.
Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.
Each day (during the morning) Ivan earns exactly one burle.
There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly k_i microtransactions of the i-th type (he orders microtransactions during the evening).
Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.
There are also m special offers in the game shop. The j-th offer (d_j, t_j) means that microtransactions of the t_j-th type are on sale during the d_j-th day.
Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.
Input
The first line of the input contains two integers n and m (1 β€ n, m β€ 1000) β the number of types of microtransactions and the number of special offers in the game shop.
The second line of the input contains n integers k_1, k_2, ..., k_n (0 β€ k_i β€ 1000), where k_i is the number of copies of microtransaction of the i-th type Ivan has to order. It is guaranteed that sum of all k_i is not less than 1 and not greater than 1000.
The next m lines contain special offers. The j-th of these lines contains the j-th special offer. It is given as a pair of integers (d_j, t_j) (1 β€ d_j β€ 1000, 1 β€ t_j β€ n) and means that microtransactions of the t_j-th type are on sale during the d_j-th day.
Output
Print one integer β the minimum day when Ivan can order all microtransactions he wants and actually start playing.
Examples
Input
5 6
1 2 0 2 0
2 4
3 3
1 5
1 2
1 5
2 3
Output
8
Input
5 3
4 2 1 3 2
3 5
4 2
2 5
Output
20 | instruction | 0 | 82,661 | 10 | 165,322 |
Tags: binary search, greedy
Correct Solution:
```
import sys
import copy
DEBUG = False
if DEBUG:
inf = open("input.txt")
else:
inf = sys.stdin
N, M = list(map(int, inf.readline().split(' ')))
n_items = list(map(int, inf.readline().split(' ')))
sales = []
for _ in range(M):
sale = list(map(int, inf.readline().split(' ')))
sales.append(sale) # sale_day, sale_type
sales = sorted(sales, key=lambda x: x[0], reverse=True) # sort by day
def can_buy_in(dday):
used = 0
money_left = dday
items = n_items[:]
for sale_day, sale_type in sales:
if sale_day > dday:
continue
if money_left > sale_day:
money_left = sale_day
can_buy = min(items[sale_type-1], money_left)
# buy it
used += can_buy
items[sale_type-1] -= can_buy
money_left -= can_buy
if money_left == 0:
break
need_money_for_rest = sum(items) * 2
return need_money_for_rest + used <= dday
total_items = sum(n_items)
low = total_items
high = total_items * 2
# find minimum can_buy day
while low <= high:
mid = (low + high) // 2
if can_buy_in(mid):
high = mid-1
else:
low = mid+1
print(low)
``` | output | 1 | 82,661 | 10 | 165,323 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is constraints.
Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.
Each day (during the morning) Ivan earns exactly one burle.
There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly k_i microtransactions of the i-th type (he orders microtransactions during the evening).
Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.
There are also m special offers in the game shop. The j-th offer (d_j, t_j) means that microtransactions of the t_j-th type are on sale during the d_j-th day.
Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.
Input
The first line of the input contains two integers n and m (1 β€ n, m β€ 1000) β the number of types of microtransactions and the number of special offers in the game shop.
The second line of the input contains n integers k_1, k_2, ..., k_n (0 β€ k_i β€ 1000), where k_i is the number of copies of microtransaction of the i-th type Ivan has to order. It is guaranteed that sum of all k_i is not less than 1 and not greater than 1000.
The next m lines contain special offers. The j-th of these lines contains the j-th special offer. It is given as a pair of integers (d_j, t_j) (1 β€ d_j β€ 1000, 1 β€ t_j β€ n) and means that microtransactions of the t_j-th type are on sale during the d_j-th day.
Output
Print one integer β the minimum day when Ivan can order all microtransactions he wants and actually start playing.
Examples
Input
5 6
1 2 0 2 0
2 4
3 3
1 5
1 2
1 5
2 3
Output
8
Input
5 3
4 2 1 3 2
3 5
4 2
2 5
Output
20 | instruction | 0 | 82,662 | 10 | 165,324 |
Tags: binary search, greedy
Correct Solution:
```
import sys
import bisect
import copy
input = sys.stdin.readline
n,m=map(int,input().split())
K=[0]+list(map(int,input().split()))
SP=[list(map(int,input().split())) for i in range(m)]
SP2=[[] for i in range(n+1)]
for d,t in SP:
SP2[t].append(d)
for i in range(n+1):
SP2[i].sort()
SUM=sum(K)
MIN=SUM
MAX=SUM*2
MAXBUY=0
while MIN!=MAX:
day=(MIN+MAX)//2
DAYS=[[] for i in range(day+1)]
for i in range(n+1):
x=bisect.bisect_right(SP2[i],day)-1
if x>=0:
DAYS[SP2[i][x]].append(i)
GOLD=0
SUMK=SUM
K2=copy.deepcopy(K)
for d in range(1,day+1):
GOLD+=1
for t in DAYS[d]:
DBUY=min(K2[t],GOLD,SUMK)
K2[t]-=DBUY
GOLD-=DBUY
if GOLD>=sum(K2)*2:
MAX=day
else:
MIN=day+1
print(MIN)
``` | output | 1 | 82,662 | 10 | 165,325 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is constraints.
Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.
Each day (during the morning) Ivan earns exactly one burle.
There are n types of microtransactions in the game. Each microtransaction costs 2 burles usually and 1 burle if it is on sale. Ivan has to order exactly k_i microtransactions of the i-th type (he orders microtransactions during the evening).
Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for 1 burle and otherwise he can buy it for 2 burles.
There are also m special offers in the game shop. The j-th offer (d_j, t_j) means that microtransactions of the t_j-th type are on sale during the d_j-th day.
Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.
Input
The first line of the input contains two integers n and m (1 β€ n, m β€ 1000) β the number of types of microtransactions and the number of special offers in the game shop.
The second line of the input contains n integers k_1, k_2, ..., k_n (0 β€ k_i β€ 1000), where k_i is the number of copies of microtransaction of the i-th type Ivan has to order. It is guaranteed that sum of all k_i is not less than 1 and not greater than 1000.
The next m lines contain special offers. The j-th of these lines contains the j-th special offer. It is given as a pair of integers (d_j, t_j) (1 β€ d_j β€ 1000, 1 β€ t_j β€ n) and means that microtransactions of the t_j-th type are on sale during the d_j-th day.
Output
Print one integer β the minimum day when Ivan can order all microtransactions he wants and actually start playing.
Examples
Input
5 6
1 2 0 2 0
2 4
3 3
1 5
1 2
1 5
2 3
Output
8
Input
5 3
4 2 1 3 2
3 5
4 2
2 5
Output
20 | instruction | 0 | 82,663 | 10 | 165,326 |
Tags: binary search, greedy
Correct Solution:
```
def main():
inp = readnumbers()
ii = 0
n = inp[ii]
ii += 1
m = inp[ii]
ii += 1
K = inp[ii:n+ii]
ii += n
types = [[] for _ in range(n)]
for _ in range(m):
types[inp[ii+1]-1].append(inp[ii] - 1)
ii += 2
needed = sum(K)
nonzero = [t for t in range(n) if K[t]>0 and types[t]]
for t in nonzero:
types[t].sort()
a = 0
b = 4*10**5 + 10
extra = [0]*b
while a<b:
mid = (a+b)//2
for t in nonzero:
A = types[t]
ind = len(A)
while ind and A[ind-1] >= mid: ind -= 1
if ind:
extra[A[ind-1]] += K[t]
bought = 0
money = 0
for d in range(mid):
money += 1
if extra[d]:
amount = min(money, extra[d])
money -= amount
bought += amount
extra[d] = 0
left = needed - bought
if money >= left*2:
b = mid
else:
a = mid + 1
print(a)
######## Python 2 and 3 footer by Pajenegod and c1729
# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.
# So on cf, use PyPy2 for best string performance.
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# Cout implemented in Python
import sys
class ostream:
def __lshift__(self,a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero = 0):
conv = ord if py2 else lambda x:x
A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read()
try:
while True:
if s[i] >= b'0' [0]:
numb = 10 * numb + conv(s[i]) - 48
elif s[i] == b'-' [0]: sign = -1
elif s[i] != b'\r' [0]:
A.append(sign*numb)
numb = zero; sign = 1
i += 1
except:pass
if s and s[-1] >= b'0' [0]:
A.append(sign*numb)
return A
if __name__== "__main__":
main()
``` | output | 1 | 82,663 | 10 | 165,327 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.