message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.
Input
The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 1
2 1
2
Output
3
Input
2 1
2 1
10
Output
-5
Note
In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5. | instruction | 0 | 1,180 | 14 | 2,360 |
Tags: implementation
Correct Solution:
```
n,x=map(int,input().split())
l=list(map(int,input().split()))
m=int(input())
if m>=n:
print(sum(l)-(m-n)*x)
# j = sorted(l)
# print(j)
else:
j=sorted(l)
# print(j)
c=0
for i in range (m):
c+=j[i]
print(c)
``` | output | 1 | 1,180 | 14 | 2,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.
Input
The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 1
2 1
2
Output
3
Input
2 1
2 1
10
Output
-5
Note
In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5. | instruction | 0 | 1,181 | 14 | 2,362 |
Tags: implementation
Correct Solution:
```
n, d = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
m = int(input())
if m <= n:
print(sum(arr[:m]))
else:
print(sum(arr) - d * (m-n))
``` | output | 1 | 1,181 | 14 | 2,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.
Input
The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 1
2 1
2
Output
3
Input
2 1
2 1
10
Output
-5
Note
In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5. | instruction | 0 | 1,182 | 14 | 2,364 |
Tags: implementation
Correct Solution:
```
n, d = list(map(int, input().split()))
a = list(map(int, input().split()))
m = int(input())
s = 0
a.sort()
for i in range(m):
if i < len(a):
s += a[i]
else:
s -= d
print(s)
``` | output | 1 | 1,182 | 14 | 2,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.
Input
The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 1
2 1
2
Output
3
Input
2 1
2 1
10
Output
-5
Note
In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5. | instruction | 0 | 1,183 | 14 | 2,366 |
Tags: implementation
Correct Solution:
```
#!/usr/bin/env python3
n, d = tuple(map(int, input().split(None, 2)))
a = list(map(int, input().split()))
assert len(a) == n
m = int(input())
a.sort()
if n <= m:
print(sum(a) - d * (m - n))
else:
print(sum(a[:m]))
``` | output | 1 | 1,183 | 14 | 2,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.
Input
The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 1
2 1
2
Output
3
Input
2 1
2 1
10
Output
-5
Note
In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5. | instruction | 0 | 1,184 | 14 | 2,368 |
Tags: implementation
Correct Solution:
```
#!/usr/local/bin/python3
n, d = map(int, input().split())
a = list(map(int, input().split()))
m = int(input())
a.sort()
if m <= n:
result = sum(a[:m])
else:
result = sum(a) - (m-n)*d
print(result)
``` | output | 1 | 1,184 | 14 | 2,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.
Input
The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 1
2 1
2
Output
3
Input
2 1
2 1
10
Output
-5
Note
In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5. | instruction | 0 | 1,185 | 14 | 2,370 |
Tags: implementation
Correct Solution:
```
# It's all about what U BELIEVE
import sys
input = sys.stdin.readline
def gint(): return int(input())
def gint_arr(): return list(map(int, input().split()))
def gfloat(): return float(input())
def gfloat_arr(): return list(map(float, input().split()))
def pair_int(): return map(int, input().split())
###############################################################################
INF = (1 << 31)
MOD = "1000000007"
dx = [-1, 0, 1, 0, -1, 1, 1, -1]
dy = [ 0, 1, 0, -1, 1, 1, -1, -1]
############################ SOLUTION IS COMING ###############################
n, d = gint_arr()
a = gint_arr()
a.sort()
m = gint()
res = sum(a[:m])
print(res if m <= n else res - (m - n) * d)
``` | output | 1 | 1,185 | 14 | 2,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.
Input
The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 1
2 1
2
Output
3
Input
2 1
2 1
10
Output
-5
Note
In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5. | instruction | 0 | 1,186 | 14 | 2,372 |
Tags: implementation
Correct Solution:
```
M = lambda: map(int, input().split())
L = lambda: list(map(int, input().split()))
I = lambda: int(input())
n, d = M()
a, k = sorted(L()), I()
print(sum(a) + (n - k) * d if n < k else sum(a[: k]))
``` | output | 1 | 1,186 | 14 | 2,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.
Input
The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 1
2 1
2
Output
3
Input
2 1
2 1
10
Output
-5
Note
In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 = - 5. | instruction | 0 | 1,187 | 14 | 2,374 |
Tags: implementation
Correct Solution:
```
n, d = map(int, input().split())
a = list(map(int, input().split()))
m = int(input())
s = 0
a = sorted(a)
zanyat = n - m
if zanyat == 0:
s = sum(a)
elif zanyat > 0:
s = sum(a[:m])
else:
# print(zanyat)
s = zanyat * d + sum(a)
print(s)
``` | output | 1 | 1,187 | 14 | 2,375 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. | instruction | 0 | 1,196 | 14 | 2,392 |
Tags: implementation
Correct Solution:
```
N, A, B, Joy = int(input()), list(map(int, input().split())), list(map(int, input().split())), 0
for i in range(N):
if A[i] * 2 < B[i] or B[i] == 1:
Joy -= 1
else:
Joy += ((B[i] // 2) ** 2 if B[i] % 2 == 0 else B[i] // 2 * (B[i] // 2 + 1))
print(Joy)
# Caption: God bless you General Soleimani
# ---------Hard Revenge---------
# ****** Rest in Peace ******
``` | output | 1 | 1,196 | 14 | 2,393 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. | instruction | 0 | 1,197 | 14 | 2,394 |
Tags: implementation
Correct Solution:
```
# # import numpy as np
#
# def spliter(arr,low,high):
# if(high-low==1):
# # print("1")
# return 1
#
# if(arr[low:high]==sorted(arr[low:high])):
# # print("here "+str(high-low))
# return high-low
# else:
# mid=(high+low)//2
# # print("----------")
# # print((low,mid))
# # print("---------")
# # print((mid,high))
# # print("-----------")
# a=spliter(arr,low,mid)
# # print("was here")
# b=spliter(arr,mid,high)
#
# # print((a,b))
# if(a>=b):
# return a
# else:
# return b
#
# def main():
# n=int(input())
# arr=list(map(int,input().split(" ")))
# lenght = n
# if arr==sorted(arr):
# print(n)
# else:
# mid=lenght//2
# a=spliter(arr,0,mid)
# b=spliter(arr,mid,lenght)
# if(a>b):
# print(a)
# else:
# print(b)
#
# main()
# def main():
# checker="nineteen"
# # n="nneteenabcnneteenabcnneteenabcnneteenabcnneteenabcii"
# n=input()
# number={}
# # print(set(n))
# for i in n:
# if i in checker:
# number[i]=number.get(i,0)+1
# # number=sorted(number.values())
# # print(sorted(number.keys()))
# for i in ["n","t","i","e"]:
# if i not in number.keys():
# break
#
# else:
# num=number["i"]
# min=num
# other_Dict={"n":3,"i":1,"e":3,"t":1}
# if(number["n"]%3==2):
# number["n"]=number.get("n",0)+number["n"]//3
#
# for i in number.keys():
# if (number[i]//other_Dict[i]>=num):
# pass
# elif(number[i]//other_Dict[i]<=num):
#
# if(min>number[i]//other_Dict[i]):
# min=number[i]//other_Dict[i]
# else:
# print(min)
# return
# print(0)
# main()
# return False
# for i,j in number:
# if i=="n" and i
# # print(number)
# print(main())
# main()
# d={}
# d[10]=1
# d[10]+=2
# d[10].append(10)
# 2//3
# d.get(10,5)
# d.keys()
# main()
# d={}
# for i in "nineteen":
# d[i]=d.get(i,0)+1
# d
# "i" not in d.keys()
# some="nineoindojaoidjoiajdoiaoidjiadjkajoidjoiajdljadjoiadlkjaadj"
# list(some.split("nineteen"))
#
#
# def main():
# n=list(map(int,input().split(" ")))
# navigation=[]
# for i in range(n[1]-n[2],n[1]):
# if(i>0):
# navigation.append(i)
# for i in range(n[1],n[1]+n[2]+1):
# if(i<=n[0]):
# navigation.append(i)
# if(navigation[0]>1):
# navi=[0]*len(navigation)
# navi[0]="<<"
# navi[1:len(navigation)]=navigation
# navigation=navi
# if(navigation[len(navigation)-1]<n[0]):
# navigation.append(">>")
# string=""
# for i in navigation:
# if(str(i)==str(n[1])):
# string+="({}) ".format(i)
# else:
# string+="{} ".format(i)
# print(string[:-1])
# main()
# question number Inna and Alarm Clock
# import numpy as np
# def inna_and_alarm_clock:
# n=int(input())
# matrix=[]
# display=[[0]*100]*100
# display=np.array(display)
# rows=[]
# columns=[]
# for _ in range(n):
# matrix.append(map(int,input().split(" ")))
# # matrix=data
# for i,j in matrix:
# # print((i,j))
# display[j][i]=1
# print(np.array(display))
# vertical_steps=0
# horizontal_steps=0
# for i in range(100):
# if 1 in display[:,i]:
# horizontal_steps+=1
# for i in range(100):
# if 1 in display[i,:]:
# vertical_steps+=1
# if(horizontal_steps<vertical_steps):
# print(horizontal_steps)
# else:
# print(vertical_steps)
#
# # data=np.array([[1,1],[1,2],[2,3],[3,3]])
# main()
#
# arr=[[1,1],[1,2],[2,3],[3,3]]
def main():
n=int(input())
a=list(map(float,input().split(" ")))
b=list(map(float,input().split(" ")))
iterr=list(zip(a,b))
happiness=0
for x,y in iterr:
# print("x={},y={}".format(x,y),end=" ")
# print((x,y))
if(x<y/2 or y==1):
happiness-=1
elif(x==y/2):
happiness+=x*x
elif(x>y/2):
if(y%2==0):
x=y//2
happiness+=x*x
else:
x=(y+1)//2
happiness+=(x*(y-x))
# print(happiness)
# happiness+=x*x
print(int(happiness))
main()
``` | output | 1 | 1,197 | 14 | 2,395 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. | instruction | 0 | 1,198 | 14 | 2,396 |
Tags: implementation
Correct Solution:
```
def great_sum_finder(num, rang):
if abs(num - rang) > rang or num == 1:
return -1
if rang == 1:
return 1
if num < rang:
return great_sum_finder(num, num - 1)
return (num // 2) * (num - num // 2)
n = int(input())
a = input().split()
b = input().split()
a = list(map(int, a))
b = list(map(int, b))
joy = 0
for i in range(n):
joy += great_sum_finder(b[i], a[i])
print(joy)
``` | output | 1 | 1,198 | 14 | 2,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. | instruction | 0 | 1,199 | 14 | 2,398 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = input().split()
a = list(map(lambda x: int(x) if x.isdigit() else 0, a))
b = input().split()
b = list(map(lambda x: int(x) if x.isdigit() else 0, b))
sum = 0
while(len(a) > 0):
if(b[len(a)-1] == 1):
sum-=1
else:
if(a[len(a)-1] == 1):
if(b[len(a)-1] == 2):
sum+=1
else:
sum-=1
else:
c = int(b[len(a)-1]/2)
while(c > a[len(a)-1]):
c-=1
if(b[len(a)-1] - c > a[len(a)-1]):
sum-=1
else:
sum = sum + c*(b[len(a)-1]-c)
d = len(a)
del a[d-1]
del b[d-1]
print(sum)
``` | output | 1 | 1,199 | 14 | 2,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. | instruction | 0 | 1,200 | 14 | 2,400 |
Tags: implementation
Correct Solution:
```
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
x=0
for i in range(n):
if 2*a[i]>=b[i] and b[i]!=1:
p=b[i]//2
q=b[i]-p
x=x+p*q
else:
x=x-1
print(x)
``` | output | 1 | 1,200 | 14 | 2,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. | instruction | 0 | 1,201 | 14 | 2,402 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
for i in range(n):
if 2 * a[i] >= b[i] and b[i] > 1:
x = b[i] // 2
ans += (x * (b[i] - x))
else:
ans -= 1
print(ans)
``` | output | 1 | 1,201 | 14 | 2,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. | instruction | 0 | 1,202 | 14 | 2,404 |
Tags: implementation
Correct Solution:
```
n = int(input())
a,b = list(map(int,input().split())), list(map(int,input().split()))
r = 0
for i in range(n):
if 2*a[i] >= b[i] and b[i]>1:
x = b[i]//2
y = b[i]-x
r += x*y
else:
r -= 1
print(r)
``` | output | 1 | 1,202 | 14 | 2,405 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1. | instruction | 0 | 1,203 | 14 | 2,406 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
for i in range(n):
if 2 <= b[i] <= 2 * a[i]:
x = b[i] // 2
res += x * (b[i] - x)
else:
res += -1
print(res)
``` | output | 1 | 1,203 | 14 | 2,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1.
Submitted Solution:
```
num = int(input())
arr_a = list(map(int,(input()).split()))
arr_b = list(map(int,(input()).split()))
joy = 0
for i in range(num):
if arr_b[i] == 1:
joy -= 1
continue
if arr_a[i] * 2 >= arr_b[i]:
temp = arr_b[i] // 2
joy = joy + temp * (arr_b[i] - temp)
else:
joy -= 1
print(joy)
``` | instruction | 0 | 1,204 | 14 | 2,408 |
Yes | output | 1 | 1,204 | 14 | 2,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
for i in range(n):
if 2 * a[i] < b[i]:
s -= 1
else:
x = b[i] // 2
y = b[i] - x
if x * y != 0:
s += x * y
else:
s -= 1
print(s)
# 10
# 1 2 3 4 5 6 7 8 9 10
# 1 2 3 4 5 6 7 8 9 10
``` | instruction | 0 | 1,205 | 14 | 2,410 |
Yes | output | 1 | 1,205 | 14 | 2,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1.
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
happiness = 0
for i in range(n):
if (b[i] + 1) // 2 <= a[i] and b[i] > 1:
happiness += (b[i] // 2) * ((b[i] + 1) // 2)
else:
happiness -= 1
print(happiness)
``` | instruction | 0 | 1,206 | 14 | 2,412 |
Yes | output | 1 | 1,206 | 14 | 2,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1.
Submitted Solution:
```
input()
print(sum(-1 if (b>2*a or b==1) else ((b//2)*((b+1)//2)) for a,b in zip(map(int,input().split()),map(int,input().split()))))
``` | instruction | 0 | 1,207 | 14 | 2,414 |
Yes | output | 1 | 1,207 | 14 | 2,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1.
Submitted Solution:
```
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
def check_even_odd(e):
if e%2 == 0:
return "Even"
else:
return "Odd"
# To access the elements of A and B
res = 0
i = 0
while i<n:
add = 0
prop = check_even_odd(B[i])
if prop == "Even":
if A[i] >= (B[i]//2):
add = (B[i]//2)**2
else:
add = -1
else:
if A[i] >= ((B[i]+1)//2):
a = (B[i]+1)//2
b = a-1
add = a*b
else:
add = -1
res += add
i += 1
print(res)
``` | instruction | 0 | 1,208 | 14 | 2,416 |
No | output | 1 | 1,208 | 14 | 2,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1.
Submitted Solution:
```
n = int(input())
aInp = input()
bInp = input()
aList = []
bList = []
joy = 0
for i in range(n):
aList.append(int(aInp.split(' ')[i]))
bList.append(int(bInp.split(' ')[i]))
aList.sort()
bList.sort()
for b in bList:
for a1 in range(len(aList)):
if n == 1:
if b == aList[a1]:
joy = joy + aList[a1]*aList[a1]
else:
joy = joy - 1
if aList[a1] < b:
for a2 in range(a1+1,len(aList)):
if aList[a1] + aList[a2] == b:
joy = joy + aList[a1] * aList[a2]
b = 0
break
if a2 == len(aList) - 1:
joy = joy - 1
break
print(joy)
``` | instruction | 0 | 1,209 | 14 | 2,418 |
No | output | 1 | 1,209 | 14 | 2,419 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1.
Submitted Solution:
```
n, a, b = input(), list(map(int, input().split())), list(map(int, input().split()))
print(sum(y * y - (y & 1) if 2 * x >= y else -1 for x, y in zip(a, b)) // 4)
``` | instruction | 0 | 1,210 | 14 | 2,420 |
No | output | 1 | 1,210 | 14 | 2,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the i-th note at volume v (1 ≤ v ≤ ai; v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the i-th note was played on the guitar and the piano must equal bi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the i-th note at volumes xi and yi (xi + yi = bi) correspondingly, Sereja's joy rises by xi·yi.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of notes in the song. The second line contains n integers ai (1 ≤ ai ≤ 106). The third line contains n integers bi (1 ≤ bi ≤ 106).
Output
In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.
Examples
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note
In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1·1 + 1·1 + 1·2 = 4.
In the second sample, there is no such pair (x, y), that 1 ≤ x, y ≤ 2, x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1.
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
x=list(map(int,input().split()))
count=0
for i in range(len(l)):
if l[i]>x[i]:
count-=1
else:
count+=(int(l[i]//2)*(l[i]-int(l[i]//2)))
if x[i]-l[i]<=l[i]:
count+=(l[i]*(x[i]-l[i]))
else:
count-=1
print(count)
``` | instruction | 0 | 1,211 | 14 | 2,422 |
No | output | 1 | 1,211 | 14 | 2,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).
The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
Input
The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).
The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).
Output
Print the maximum possible total sociability of all groups.
Examples
Input
5
1 2 3 1 2
Output
3
Input
3
3 3 3
Output
0
Note
In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.
In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group. | instruction | 0 | 1,248 | 14 | 2,496 |
Tags: data structures, dp, greedy
Correct Solution:
```
#import sys
#input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
dp = [0] * n
pos, neg = -10 ** 18, -10 ** 18
for i, x in enumerate(a):
prv = 0 if i == 0 else dp[i - 1]
dp[i] = max(neg + x, pos - x, prv)
pos = max(pos, prv + x)
neg = max(neg, prv - x)
print(dp[-1])
``` | output | 1 | 1,248 | 14 | 2,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).
The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
Input
The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).
The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).
Output
Print the maximum possible total sociability of all groups.
Examples
Input
5
1 2 3 1 2
Output
3
Input
3
3 3 3
Output
0
Note
In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.
In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group. | instruction | 0 | 1,249 | 14 | 2,498 |
Tags: data structures, dp, greedy
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
dp = [0] * n
pos, neg = -10 ** 18, -10 ** 18
for i, x in enumerate(a):
prv = 0 if i == 0 else dp[i - 1]
dp[i] = max(neg + x, pos - x, prv)
pos = max(pos, prv + x)
neg = max(neg, prv - x)
print(dp[-1])
``` | output | 1 | 1,249 | 14 | 2,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).
The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
Input
The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).
The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).
Output
Print the maximum possible total sociability of all groups.
Examples
Input
5
1 2 3 1 2
Output
3
Input
3
3 3 3
Output
0
Note
In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.
In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.
Submitted Solution:
```
l = input()
a = list(map(int, input().split()))
a.sort()
x = 0
for i in range(len(a)//2):
x+= (a[len(a)-1-i]- a[i])
print(x)
``` | instruction | 0 | 1,250 | 14 | 2,500 |
No | output | 1 | 1,250 | 14 | 2,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).
The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
Input
The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).
The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).
Output
Print the maximum possible total sociability of all groups.
Examples
Input
5
1 2 3 1 2
Output
3
Input
3
3 3 3
Output
0
Note
In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.
In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.
Submitted Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
dp = [0] * n
mx = -a[0]
for i, x in enumerate(a):
prv = 0 if i == 0 else dp[i - 1]
dp[i] = max(mx + x, prv)
mx = max(mx, prv - x)
print(dp[-1])
``` | instruction | 0 | 1,251 | 14 | 2,502 |
No | output | 1 | 1,251 | 14 | 2,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.
For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.
Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.
Help Vasya find the maximum number of photos he is able to watch during T seconds.
Input
The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.
Second line of the input contains a string of length n containing symbols 'w' and 'h'.
If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.
If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.
Output
Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
Examples
Input
4 2 3 10
wwhw
Output
2
Input
5 2 4 13
hhwhh
Output
4
Input
5 2 4 1000
hhwhh
Output
5
Input
3 1 100 10
whw
Output
0
Note
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it. | instruction | 0 | 1,295 | 14 | 2,590 |
Tags: binary search, brute force, dp, two pointers
Correct Solution:
```
#! /usr/bin/env python3
def main():
n, a, b, t = map(int, input().split())
oris = input()
def get_time(front, rear, count_rot):
span = front - rear
offset = min(front, -rear)
return span, span * a + (span + 1) + offset * a + count_rot * b
front = rear = span = count_rot = new_count_rot = time = 0
has_one = False
for i in range(0, -n, -1):
if oris[i] == 'w':
new_count_rot += 1
new_span, new_time = get_time(front, i, new_count_rot)
if new_time > t:
break
has_one = True
span, time, rear, count_rot = new_span, new_time, i, new_count_rot
if not has_one:
return 0
maxi = max_span = n - 1
while front < maxi and rear <= 0 and span != max_span:
front += 1
if oris[front] == 'w':
count_rot += 1
while True:
new_span, time = get_time(front, rear, count_rot)
if time <= t:
break
if oris[rear] == 'w':
count_rot -= 1
rear += 1
if rear > 0:
return span + 1
span = max(new_span, span)
return span + 1
print(main())
``` | output | 1 | 1,295 | 14 | 2,591 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.
For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.
Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.
Help Vasya find the maximum number of photos he is able to watch during T seconds.
Input
The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.
Second line of the input contains a string of length n containing symbols 'w' and 'h'.
If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.
If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.
Output
Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
Examples
Input
4 2 3 10
wwhw
Output
2
Input
5 2 4 13
hhwhh
Output
4
Input
5 2 4 1000
hhwhh
Output
5
Input
3 1 100 10
whw
Output
0
Note
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it. | instruction | 0 | 1,296 | 14 | 2,592 |
Tags: binary search, brute force, dp, two pointers
Correct Solution:
```
import bisect
def preview(n, a, b, t, S):
t -= b+1 if S[0] else 1
S[0] = False
if t < 0:
return 0
R = []
s = 0
for i in range(1, n):
s += a + (b+1 if S[i] else 1)
if s > t:
break
R.append(s)
else:
return n
L = []
s = 0
for i in reversed(range(1, n)):
s += a + (b+1 if S[i] else 1)
if s > t:
break
L.append(s)
m = 1 + max(len(R), len(L))
ai = 0
j = len(L) - 1
for i in range(len(R)):
ai += a
t1 = t - R[i] - ai
if t1 < 0:
break
j = bisect.bisect_right(L, t1, hi=j+1) - 1
if j < 0:
break
m = max(m, i + j + 3)
ai = 0
j = len(R) - 1
for i in range(len(L)):
ai += a
t1 = t - L[i] - ai
if t1 < 0:
break
j = bisect.bisect_right(R, t1, hi=j+1) - 1
if j < 0:
break
m = max(m, i + j + 3)
assert m < n
return m
def main():
n, a, b, t = readinti()
S = [c == 'w' for c in input()]
print(preview(n, a, b, t, S))
##########
import sys
def readint():
return int(input())
def readinti():
return map(int, input().split())
def readintl():
return list(readinti())
def readintt():
return tuple(readinti())
def readintll(k):
return [readintl() for _ in range(k)]
def readinttl(k):
return [readintt() for _ in range(k)]
def log(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
if __name__ == '__main__':
main()
``` | output | 1 | 1,296 | 14 | 2,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.
For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.
Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.
Help Vasya find the maximum number of photos he is able to watch during T seconds.
Input
The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.
Second line of the input contains a string of length n containing symbols 'w' and 'h'.
If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.
If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.
Output
Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
Examples
Input
4 2 3 10
wwhw
Output
2
Input
5 2 4 13
hhwhh
Output
4
Input
5 2 4 1000
hhwhh
Output
5
Input
3 1 100 10
whw
Output
0
Note
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
Submitted Solution:
```
import bisect
def preview(n, a, b, t, S):
t -= b if S[0] else 1
S[0] = False
if t < 0:
return 0
R = []
s = 0
for i in range(1, n):
s += a + (b if S[i] else 1)
if s > t:
break
R.append(s)
else:
return n
L = []
s = 0
for i in reversed(range(1, n)):
s += a + (b if S[i] else 1)
if s > t:
break
L.append(s)
m = 1 + max(len(R), len(L))
ai = 0
j = len(L) - 1
for i in range(len(R)):
ai += a
t1 = t - R[i] - ai
if t1 < 0:
break
j = bisect.bisect_right(L, t1, hi=j+1) - 1
if j < 0:
break
m = max(m, i + j + 3)
ai = 0
j = len(R) - 1
for i in range(len(L)):
ai += a
t1 = t - L[i] - ai
if t1 < 0:
break
j = bisect.bisect_right(R, t1, hi=j+1) - 1
if j < 0:
break
m = max(m, i + j + 3)
return m
def main():
n, a, b, t = readinti()
S = [c == 'w' for c in input()]
print(preview(n, a, b, t, S))
##########
import sys
def readint():
return int(input())
def readinti():
return map(int, input().split())
def readintl():
return list(readinti())
def readintt():
return tuple(readinti())
def readintll(k):
return [readintl() for _ in range(k)]
def readinttl(k):
return [readintt() for _ in range(k)]
def log(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
if __name__ == '__main__':
main()
``` | instruction | 0 | 1,297 | 14 | 2,594 |
No | output | 1 | 1,297 | 14 | 2,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.
For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.
Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.
Help Vasya find the maximum number of photos he is able to watch during T seconds.
Input
The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.
Second line of the input contains a string of length n containing symbols 'w' and 'h'.
If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.
If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.
Output
Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
Examples
Input
4 2 3 10
wwhw
Output
2
Input
5 2 4 13
hhwhh
Output
4
Input
5 2 4 1000
hhwhh
Output
5
Input
3 1 100 10
whw
Output
0
Note
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
Submitted Solution:
```
import bisect
def preview(n, a, b, t, S):
if S[0]:
t -= b
S[0] = False
else:
t -= 1
if t < 0:
return 0
R = []
s = 0
for i in range(1, n):
s += a + (b if S[i] else 1)
if s > t:
break
R.append(s)
else:
return n
L = []
s = 0
for i in reversed(range(1, n)):
s += a + (b if S[i] else 1)
if s > t:
break
L.append(s)
m = 1 + max(len(R), len(L))
t1 = t
for i in range(len(R)):
t1 -= R[i] + a
if t1 < 0:
break
j = bisect.bisect_right(L, t1) - 1
if j > i:
break
if j >= 0:
m = max(m, i + j + 3)
t1 = t
for i in range(len(L)):
t1 -= L[i] + a
if t1 < 0:
break
j = bisect.bisect_right(R, t1) - 1
if j > i:
break
if j >= 0:
m = max(m, i + j + 3)
return m
def main():
n, a, b, t = readinti()
S = [c == 'w' for c in input()]
print(preview(n, a, b, t, S))
##########
import sys
def readint():
return int(input())
def readinti():
return map(int, input().split())
def readintl():
return list(readinti())
def readintt():
return tuple(readinti())
def readintll(k):
return [readintl() for _ in range(k)]
def readinttl(k):
return [readintt() for _ in range(k)]
def log(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
if __name__ == '__main__':
main()
``` | instruction | 0 | 1,298 | 14 | 2,596 |
No | output | 1 | 1,298 | 14 | 2,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.
For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.
Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.
Help Vasya find the maximum number of photos he is able to watch during T seconds.
Input
The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.
Second line of the input contains a string of length n containing symbols 'w' and 'h'.
If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.
If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.
Output
Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
Examples
Input
4 2 3 10
wwhw
Output
2
Input
5 2 4 13
hhwhh
Output
4
Input
5 2 4 1000
hhwhh
Output
5
Input
3 1 100 10
whw
Output
0
Note
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
Submitted Solution:
```
import bisect
def preview(n, a, b, t, S):
if S[0]:
t -= b
S[0] = False
else:
t -= 1
if t < 0:
return 0
R = []
s = 0
for i in range(1, n):
s += a + (b if S[i] else 1)
if s > t:
break
R.append(s)
else:
return n
L = []
s = 0
for i in reversed(range(1, n)):
s += a + (b if S[i] else 1)
if s > t:
break
L.append(s)
m = 1 + max(len(R), len(L))
t1 = t
for i in range(len(R)):
t1 -= R[i] + a
if t1 < 0:
break
j = bisect.bisect_right(L, t1) - 1
if j < 0:
break
m = max(m, i + j + 3)
t1 = t
for i in range(len(L)):
t1 -= L[i] + a
if t1 < 0:
break
j = bisect.bisect_right(R, t1) - 1
if j < 0:
break
m = max(m, i + j + 3)
return m
def main():
n, a, b, t = readinti()
S = [c == 'w' for c in input()]
print(preview(n, a, b, t, S))
##########
import sys
def readint():
return int(input())
def readinti():
return map(int, input().split())
def readintl():
return list(readinti())
def readintt():
return tuple(readinti())
def readintll(k):
return [readintl() for _ in range(k)]
def readinttl(k):
return [readintt() for _ in range(k)]
def log(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
if __name__ == '__main__':
main()
``` | instruction | 0 | 1,299 | 14 | 2,598 |
No | output | 1 | 1,299 | 14 | 2,599 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.
For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.
Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.
Help Vasya find the maximum number of photos he is able to watch during T seconds.
Input
The first line of the input contains 4 integers n, a, b, T (1 ≤ n ≤ 5·105, 1 ≤ a, b ≤ 1000, 1 ≤ T ≤ 109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.
Second line of the input contains a string of length n containing symbols 'w' and 'h'.
If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.
If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.
Output
Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
Examples
Input
4 2 3 10
wwhw
Output
2
Input
5 2 4 13
hhwhh
Output
4
Input
5 2 4 1000
hhwhh
Output
5
Input
3 1 100 10
whw
Output
0
Note
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
Submitted Solution:
```
#! /usr/bin/env python3
def main():
# n, a, b, t = map(int, input().split())
# oris = input()
s1 = "5 2 4 1000"
s2 = "hhwhh"
n, a, b, t = map(int, s1.split())
oris = s2
def get_time(front, rear, count_rot):
span = front - rear
offset = min(front, -rear)
return span, span * a + (span + 1) + offset * a + count_rot * b
front = rear = span = count_rot = new_count_rot = time = 0
has_one = False
for i in range(0, -n, -1):
if oris[i] == 'w':
new_count_rot += 1
new_span, new_time = get_time(front, i, new_count_rot)
if new_time > t:
break
has_one = True
span, time, rear, count_rot = new_span, new_time, i, new_count_rot
if not has_one:
return 0
maxi = max_span = n - 1
while front < maxi and rear <= 0 and span != max_span:
front += 1
if oris[front] == 'w':
count_rot += 1
while True:
new_span, time = get_time(front, rear, count_rot)
if time <= t:
break
if oris[rear] == 'w':
count_rot -= 1
rear += 1
if rear > 0:
return span + 1
span = max(new_span, span)
return span + 1
print(main())
``` | instruction | 0 | 1,300 | 14 | 2,600 |
No | output | 1 | 1,300 | 14 | 2,601 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs. | instruction | 0 | 1,695 | 14 | 3,390 |
Tags: greedy, implementation, two pointers
Correct Solution:
```
m, k, n, s = map(int, input().split())
fl = [int(x) for x in input().split()]
seq = [int(x) for x in input().split()]
d = {}
p = {}
werwerwerwerw=0
for i in seq:
d[i] = d.get(i, 0) + 1
p[i] = d[i]
f = False
collect = s
keys = set(d.keys())
l = 0
r = -1
c = 0
while r != m:
if collect:
r += 1
if r == m:
break
if fl[r] in keys:
d[fl[r]] -= 1
if d[fl[r]] >= 0:
collect -= 1
else:
l += 1
if fl[l - 1] in keys:
d[fl[l - 1]] += 1
if d[fl[l - 1]] > 0:
collect += 1
out = l % k + (r - l + 1 - k)
if not collect and n * k <= m - out:
f = True
print(max(0, out))
for j in range(l, r + 1):
if c == max(0, out):
break
if fl[j] not in keys or p[fl[j]] == 0:
print(j + 1, end=' ')
c += 1
else:
p[fl[j]] -= 1
break
if not f:
print(-1)
``` | output | 1 | 1,695 | 14 | 3,391 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs. | instruction | 0 | 1,696 | 14 | 3,392 |
Tags: greedy, implementation, two pointers
Correct Solution:
```
# Your circuit's dead, there's something wrong!
# Can you hear me, Major Tom?...
m, k, n, s = map(int, input().split())
fl = [int(x) for x in input().split()]
seq = [int(x) for x in input().split()]
d = {}
p = {}
for i in seq:
d[i] = d.get(i, 0) + 1
p[i] = d[i]
f = False
collect = s
keys = set(d.keys())
l = 0
r = -1
c = 0
while r != m:
if collect:
r += 1
if r == m:
break
if fl[r] in keys:
d[fl[r]] -= 1
if d[fl[r]] >= 0:
collect -= 1
else:
l += 1
if fl[l - 1] in keys:
d[fl[l - 1]] += 1
if d[fl[l - 1]] > 0:
collect += 1
out = l % k + (r - l + 1 - k)
if not collect and n * k <= m - out:
f = True
print(max(0, out))
for j in range(l, r + 1):
if c == max(0, out):
break
if fl[j] not in keys or p[fl[j]] == 0:
print(j + 1, end=' ')
c += 1
else:
p[fl[j]] -= 1
break
if not f:
print(-1)
``` | output | 1 | 1,696 | 14 | 3,393 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs. | instruction | 0 | 1,697 | 14 | 3,394 |
Tags: greedy, implementation, two pointers
Correct Solution:
```
from sys import stdin, stdout, setrecursionlimit
input = stdin.readline
# import string
# characters = string.ascii_lowercase
# digits = string.digits
# setrecursionlimit(int(1e6))
# dir = [-1,0,1,0,-1]
# moves = 'NESW'
inf = float('inf')
from functools import cmp_to_key
from collections import defaultdict as dd
from collections import Counter, deque
from heapq import *
import math
from math import floor, ceil, sqrt
def geti(): return map(int, input().strip().split())
def getl(): return list(map(int, input().strip().split()))
def getis(): return map(str, input().strip().split())
def getls(): return list(map(str, input().strip().split()))
def gets(): return input().strip()
def geta(): return int(input())
def print_s(s): stdout.write(s+'\n')
def solve():
m, k, n, s = geti()
a = getl()
s = Counter(getl())
length = k + m - n * k
val = sum(s.values())
occur = dd(int)
ok = 0
i = 0
done = 0
start = 0
# print(length)
while i < m:
while done != length and i < m:
occur[a[i]] += 1
if occur[a[i]] == s[a[i]]:
ok += 1
done += 1
i += 1
# print(done, start, ok)
if ok == len(s):
res = []
need = length - k
for j in range(start, start + length):
if not need:
break
if occur[a[j]] > s[a[j]]:
occur[a[j]] -= 1
res.append(j+1)
need -= 1
print(len(res))
print(*res)
break
else:
waste = k
while waste:
if occur[a[start]] == s[a[start]]:
ok -= 1
occur[a[start]] -= 1
waste -= 1
start += 1
done -= 1
else:
print(-1)
# def check(length):
# occur = dd(int)
# ok = 0
# prev = 0
# start = m-1
# for i in range(m):
# if a[i] in s:
# start = i
# break
# prev += 1
# prev %= k
# for i in range(start, m):
# occur[a[i]] += 1
# if occur[a[i]] == s[a[i]]:
# ok += 1
# if ok == len(s):
# # print(start, prev, i)
# total = i - start + 1
# to_rem = total - k + prev
# if to_rem <= 0:
# return []
# if to_rem <= length:
# res = []
# for j in range(start-1, -1, -1):
# if prev:
# res.append(j + 1)
# prev -= 1
# to_rem -= 1
# else:
# break
# for j in range(start, i):
# if occur[a[j]] > s[a[j]]:
# res.append(j + 1)
# to_rem -= 1
# if not to_rem:
# break
# return res
# else:
# while start < i:
# occur[a[start]] -= 1
# prev += 1
# prev %= k
# start += 1
# if a[start-1] in s and occur[a[start-1]] < s[a[start-1]]:
# ok -= 1
# if a[start] in s:
# break
# # print(a[start:])
# # print(Counter(a[start:]))
# # print(s)
# return -1
#
# res = check(length)
# if res == -1:
# print(res)
# else:
# print(len(res))
# if res:
# print(*res)
if __name__=='__main__':
solve()
``` | output | 1 | 1,697 | 14 | 3,395 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs. | instruction | 0 | 1,698 | 14 | 3,396 |
Tags: greedy, implementation, two pointers
Correct Solution:
```
import sys
from collections import Counter
input = sys.stdin.buffer.readline
m,k,n,s = map(int,input().split())
a = list(map(int,input().split()))
b = Counter(map(int,input().split()))
counts = Counter()
unsatisfied = len(b)
to_remove = m+1
mn_len = m+1
remove_idx = -1
j = 0
for i in range(m):
while j < m and unsatisfied != 0:
counts[a[j]] += 1
if counts[a[j]] == b[a[j]]:
unsatisfied -= 1
j += 1
if unsatisfied == 0:
curr_remove = i % k + max(0, j - i - k)
if curr_remove < to_remove:
to_remove = curr_remove
mn_len = j - i
remove_idx = i
if counts[a[i]] == b[a[i]]:
unsatisfied += 1
counts[a[i]] -= 1
if m - to_remove < n*k:
print(-1)
else:
print(to_remove)
indexes = {}
for i in range(remove_idx,remove_idx + mn_len):
if a[i] in indexes:
indexes[a[i]].append(i+1)
else:
indexes[a[i]] = [i+1]
removed = list(range(1,remove_idx % k + 1))
to_remove -= remove_idx % k
for i in indexes:
if to_remove == 0:
break
else:
removed.extend(indexes[i][:min(len(indexes[i]) - b[i],to_remove)])
to_remove -= min(len(indexes[i]) - b[i],to_remove)
print(*removed)
``` | output | 1 | 1,698 | 14 | 3,397 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs. | instruction | 0 | 1,699 | 14 | 3,398 |
Tags: greedy, implementation, two pointers
Correct Solution:
```
def main():
m, k, n, s = map(int, input().split())
a = list(map(int, input().split())) # prevbug: input in a line!
b = list(map(int, input().split())) # prevbug: convert to list
b_dict = {}
for x in b:
b_dict.setdefault(x, 0)
b_dict[x] += 1 # prevbug: b or b_dict
left = 0
right = 0
max_cut = m - n * k
condition_not_met = len(b_dict)
a_dict = {}
while right < m and condition_not_met > 0:
x = a[right]
a_dict.setdefault(x, 0)
a_dict[x] += 1
if x in b_dict and a_dict[x] == b_dict[x]:
condition_not_met -= 1
right += 1 # prevbug: ftl
if condition_not_met > 0:
print(-1)
return
def num_to_remove(lft, rgt):
lft = lft // k * k
num_in_seq = rgt - lft
if num_in_seq < k:
return 0 # prevbug: if sequence is shorter than k, then no need to remove flowers
return num_in_seq - k
def test_plan():
nonlocal left
if num_to_remove(left, right) <= max_cut:
tot = num_to_remove(left, right)
print(tot)
left = left // k * k
while tot > 0:
x = a[left]
if x in b_dict:
b_dict[x] -= 1
if b_dict[x] == 0:
del b_dict[x]
else:
print(left + 1, end=' ')
tot -= 1 # prevbug: ftl
left += 1
return True
return False
while True:
while left < right: # prevbug: should shift left before shifting right
x = a[left]
if x in b_dict and a_dict[x] - 1 < b_dict[x]:
break
else:
a_dict[x] -= 1
if a_dict[x] == 0:
del a_dict[x] # prevbug: ftl
left += 1
if test_plan():
return
if right < m:
a_dict.setdefault(a[right], 0)
a_dict[a[right]] += 1
right += 1
else:
break
print(-1)
if __name__ == '__main__':
main()
``` | output | 1 | 1,699 | 14 | 3,399 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs. | instruction | 0 | 1,700 | 14 | 3,400 |
Tags: greedy, implementation, two pointers
Correct Solution:
```
from math import *
import os, sys
from decimal import Decimal as db
from bisect import *
from io import BytesIO
from queue import Queue
from heapq import *
#input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
m, k, n, s = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
p = {}
kol = s
for c in b:
#kol += int(d.get(c, 0) == 0)
d[c] = d.get(c, 0) + 1
p[c] = d[c]
l = 0
r = -1
c = 0
while r < m:
if kol:
r += 1
if r == m:
break
if a[r] in d:
d[a[r]] -= 1
if d[a[r]] >= 0:
kol -= 1
else:
l += 1
if a[l - 1] in d:
d[a[l - 1]] += 1
if d[a[l - 1]] > 0:
kol += 1
tmp = l % k + r - l - k + 1
if not kol and n * k <= m - tmp:
print(max(0, tmp))
for i in range(l, r + 1):
if c == max(0, tmp):
exit(0)
if a[i] not in d or p[a[i]] == 0:
print(i + 1, end = ' ')
c += 1
else:
p[a[i]] -= 1
exit(0)
print(-1)
``` | output | 1 | 1,700 | 14 | 3,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs. | instruction | 0 | 1,701 | 14 | 3,402 |
Tags: greedy, implementation, two pointers
Correct Solution:
```
m, k, n, s = map(int, input().split())
ls = list(map(int, input().split()))
seq = map(int, input().split())
dseq = {}
for se in seq:
if se not in dseq: dseq[se] = 0
dseq[se]+=1
maxwindowsize = m-k*n + k
dcurr = {}
nrusefull = 0
for i in range(maxwindowsize-k):
if ls[i] not in dcurr: dcurr[ls[i]] = 0
dcurr[ls[i]] +=1
if ls[i] in dseq and dseq[ls[i]] >= dcurr[ls[i]]: nrusefull+=1
# found it
for j in range(m):
# print(j%k, j+k)
if j % k == 0 and j+k<=m:
mini = maxwindowsize+j-k
# print(mini, mini+k)
for i in range(mini, min(mini+k, m)):
if ls[i] not in dcurr: dcurr[ls[i]] = 0
dcurr[ls[i]] +=1
if ls[i] in dseq and dseq[ls[i]] >= dcurr[ls[i]]: nrusefull+=1
if nrusefull == s:
printl = []
dlast = {x:0 for x in dcurr}
for x in range(j, j+maxwindowsize):
if len(printl) == maxwindowsize-k: break
dlast[ls[x]] +=1
if ls[x] not in dseq or dseq[ls[x]] < dlast[ls[x]]:
printl.append(str(x+1))
break
dcurr[ls[j]] -=1
if ls[j] in dseq and dcurr[ls[j]] < dseq[ls[j]]: nrusefull-=1
else:
print(-1)
raise SystemExit()
print(len(printl))
print(" ".join(printl))
``` | output | 1 | 1,701 | 14 | 3,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs.
Submitted Solution:
```
def main():
m, k, n, s = map(int, input().split())
a = list(map(int, input().split())) # prevbug: input in a line!
b = list(map(int, input().split())) # prevbug: convert to list
b_dict = {}
for x in b:
b_dict.setdefault(x, 0)
b_dict[x] += 1 # prevbug: b or b_dict
left = 0
right = 0
max_cut = m - n * k
condition_not_met = len(b_dict)
a_dict = {}
while right < m and condition_not_met > 0:
x = a[right]
a_dict.setdefault(x, 0)
a_dict[x] += 1
if x in b_dict and a_dict[x] == b_dict[x]:
condition_not_met -= 1
right += 1 # prevbug: ftl
if condition_not_met > 0:
print(-1)
return
def num_to_remove(lft, rgt):
lft = lft // k * k
num_in_seq = rgt - lft
return num_in_seq - k
def test_plan():
nonlocal left
if num_to_remove(left, right) <= max_cut:
tot = num_to_remove(left, right)
print(tot)
left = left // k * k
while tot > 0:
x = a[left]
if x in b_dict:
b_dict[x] -= 1
if b_dict[x] == 0:
del b_dict[x]
else:
print(left + 1, end=' ')
tot -= 1 # prevbug: ftl
left += 1
return True
return False
while True:
while left < right: # prevbug: should shift left before shifting right
x = a[left]
if x in b_dict and a_dict[x] - 1 < b_dict[x]:
break
else:
a_dict[x] -= 1
if a_dict[x] == 0:
del a_dict[x] # prevbug: ftl
left += 1
if test_plan():
return
if right < m:
a_dict.setdefault(a[right], 0)
a_dict[a[right]] += 1
right += 1
else:
break
print(-1)
if __name__ == '__main__':
main()
``` | instruction | 0 | 1,702 | 14 | 3,404 |
No | output | 1 | 1,702 | 14 | 3,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs.
Submitted Solution:
```
m, k, n, s = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = dict()
counts = dict()
for i in b:
if i in counts:
counts[i] += 1
else:
counts[i] = 1
left, r = 0, k + m - n * k
for i in a:
d[i] = 0
for i in b:
d[i] = 0
ok = False
ans = []
while r <= m:
d.clear()
for i in range(left, r):
if a[i] in d:
d[a[i]] += 1
else:
d[a[i]] = 1
ok = True
for i in counts.keys():
if i not in d or d[i] < counts[i]:
ok = False
break
if ok:
break
left += k
r = min(r + k, m + 1)
if not ok:
print(-1)
exit()
cur = m - n * k
for i in range(left, r):
if cur == 0:
break
if a[i] not in counts or counts[a[i]] == 0:
ans.append(str(i + 1))
cur -= 1
print(len(ans))
print(' '.join(ans))
``` | instruction | 0 | 1,703 | 14 | 3,406 |
No | output | 1 | 1,703 | 14 | 3,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs.
Submitted Solution:
```
def All(have, need):
res = True
for flower in need.keys():
if (flower not in have.keys()) or (have[flower] < need[flower]):
res = False
break
return res
m, k, n, s = map(int, input().split())
flower = list(map(int, input().split()))
need = {}
need_lst = list(map(int, input().split()))
for fl in need_lst:
if fl in need.keys():
need[fl] += 1
else:
need[fl] = 1
d = -1
have = {}
r = 0
for l in range(m):
while not All(have, need) and r < m:
if flower[r] in need.keys():
if flower[r] in have.keys():
have[flower[r]] += 1
else:
have[flower[r]] = 1
r += 1
r -= 1
#print(have, need, l, r, All(have, need))
if r == m:
break
if (l//k + (m-1-r)//k >= n-1) and (r-l+1 >= k) and (have != {}):
del_list = []
i = l
ln = r - l + 1
while ln > k and i <= r:
if flower[i] not in need.keys():
del_list.append(i+1)
ln -= 1
if (flower[i] in need.keys()) and (have[flower[i]] > need[flower[i]]):
have[flower[i]] -= 1
del_list.append(i+1)
ln -= 1
i += 1
i = 0
while l % k != 0: #and l <= (n-1)*k
del_list.append(i+1)
l -= 1
i += 1
d = len(del_list)
break
if l < m-1:
if flower[l] in have.keys():
have[flower[l]] -= 1
if have[flower[l]] == 0:
have.pop(flower[l])
print(d)
if d != 0 and d != -1:
print(' '.join(map(str, del_list)))
'''
13 4 1 3
3 3 3 3 3 4 3 7 1 3 3 2 4
4 3 4
'''
``` | instruction | 0 | 1,704 | 14 | 3,408 |
No | output | 1 | 1,704 | 14 | 3,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
At the first holiday in spring, the town Shortriver traditionally conducts a flower festival. Townsfolk wear traditional wreaths during these festivals. Each wreath contains exactly k flowers.
The work material for the wreaths for all n citizens of Shortriver is cut from the longest flowered liana that grew in the town that year. Liana is a sequence a_1, a_2, ..., a_m, where a_i is an integer that denotes the type of flower at the position i. This year the liana is very long (m ≥ n ⋅ k), and that means every citizen will get a wreath.
Very soon the liana will be inserted into a special cutting machine in order to make work material for wreaths. The machine works in a simple manner: it cuts k flowers from the beginning of the liana, then another k flowers and so on. Each such piece of k flowers is called a workpiece. The machine works until there are less than k flowers on the liana.
Diana has found a weaving schematic for the most beautiful wreath imaginable. In order to weave it, k flowers must contain flowers of types b_1, b_2, ..., b_s, while other can be of any type. If a type appears in this sequence several times, there should be at least that many flowers of that type as the number of occurrences of this flower in the sequence. The order of the flowers in a workpiece does not matter.
Diana has a chance to remove some flowers from the liana before it is inserted into the cutting machine. She can remove flowers from any part of the liana without breaking liana into pieces. If Diana removes too many flowers, it may happen so that some of the citizens do not get a wreath. Could some flowers be removed from the liana so that at least one workpiece would conform to the schematic and machine would still be able to create at least n workpieces?
Input
The first line contains four integers m, k, n and s (1 ≤ n, k, m ≤ 5 ⋅ 10^5, k ⋅ n ≤ m, 1 ≤ s ≤ k): the number of flowers on the liana, the number of flowers in one wreath, the amount of citizens and the length of Diana's flower sequence respectively.
The second line contains m integers a_1, a_2, ..., a_m (1 ≤ a_i ≤ 5 ⋅ 10^5) — types of flowers on the liana.
The third line contains s integers b_1, b_2, ..., b_s (1 ≤ b_i ≤ 5 ⋅ 10^5) — the sequence in Diana's schematic.
Output
If it's impossible to remove some of the flowers so that there would be at least n workpieces and at least one of them fullfills Diana's schematic requirements, output -1.
Otherwise in the first line output one integer d — the number of flowers to be removed by Diana.
In the next line output d different integers — the positions of the flowers to be removed.
If there are multiple answers, print any.
Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
Note
In the first example, if you don't remove any flowers, the machine would put out two workpieces with flower types [1, 2, 3] and [3, 2, 1]. Those workpieces don't fit Diana's schematic. But if you remove flower on 4-th place, the machine would output workpieces [1, 2, 3] and [2, 1, 2]. The second workpiece fits Diana's schematic.
In the second example there is no way to remove flowers so that every citizen gets a wreath and Diana gets a workpiece that fits here schematic.
In the third example Diana is the only citizen of the town and that means she can, for example, just remove all flowers except the ones she needs.
Submitted Solution:
```
x=list(map(int,input('').split()))
y=list(map(int,input('').split()))
z=list(map(int,input('').split()))
sss=set(z)
di={i:0 for i in sss}
for qwer in z:
di[qwer]+=1
m,k,n,s=x[0],x[1],x[2],x[3] # цветов, в цикле
ost=m-n*k
q=[y[i*k:(i+1)*k+ost] for i in range(m)]
ttt=-1
ans1=0
ans2=[]
for i in range(len(q)):
count=0
if len(q[i])<k:
continue
dis={j:0 for j in (q[i]+list(sss))}
for j in q[i]:
dis[j]+=1
for j in sss:
if di[j]>dis[j]:
count=-1
if count==-1:
continue
else:
ttt=0
count1=k+ost
while len(q[i])!=k:
count1-=1
if not q[i][count1] in sss:
print(q[i][count1])
q[i]=q[i][0:count1]+q[i][count1+1:]
ans1+=1
ans2.append(i*k+count1)
elif dis[q[i][count1]]>di[q[i][count1]]:
print(q[i][count1])
dis[q[i][count1]]-=1
q[i]=q[i][0:count1]+q[i][count1+1:]
ans1+=1
ans2.append(i*k+count1)
break
if ttt==-1:
print(-1)
else:
print(ans1)
ans2=[go+1 for go in ans2]
print(' '.join(list(map(str,ans2))))
``` | instruction | 0 | 1,705 | 14 | 3,410 |
No | output | 1 | 1,705 | 14 | 3,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:
* Employee A is the immediate manager of employee B
* Employee B has an immediate manager employee C such that employee A is the superior of employee C.
The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.
Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.
What is the minimum number of groups that must be formed?
Input
The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees.
The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.
It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles.
Output
Print a single integer denoting the minimum number of groups that will be formed in the party.
Examples
Input
5
-1
1
2
1
-1
Output
3
Note
For the first example, three groups are sufficient, for example:
* Employee 1
* Employees 2 and 4
* Employees 3 and 5 | instruction | 0 | 1,710 | 14 | 3,420 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
n = int(input())
l = [int(input())-1 for i in range(n)]
cc = 0
for i in range(n):
c = 0
while i != -2:
i = l[i]
c += 1
cc = max(c,cc)
print(cc)
``` | output | 1 | 1,710 | 14 | 3,421 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:
* Employee A is the immediate manager of employee B
* Employee B has an immediate manager employee C such that employee A is the superior of employee C.
The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.
Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.
What is the minimum number of groups that must be formed?
Input
The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees.
The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.
It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles.
Output
Print a single integer denoting the minimum number of groups that will be formed in the party.
Examples
Input
5
-1
1
2
1
-1
Output
3
Note
For the first example, three groups are sufficient, for example:
* Employee 1
* Employees 2 and 4
* Employees 3 and 5 | instruction | 0 | 1,711 | 14 | 3,422 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
n=int(input())
a=[]
for i in range(n):
a.append(int(input())-1)
ans=0
for i in range(len(a)):
L=0
while i>-2:
i=a[i]
L+=1
ans=max(ans,L)
print(ans)
``` | output | 1 | 1,711 | 14 | 3,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:
* Employee A is the immediate manager of employee B
* Employee B has an immediate manager employee C such that employee A is the superior of employee C.
The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.
Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.
What is the minimum number of groups that must be formed?
Input
The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees.
The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.
It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles.
Output
Print a single integer denoting the minimum number of groups that will be formed in the party.
Examples
Input
5
-1
1
2
1
-1
Output
3
Note
For the first example, three groups are sufficient, for example:
* Employee 1
* Employees 2 and 4
* Employees 3 and 5 | instruction | 0 | 1,712 | 14 | 3,424 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n = I()
p = [I() for _ in range(n)]
fm = {}
fm[-1] = 0
def f(i):
if i in fm:
return fm[i]
t = f(p[i-1]) + 1
fm[i] = t
return t
r = 0
for i in range(1,n+1):
t = f(i)
if r < t:
r = t
return r
print(main())
``` | output | 1 | 1,712 | 14 | 3,425 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:
* Employee A is the immediate manager of employee B
* Employee B has an immediate manager employee C such that employee A is the superior of employee C.
The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.
Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.
What is the minimum number of groups that must be formed?
Input
The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees.
The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.
It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles.
Output
Print a single integer denoting the minimum number of groups that will be formed in the party.
Examples
Input
5
-1
1
2
1
-1
Output
3
Note
For the first example, three groups are sufficient, for example:
* Employee 1
* Employees 2 and 4
* Employees 3 and 5 | instruction | 0 | 1,713 | 14 | 3,426 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():
return map(int,input().split())
def array():
return [int(i) for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
ind=[]
def dfs(adj,s,vis,c):
global ind
ind.append(c)
vis[s] = True
for i in adj[s]:
if vis[i] == False:
dfs(adj,i,vis,c+1)
adj=defaultdict(list)
n=int(input())
ar=[]
for i in range(n):
a=int(input())
ar.append(a)
if(a!=-1):
adj[a].append(i+1)
adj[i+1].append(a)
#print(adj)
ans=0
vis={i:False for i in range(1,n+1)}
for i in range(1,n+1):
if(ar[i-1]==-1 and vis[i]==False):
dfs(adj,i,vis,0)
print(max(ind)+1)
``` | output | 1 | 1,713 | 14 | 3,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:
* Employee A is the immediate manager of employee B
* Employee B has an immediate manager employee C such that employee A is the superior of employee C.
The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.
Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.
What is the minimum number of groups that must be formed?
Input
The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees.
The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.
It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles.
Output
Print a single integer denoting the minimum number of groups that will be formed in the party.
Examples
Input
5
-1
1
2
1
-1
Output
3
Note
For the first example, three groups are sufficient, for example:
* Employee 1
* Employees 2 and 4
* Employees 3 and 5 | instruction | 0 | 1,714 | 14 | 3,428 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
import sys
import math
import collections
import heapq
import decimal
input=sys.stdin.readline
n=int(input())
l=[]
for i in range(n):
l.append(int(input()))
m=0
for i in range(n):
c=0
while(i>-1):
c+=1
i=l[i]-1
m=max(m,c)
print(m)
``` | output | 1 | 1,714 | 14 | 3,429 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.