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.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | instruction | 0 | 104,103 | 14 | 208,206 |
Tags: dfs and similar, math
Correct Solution:
```
from fractions import gcd
def nok(a,b):
return a*b//gcd(a,b)
n=int(input())
a=[0]+[int(i) for i in input().split()]
ans=1
for i in range(1,1+n):
if a[i]!=i:
now=i
res=-1
for j in range(n):
now=a[now]
if now==i:
res=j+1
if res%2==0:
res//=2
ans=nok(ans,res)
break
else:
ans=-1
break
print(ans)
``` | output | 1 | 104,103 | 14 | 208,207 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | instruction | 0 | 104,104 | 14 | 208,208 |
Tags: dfs and similar, math
Correct Solution:
```
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
n = int(input())
crash = list(map(int, input().split()))
s = set()
t = 1
for i in range(n):
counter = 1
j = crash[i] - 1
while j != i and counter <= n:
counter += 1
j = crash[j] - 1
if counter > n:
t = -1
break
else:
#print(counter)
if counter % 2 == 0:
counter //= 2
t = (t * counter) // gcd(t, counter)
print(t)
``` | output | 1 | 104,104 | 14 | 208,209 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | instruction | 0 | 104,105 | 14 | 208,210 |
Tags: dfs and similar, math
Correct Solution:
```
n = int(input())
adj = [int(x) - 1 for x in input().split()]
ans = 1
from fractions import gcd
lcm = lambda x, y : (x * y) // gcd(x, y)
for i in range(n):
visited = [0] * n
visited[i] = 1
j = adj[i]
while visited[j] == 0:
visited[j] = 1
j = adj[j]
if i != j:
print(-1)
exit()
else:
sm = sum(visited)
if sm & 1:
ans = lcm(ans, sum(visited))
else:
ans = lcm(ans, sm // 2)
print(ans)
``` | output | 1 | 104,105 | 14 | 208,211 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | instruction | 0 | 104,106 | 14 | 208,212 |
Tags: dfs and similar, math
Correct Solution:
```
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def solve():
n = int(input())
arr = list(map(int, input().split()))
memarr = []
for i in range(n):
arr[i] -= 1
for i in range(n):
if arr[i] == -1:
continue
mem = 1
nx = arr[i]
arr[i] = -1
while(arr[nx] != -1):
t = arr[nx]
arr[nx] = -1
nx = t
mem += 1
if(nx == i):
memarr.append(mem)
else:
print(-1)
return
for i in range(len(memarr)):
if memarr[i] % 2 == 0:
memarr[i] //= 2
res = 1
for i in memarr:
res = res * i // gcd(res, i)
print(res)
solve()
``` | output | 1 | 104,106 | 14 | 208,213 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | instruction | 0 | 104,107 | 14 | 208,214 |
Tags: dfs and similar, math
Correct Solution:
```
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
n = int(input())
a = list(map(int, input().split()))
for i in range(len(a)):
a[i] -= 1
lens = []
for i in range(len(a)):
now = i
l = 0
for j in range(3 * n):
l += 1
now = a[now]
if now == i:
break
if l == 3 * n:
print(-1)
exit(0)
if l % 2 == 0:
l //= 2
lens.append(l)
res = lens[0]
for i in range(1, len(lens)):
res = res * lens[i] // gcd(res, lens[i])
print(res)
``` | output | 1 | 104,107 | 14 | 208,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | instruction | 0 | 104,108 | 14 | 208,216 |
Tags: dfs and similar, math
Correct Solution:
```
import sys, math
n, c = int(input()), list(map(int, input().split()))
n += 1
c.insert(0, 0)
done, t = [False] * n, []
for i in range(1, n):
a, ct = i, 0
while not done[a]:
done[a] = True
ct, a = ct + 1, c[a]
if a is not i:
print(-1)
sys.exit(0)
if ct > 0:
if ct & 1 is 0:
ct //= 2
t.append(int(ct))
m = t[0]
for i in range(1, len(t)):
g = math.gcd(m, t[i])
m = m * t[i] // g
print(m)
``` | output | 1 | 104,108 | 14 | 208,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | instruction | 0 | 104,109 | 14 | 208,218 |
Tags: dfs and similar, math
Correct Solution:
```
def gcd(a,b):
while b:
a,b=b,a%b
return a
def lcm(arr):
if len(arr)==1:
return arr[0]
else:
a=arr.pop()
b=arr.pop()
arr.append(a*b//gcd(a,b))
return lcm(arr)
n=int(input())
lv=[(int(z)-1) for z in input().split()]
tbl=set(lv)
if len(tbl)==n:
cntlcm=[]
vv=[0]*n
i=0
while i<n:
if vv[i]:
i+=1
else:
k=i
cnt=0
while not vv[k]:
cnt+=1
vv[k]=1
k=lv[k]
if cnt%2:
cntlcm.append(cnt)
else:
cntlcm.append(cnt//2)
print(lcm(cntlcm))
else:
print(-1)
``` | output | 1 | 104,109 | 14 | 208,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | instruction | 0 | 104,110 | 14 | 208,220 |
Tags: dfs and similar, math
Correct Solution:
```
def make_function(n, crushes):
def f(original_node):
node = original_node
x = 0
for i in range(n):
node = crushes[node]
x += 1
if node == original_node:
break
else:
return -1
return x / 2 if x % 2 == 0 else x
return f
def lcd(numbers):
gcd = lambda x, y: int(x) if y == 0 else int(gcd(y, x % y))
ret = 1
for number in numbers:
ret = ret * number / gcd(ret, number)
return ret
def main():
n = int(input())
a = map(int, input().split())
crushes = {i: x for i, x in zip(range(1, n+1), a)}
f = make_function(n, crushes)
numbers = list(map(int, map(f, crushes)))
if -1 in numbers:
print (-1)
return
print (int(lcd(numbers)))
main()
``` | output | 1 | 104,110 | 14 | 208,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
Submitted Solution:
```
import sys, math
n, c = int(input()), list(map(int, input().split()))
n += 1
c.insert(0, 0)
done, t = [False] * n, []
for i in range(1, n):
a = i
ct = 0
while not done[a]:
ct += 1
done[a] = True
a = c[a]
if a is not i:
print(-1)
sys.exit(0)
if ct > 0:
if ct & 1 is 0:
ct /= 2
t.append(int(ct))
#print(i, ct)
g, m = t[0], t[0]
for i in range(1, len(t)):
g = math.gcd(m, t[i])
m = m * t[i] // g
#print(g, m)
print(m)
``` | instruction | 0 | 104,111 | 14 | 208,222 |
Yes | output | 1 | 104,111 | 14 | 208,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
Submitted Solution:
```
#from collections import deque
from functools import reduce
n = int(input())
crush = [int(i) - 1 for i in input().split()]
def parity_treat(n):
if n%2 == 0:
return n//2
else:
return n
def gcd(a,b):
while b:
a, b = b, a%b
return a
def lcm(a,b):
return a * b // gcd(a,b)
def lcmm(*args):
return reduce(lcm, args)
if len(set(crush)) < n:
print(-1)
else:
component_size = []
visited = set()
for i in range(n):
if i not in visited:
tmp = 1
start = i
visited.add(start)
j = crush[start]
while j != start:
visited.add(j)
j = crush[j]
tmp+=1
component_size.append(tmp)
component_size = [parity_treat(i) for i in component_size]
print(lcmm(*component_size))
``` | instruction | 0 | 104,112 | 14 | 208,224 |
Yes | output | 1 | 104,112 | 14 | 208,225 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
Submitted Solution:
```
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
n = int(input())
a = list(map(int, input().split()))
if sorted(a) != [i + 1 for i in range(n)]:
print(-1)
else:
ans = 1
used = [0 for i in range(n)]
for i in range(n):
if used[i] == 0:
j = i
am = 0
while used[j] == 0:
am += 1
used[j] = 1
j = a[j] - 1
if am % 2:
ans = lcm(ans, am)
else:
ans = lcm(ans, am // 2)
print(ans)
``` | instruction | 0 | 104,113 | 14 | 208,226 |
Yes | output | 1 | 104,113 | 14 | 208,227 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
Submitted Solution:
```
input()
crush = [0] + [int(x) for x in input().split()]
visited = set()
circle_sizes = []
def gcd(a, b):
return a if b == 0 else gcd(b, a%b)
def lcm(a, b):
return a * b // gcd(a, b)
def solve():
for i in range(len(crush)):
if i not in visited:
start, cur, count = i, i, 0
while cur not in visited:
visited.add(cur)
count += 1
cur = crush[cur]
if cur != start:
return -1
circle_sizes.append(count if count % 2 else count // 2)
if len(circle_sizes) == 1:
return circle_sizes[0]
ans = lcm(circle_sizes[0], circle_sizes[1])
for size in circle_sizes[2:]:
ans = lcm(ans, size)
return ans
print(solve())
``` | instruction | 0 | 104,114 | 14 | 208,228 |
Yes | output | 1 | 104,114 | 14 | 208,229 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
Submitted Solution:
```
def gcd(a, b):
while (b != 0):
a, b = b, a % b
return a
n = int(input())
crush = list(map(int, input().split()))
for i in range(n):
crush[i] -= 1
ans = True
s = set()
for i in range(n):
j = crush[i]
x = i
#now = i, crush[i] = where
#print(j, end = ' ')
c = 0
while (x != j):
#print(j, end = ' ')
if j != crush[j]:
j = crush[j]
else:
break
c += 1
if c:
ans = False
#print()
if c > 1:
c += 1
s.add(c)
if ans:
print(-1)
else:
if len(s) > 0:
nod = s.pop()
m = nod
for elem in s:
m *= elem
nod = gcd(elem, nod)
#print('ans')
print(m // nod)
else:
print(1)
``` | instruction | 0 | 104,115 | 14 | 208,230 |
No | output | 1 | 104,115 | 14 | 208,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
Submitted Solution:
```
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
n = int(input())
crash = list(map(int, input().split()))
s = set()
t2 = 0
t = -1
for i in range(n):
counter = 1
j = crash[i] - 1
while j != i and counter <= n:
counter += 1
j = crash[j] - 1
if counter > n:
t = -1
break
else:
#print(counter)
if counter == 2:
t = max(t, counter - 1)
t2 = 2
else:
s.add(counter)
#print(s)
t = max(t, counter)
#print(s)
if t == -1 or t == 1:
print(t)
else:
a = s.pop()
a1 = a
nod = 1
for elem in s:
a1 *= elem
nod = gcd(a, elem)
a = nod
a1 = a1//nod
if t2 != 0 and a1 % 2 == 0:
print(a1 * 2)
else:
print(a1)
``` | instruction | 0 | 104,116 | 14 | 208,232 |
No | output | 1 | 104,116 | 14 | 208,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
Submitted Solution:
```
#!/usr/bin/env python3
def main():
import fractions
def lcm(x, y):
return x // fractions.gcd(x, y) * y
try:
while True:
n = int(input())
a = list(map(int, input().split()))
t = [[-1] * n for i in range(n)]
for i in range(n):
for trg in range(n):
cur = i
used = set()
elapsed = 1
while cur != trg:
if cur in used:
break
used.add(cur)
cur = a[cur] - 1
elapsed += 1
else:
t[i][trg] = elapsed
def print_result():
result = 1
for i in range(n):
for j in range(n):
if (t[i][j] == -1) != (t[j][i] == -1):
print(-1)
return
result = lcm(result, lcm(t[i][j], t[j][i]))
print(result >> 1)
print_result()
except EOFError:
pass
main()
``` | instruction | 0 | 104,117 | 14 | 208,234 |
No | output | 1 | 104,117 | 14 | 208,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.
<image>
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t β₯ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
Input
The first line of input contains integer n (1 β€ n β€ 100) β the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 β€ crushi β€ n) β the number of i-th person's crush.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
for i in range(len(a)):
a[i] -= 1
def check(num):
colour = [ 0 for i in range(n)]
for i in range(len(a)):
if not colour[i]:
pos = i
for j in range(num):
pos = a[pos]
if pospath(pos, i, num):
colour[i], colour[pos] = 1, 1
res = 1
for i in range(len(colour)):
res *= colour[i]
return res == 1
def pospath(fr, to, length):
k = fr
for i in range(length):
k = a[k]
return to == k
for i in range(1, 101):
if check(i):
print(i)
exit()
print(-1)
``` | instruction | 0 | 104,118 | 14 | 208,236 |
No | output | 1 | 104,118 | 14 | 208,237 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Noora is a student of one famous high school. It's her final year in school β she is going to study in university next year. However, she has to get an Β«AΒ» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 β to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with Β«AΒ» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
Input
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ k) denoting marks received by Noora before Leha's hack.
Output
Print a single integer β minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
Examples
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
Note
Consider the first example testcase.
Maximal mark is 10, Noora received two marks β 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. | instruction | 0 | 104,120 | 14 | 208,240 |
Tags: implementation, math
Correct Solution:
```
n,k=list(map(int,input().split()))
p=list(map(int,input().split()))
tad=k
tom=(k-1)+0.5
lol=sum(p)
s=0
for x in range(55555555):
if (lol/n)>=tom:
print(s)
break
else:
n+=1
if ((lol+k)/(n))<tom:
k+=tad
s+=1
else:
print(s+1)
break
``` | output | 1 | 104,120 | 14 | 208,241 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Noora is a student of one famous high school. It's her final year in school β she is going to study in university next year. However, she has to get an Β«AΒ» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 β to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with Β«AΒ» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
Input
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ k) denoting marks received by Noora before Leha's hack.
Output
Print a single integer β minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
Examples
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
Note
Consider the first example testcase.
Maximal mark is 10, Noora received two marks β 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. | instruction | 0 | 104,121 | 14 | 208,242 |
Tags: implementation, math
Correct Solution:
```
a,b=map(int,input().split())
c=list(map(int,input().split()))
i=0
d=0
j=len(c)
for i in range(j):
d+=c[i]
p=((b-0.5)*a-d)*2
if(p<0):
print(0)
else:
print(int(p))
``` | output | 1 | 104,121 | 14 | 208,243 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Noora is a student of one famous high school. It's her final year in school β she is going to study in university next year. However, she has to get an Β«AΒ» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 β to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with Β«AΒ» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
Input
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ k) denoting marks received by Noora before Leha's hack.
Output
Print a single integer β minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
Examples
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
Note
Consider the first example testcase.
Maximal mark is 10, Noora received two marks β 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. | instruction | 0 | 104,122 | 14 | 208,244 |
Tags: implementation, math
Correct Solution:
```
n, k = map(int, input().split())
m = list(map(int, input().split()))
def av(m):
return sum(m)/len(m)
s = sum(m)
def ro(x):
xx = int(x)
if x - xx >= 0.5:
return xx + 1
else:
return xx
pren = n
while s/n < k - 0.5:
s+= k
n+=1
print(n - pren)
``` | output | 1 | 104,122 | 14 | 208,245 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Noora is a student of one famous high school. It's her final year in school β she is going to study in university next year. However, she has to get an Β«AΒ» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 β to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with Β«AΒ» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
Input
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ k) denoting marks received by Noora before Leha's hack.
Output
Print a single integer β minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
Examples
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
Note
Consider the first example testcase.
Maximal mark is 10, Noora received two marks β 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. | instruction | 0 | 104,123 | 14 | 208,246 |
Tags: implementation, math
Correct Solution:
```
from math import ceil
n,k=map(int,input().split())
li=list(map(int,input().split()))
count=0
count2=0
def round1(n):
k=int(n)
b=n-k
if b>=0.5:
return ceil(n)
return k
while round1(sum(li)/len(li))!=k:
li.append(k)
count+=1
print((count))
``` | output | 1 | 104,123 | 14 | 208,247 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Noora is a student of one famous high school. It's her final year in school β she is going to study in university next year. However, she has to get an Β«AΒ» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 β to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with Β«AΒ» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
Input
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ k) denoting marks received by Noora before Leha's hack.
Output
Print a single integer β minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
Examples
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
Note
Consider the first example testcase.
Maximal mark is 10, Noora received two marks β 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. | instruction | 0 | 104,124 | 14 | 208,248 |
Tags: implementation, math
Correct Solution:
```
n,k = map(int, input().split())
a = list(map(int, input().split()))
sumA = sum(a)
res = 0
while round(sumA / n + 1e-13) < k:
sumA += k
n += 1
res += 1
print(res)
``` | output | 1 | 104,124 | 14 | 208,249 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Noora is a student of one famous high school. It's her final year in school β she is going to study in university next year. However, she has to get an Β«AΒ» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 β to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with Β«AΒ» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
Input
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ k) denoting marks received by Noora before Leha's hack.
Output
Print a single integer β minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
Examples
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
Note
Consider the first example testcase.
Maximal mark is 10, Noora received two marks β 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. | instruction | 0 | 104,125 | 14 | 208,250 |
Tags: implementation, math
Correct Solution:
```
n,m = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
avg = sum(l)
t = 0
while avg < (t+n)*(m-0.5) :
avg = avg+m
t+=1
print(t)
``` | output | 1 | 104,125 | 14 | 208,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Noora is a student of one famous high school. It's her final year in school β she is going to study in university next year. However, she has to get an Β«AΒ» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 β to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with Β«AΒ» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
Input
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ k) denoting marks received by Noora before Leha's hack.
Output
Print a single integer β minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
Examples
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
Note
Consider the first example testcase.
Maximal mark is 10, Noora received two marks β 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. | instruction | 0 | 104,126 | 14 | 208,252 |
Tags: implementation, math
Correct Solution:
```
import math
n, k = map(int, input().split())
s = sum(map(int, input().split()))
print(max(0, (2 * k - 1) * n - 2 * s))
``` | output | 1 | 104,126 | 14 | 208,253 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Noora is a student of one famous high school. It's her final year in school β she is going to study in university next year. However, she has to get an Β«AΒ» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 β to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with Β«AΒ» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
Input
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ k) denoting marks received by Noora before Leha's hack.
Output
Print a single integer β minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
Examples
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
Note
Consider the first example testcase.
Maximal mark is 10, Noora received two marks β 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to <image>. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate. | instruction | 0 | 104,127 | 14 | 208,254 |
Tags: implementation, math
Correct Solution:
```
x,y=map(int,input().split())
a=list(map(int,input().split()))
n=0
d=0
for i in range(x):
n=n+a[i]
while round((n+0.01)/x)!=y:
n=n+y
x=x+1
d+=1
print(d)
``` | output | 1 | 104,127 | 14 | 208,255 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, β¦, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company.
The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, β¦, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set.
In other words, the first company can present any subset of elements from \\{a_1, a_2, β¦, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, β¦, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of elements discovered by ChemForces.
The i-th of the next n lines contains two integers a_i and x_i (1 β€ a_i β€ 10^9, 1 β€ x_i β€ 10^9) β the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct.
The next line contains a single integer m (1 β€ m β€ 10^5) β the number of chemicals invented by TopChemist.
The j-th of the next m lines contains two integers b_j and y_j, (1 β€ b_j β€ 10^9, 1 β€ y_j β€ 10^9) β the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Input
1
1000000000 239
3
14 15
92 65
35 89
Output
408
Note
In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24.
In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. | instruction | 0 | 104,171 | 14 | 208,342 |
Tags: sortings
Correct Solution:
```
n=int(input())
d=dict()
for i in range(n):
a,b=list(map(int,input().split()))
try:
d[a]=max(d[a],b)
except:
d.update({a:b})
m=int(input())
for i in range(m):
a,b=list(map(int,input().split()))
try:
d[a]=max(d[a],b)
except:
d.update({a:b})
ans=0
for i in d:
ans+=d[i]
print(ans)
``` | output | 1 | 104,171 | 14 | 208,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, β¦, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company.
The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, β¦, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set.
In other words, the first company can present any subset of elements from \\{a_1, a_2, β¦, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, β¦, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of elements discovered by ChemForces.
The i-th of the next n lines contains two integers a_i and x_i (1 β€ a_i β€ 10^9, 1 β€ x_i β€ 10^9) β the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct.
The next line contains a single integer m (1 β€ m β€ 10^5) β the number of chemicals invented by TopChemist.
The j-th of the next m lines contains two integers b_j and y_j, (1 β€ b_j β€ 10^9, 1 β€ y_j β€ 10^9) β the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Input
1
1000000000 239
3
14 15
92 65
35 89
Output
408
Note
In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24.
In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. | instruction | 0 | 104,172 | 14 | 208,344 |
Tags: sortings
Correct Solution:
```
n = int(input())
c1 = {}
set1 = set()
for _ in range(n):
t1,t2 = map(int, input().split())
c1[t1] = t2
set1.add(t1)
m = int(input())
for __ in range(m):
t3,t4 = map(int, input().split())
if t3 in set1:
c1[t3] = max(c1[t3],t4)
else:
c1[t3] = t4
res = 0
for i in c1:
res+=c1[i]
print(res)
``` | output | 1 | 104,172 | 14 | 208,345 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, β¦, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company.
The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, β¦, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set.
In other words, the first company can present any subset of elements from \\{a_1, a_2, β¦, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, β¦, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of elements discovered by ChemForces.
The i-th of the next n lines contains two integers a_i and x_i (1 β€ a_i β€ 10^9, 1 β€ x_i β€ 10^9) β the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct.
The next line contains a single integer m (1 β€ m β€ 10^5) β the number of chemicals invented by TopChemist.
The j-th of the next m lines contains two integers b_j and y_j, (1 β€ b_j β€ 10^9, 1 β€ y_j β€ 10^9) β the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Input
1
1000000000 239
3
14 15
92 65
35 89
Output
408
Note
In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24.
In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. | instruction | 0 | 104,173 | 14 | 208,346 |
Tags: sortings
Correct Solution:
```
a_m = {}
ret = 0
n = int(input())
for i in range(n):
a, v = list(map(int, input().strip().split()))
a_m[a] = v
ret += v
m = int(input())
for j in range(m):
b, v = list(map(int, input().split()))
if b in a_m:
if v > a_m[b]:
ret += v-a_m[b]
else:
ret += v
print(ret)
``` | output | 1 | 104,173 | 14 | 208,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, β¦, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company.
The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, β¦, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set.
In other words, the first company can present any subset of elements from \\{a_1, a_2, β¦, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, β¦, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of elements discovered by ChemForces.
The i-th of the next n lines contains two integers a_i and x_i (1 β€ a_i β€ 10^9, 1 β€ x_i β€ 10^9) β the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct.
The next line contains a single integer m (1 β€ m β€ 10^5) β the number of chemicals invented by TopChemist.
The j-th of the next m lines contains two integers b_j and y_j, (1 β€ b_j β€ 10^9, 1 β€ y_j β€ 10^9) β the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Input
1
1000000000 239
3
14 15
92 65
35 89
Output
408
Note
In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24.
In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. | instruction | 0 | 104,174 | 14 | 208,348 |
Tags: sortings
Correct Solution:
```
def main():
dict = {}
for j in range(2):
n = int(input())
for i in range(n):
x, y = map(int, input().split())
if x in dict:
dict[x] = max(dict[x], y)
else:
dict[x] = y
print(sum(dict.values()))
if __name__ == "__main__":
main()
``` | output | 1 | 104,174 | 14 | 208,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, β¦, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company.
The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, β¦, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set.
In other words, the first company can present any subset of elements from \\{a_1, a_2, β¦, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, β¦, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of elements discovered by ChemForces.
The i-th of the next n lines contains two integers a_i and x_i (1 β€ a_i β€ 10^9, 1 β€ x_i β€ 10^9) β the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct.
The next line contains a single integer m (1 β€ m β€ 10^5) β the number of chemicals invented by TopChemist.
The j-th of the next m lines contains two integers b_j and y_j, (1 β€ b_j β€ 10^9, 1 β€ y_j β€ 10^9) β the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Input
1
1000000000 239
3
14 15
92 65
35 89
Output
408
Note
In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24.
In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. | instruction | 0 | 104,175 | 14 | 208,350 |
Tags: sortings
Correct Solution:
```
from array import array
database = {}
result = 0
n = int(input())
i = 0
while i < n:
i += 1
[a, x] = [int(x) for x in input().split()]
result += x
database[a] = x
m = int(input())
i = 0
while i < m:
i += 1
[b, y] = [int(x) for x in input().split()]
x = database.get(b, 0)
if x < y:
result += y - x
print(result)
``` | output | 1 | 104,175 | 14 | 208,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, β¦, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company.
The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, β¦, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set.
In other words, the first company can present any subset of elements from \\{a_1, a_2, β¦, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, β¦, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of elements discovered by ChemForces.
The i-th of the next n lines contains two integers a_i and x_i (1 β€ a_i β€ 10^9, 1 β€ x_i β€ 10^9) β the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct.
The next line contains a single integer m (1 β€ m β€ 10^5) β the number of chemicals invented by TopChemist.
The j-th of the next m lines contains two integers b_j and y_j, (1 β€ b_j β€ 10^9, 1 β€ y_j β€ 10^9) β the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Input
1
1000000000 239
3
14 15
92 65
35 89
Output
408
Note
In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24.
In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. | instruction | 0 | 104,176 | 14 | 208,352 |
Tags: sortings
Correct Solution:
```
n = int(input())
a = {}
for _ in range(n):
aa, xx = [int(v) for v in input().split()]
a[aa] = xx
m = int(input())
b = {}
for _ in range(m):
bb, yy = [int(v) for v in input().split()]
b[bb] = yy
sa = set(a.keys())
sb = set(b.keys())
both = sa & sb
print(
sum(max(a[v], b[v]) for v in both) +
sum(a[v] for v in sa - both) +
sum(b[v] for v in sb - both)
)
``` | output | 1 | 104,176 | 14 | 208,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, β¦, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company.
The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, β¦, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set.
In other words, the first company can present any subset of elements from \\{a_1, a_2, β¦, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, β¦, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of elements discovered by ChemForces.
The i-th of the next n lines contains two integers a_i and x_i (1 β€ a_i β€ 10^9, 1 β€ x_i β€ 10^9) β the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct.
The next line contains a single integer m (1 β€ m β€ 10^5) β the number of chemicals invented by TopChemist.
The j-th of the next m lines contains two integers b_j and y_j, (1 β€ b_j β€ 10^9, 1 β€ y_j β€ 10^9) β the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Input
1
1000000000 239
3
14 15
92 65
35 89
Output
408
Note
In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24.
In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. | instruction | 0 | 104,177 | 14 | 208,354 |
Tags: sortings
Correct Solution:
```
n1 = int(input())
item_pair = {}
for i in range(0, n1):
item_price = input().split()
item = item_price[0]
price = item_price[1]
item_pair[item] = int(price)
n2 = int(input())
for i in range(0, n2):
item_price = input().split()
item = item_price[0]
price = int(item_price[1])
if item in item_pair:
n1_price = item_pair[item]
if(price > n1_price):
item_pair[item] = price
else:
item_pair[item] = price
sum = 0
for value in item_pair.values():
sum += value
print(sum)
``` | output | 1 | 104,177 | 14 | 208,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n distinct chemical elements with indices a_1, a_2, β¦, a_n, and will get an income of x_i Berland rubles if the i-th element from this list is in the set of this company.
The TopChemist company discovered m distinct chemical elements with indices b_1, b_2, β¦, b_m, and it will get an income of y_j Berland rubles for including the j-th element from this list to its set.
In other words, the first company can present any subset of elements from \\{a_1, a_2, β¦, a_n\} (possibly empty subset), the second company can present any subset of elements from \\{b_1, b_2, β¦, b_m\} (possibly empty subset). There shouldn't be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of elements discovered by ChemForces.
The i-th of the next n lines contains two integers a_i and x_i (1 β€ a_i β€ 10^9, 1 β€ x_i β€ 10^9) β the index of the i-th element and the income of its usage on the exhibition. It is guaranteed that all a_i are distinct.
The next line contains a single integer m (1 β€ m β€ 10^5) β the number of chemicals invented by TopChemist.
The j-th of the next m lines contains two integers b_j and y_j, (1 β€ b_j β€ 10^9, 1 β€ y_j β€ 10^9) β the index of the j-th element and the income of its usage on the exhibition. It is guaranteed that all b_j are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Input
1
1000000000 239
3
14 15
92 65
35 89
Output
408
Note
In the first example ChemForces can choose the set (3, 7), while TopChemist can choose (1, 2, 4). This way the total income is (10 + 2) + (4 + 4 + 4) = 24.
In the second example ChemForces can choose the only element 10^9, while TopChemist can choose (14, 92, 35). This way the total income is (239) + (15 + 65 + 89) = 408. | instruction | 0 | 104,178 | 14 | 208,356 |
Tags: sortings
Correct Solution:
```
n = int(input())
chemForces = dict()
allSum = 0
for i in range(n):
newChem = input().split()
i,k = int(newChem[0]), int(newChem[1])
allSum += k
chemForces.update({i:k})
m = int(input())
topChemist = dict()
for j in range(m):
newTop = input().split()
j, t = int(newTop[0]), int(newTop[1])
allSum += t
topChemist.update({j : t})
intersection = set.intersection(set(chemForces), set(topChemist))
for i in intersection:
chemF = chemForces.get(i)
topC = topChemist.get(i)
if chemF <= topC:
allSum -= chemF
else:
allSum -= topC
print(allSum)
``` | output | 1 | 104,178 | 14 | 208,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000. | instruction | 0 | 104,455 | 14 | 208,910 |
Tags: brute force, data structures, sortings
Correct Solution:
```
from copy import deepcopy
class PriorityQueue():
def __init__(self):
self.elements = [(0, None)]
self.size = 0
self.capacity = 1000
def push(self, item):
# the first element of item is the index, it must be digits
self.size += 1
self.elements.append(item)
i = self.size
while self.elements[i//2][0]>item[0]:
self.elements[i] = self.elements[i//2]
i = i // 2
self.elements[i] = item
def poll(self):
if self.size <= 0:
return None
min_element = deepcopy(self.elements[1])
last_element = self.elements.pop()
self.size -= 1
i = 1
while (i*2)<=self.size:
child = i * 2 # left child
if (child != self.size and (self.elements[child+1][0] < self.elements[child][0])):
child += 1
if last_element[0] > self.elements[child][0]:
self.elements[i] = self.elements[child]
else:
break
i = child
if self.size > 0:
self.elements[i] = last_element
return min_element
import heapq
n,k = tuple(map(int, input().split(' ')))
s = []
for i in range(n):
t = tuple(map(int, input().split(' ')))
s.append(t)
def get_beauty(e):
return e[1]
s.sort(reverse=True, key=get_beauty)
max_score = 0
sum_len = 0
heap = []
try:
for i in range(n):
length = s[i][0]
beauty = s[i][1]
heapq.heappush(heap,length)
sum_len += length
if i+1 > k:
sum_len-=heapq.heappop(heap)
score = (sum_len) * beauty
max_score = max(max_score, score)
except Exception as e:
print(e)
print(max_score)
``` | output | 1 | 104,455 | 14 | 208,911 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000. | instruction | 0 | 104,456 | 14 | 208,912 |
Tags: brute force, data structures, sortings
Correct Solution:
```
import sys
import heapq
input = sys.stdin.readline
n, k = map(int, input().split())
l = []
for i in range(n):
a, b = map(int, input().split())
l.append((a,b))
l.sort(key = lambda x: x[1], reverse = True)
best = 0
curr = 0
q = []
for i in range(k):
curr += l[i][0]
heapq.heappush(q,l[i][0])
best = max(best, curr * l[i][1])
for i in range(k,n):
curr += l[i][0]
heapq.heappush(q,l[i][0])
curr -= heapq.heappop(q)
best = max(best, curr * l[i][1])
print(best)
``` | output | 1 | 104,456 | 14 | 208,913 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000. | instruction | 0 | 104,457 | 14 | 208,914 |
Tags: brute force, data structures, sortings
Correct Solution:
```
#import resource
import sys
#resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY])
mod=(10**9)+7
#fact=[1]
#for i in range(1,1001):
# fact.append((fact[-1]*i)%mod)
#ifact=[0]*1001
#ifact[1000]=pow(fact[1000],mod-2,mod)
#for i in range(1000,0,-1):
# ifact[i-1]=(i*ifact[i])%mod
from sys import stdin, stdout
#from bisect import bisect_left as bl
#from bisect import bisect_right as br
#import itertools
#import math
#import heapq
#from random import randint as rn
#from Queue import Queue as Q
def modinv(n,p):
return pow(n,p-2,p)
def ncr(n,r,p):
t=((fact[n])*((ifact[r]*ifact[n-r])%p))%p
return t
def ain():
return list(map(int,sin().split()))
def sin():
return stdin.readline()
def GCD(x, y):
while(y):
x, y = y, x % y
return x
"""**************************************************************************"""
def merge1(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
L = [0 for i in range(n1)]
R = [0 for i in range(n2)]
for i in range(0 , n1):
L[i] = arr[l + i]
for j in range(0 , n2):
R[j] = arr[m + 1 + j]
i,j,k=0,0,l
while i < n1 and j < n2 :
if L[i][1] < R[j][1]:
arr[k] = L[i]
i += 1
elif L[i][1] > R[j][1]:
arr[k] = R[j]
j += 1
else:
if L[i][0] < R[j][0]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def mergesort1(arr,l,r):
if l < r:
m = (l+(r-1))//2
mergesort1(arr, l, m)
mergesort1(arr, m+1, r)
merge1(arr, l, m, r)
def merge2(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
L = [0 for i in range(n1)]
R = [0 for i in range(n2)]
for i in range(0 , n1):
L[i] = arr[l + i]
for j in range(0 , n2):
R[j] = arr[m + 1 + j]
i,j,k=0,0,l
while i < n1 and j < n2 :
if L[i][0] > R[j][0]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def mergesort2(arr,l,r):
if l < r:
m = (l+(r-1))//2
mergesort2(arr, l, m)
mergesort2(arr, m+1, r)
merge2(arr, l, m, r)
n,k=ain()
b=[]
for i in range(n):
b.append(ain())
mergesort1(b,0,n-1)
r=[]
for i in range(n):
r.append([b[i][0],i])
mergesort2(r,0,n-1)
g=[0 for i in range(n)]
s=0
for i in range(k):
s+=r[i][0]
g[r[i][1]]=1
p=k
s1=0
for i in range(n):
q=s*b[i][1]
s1=max(s1,q)
if(g[i]==1):
s-=b[i][0]
while(p<n):
if(r[p][1]>i):
s+=r[p][0]
g[r[p][1]]=1
p+=1
break
p+=1
print (s1)
``` | output | 1 | 104,457 | 14 | 208,915 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000. | instruction | 0 | 104,458 | 14 | 208,916 |
Tags: brute force, data structures, sortings
Correct Solution:
```
from heapq import heappush, heappop
N, K = [int(s) for s in input().split()]
songs = []
for _ in range(N):
t, b = [int(s) for s in input().split()]
songs.append((t, b))
songs.sort(key= lambda x: x[1], reverse=True)
max_pleasure = 0
total_length = 0
max_lengths = []
for i in range(K):
total_length += songs[i][0]
heappush(max_lengths, songs[i][0])
max_pleasure = max(max_pleasure, total_length * songs[i][1])
for i in range(K, N):
if max_lengths[0] < songs[i][0]:
min_length = heappop(max_lengths)
heappush(max_lengths, songs[i][0])
total_length = total_length - min_length + songs[i][0]
max_pleasure = max(max_pleasure, total_length * songs[i][1])
print(max_pleasure)
``` | output | 1 | 104,458 | 14 | 208,917 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000. | instruction | 0 | 104,459 | 14 | 208,918 |
Tags: brute force, data structures, sortings
Correct Solution:
```
# -*- coding: utf-8 -*-
# @Date : 2019-03-23 09:28:20
# @Author : raj lath (oorja.halt@gmail.com)
# @Link : link
# @Version : 1.0.0
import sys
import heapq
sys.setrecursionlimit(10**5+1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
nb_songs, choose = RMI()
songval = []
for _ in range(nb_songs):
songval.append(RMI())
songval.sort(key = lambda x: -x[1])
maxs = min_val
lsum = 0
choice = []
for i in range(nb_songs):
ti, bi = songval[i]
if len(choice) < choose:
heapq.heappush(choice, ti)
lsum += ti
else:
l = heapq.heappushpop(choice, ti)
lsum += ti - l
maxs = max(maxs, lsum * bi)
print(maxs)
``` | output | 1 | 104,459 | 14 | 208,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000. | instruction | 0 | 104,460 | 14 | 208,920 |
Tags: brute force, data structures, sortings
Correct Solution:
```
import heapq
arr = input()
N,K = [int(x) for x in arr.split(' ')]
data = [[0]*2 for _ in range(N)]
for i in range(N):
arr = input()
L,B = [int(x) for x in arr.split(' ')]
data[i][0] = B
data[i][1] = L
data.sort(reverse=True)
#print(data)
h = []
s = 0
res = 0
for i in range(N):
if i<K:
s += data[i][1]
heapq.heappush(h, data[i][1])
else:
s += data[i][1]
heapq.heappush(h, data[i][1])
p = heapq.heappop(h)
s -= p
if s*data[i][0]>res:
res = s*data[i][0]
print(res)
``` | output | 1 | 104,460 | 14 | 208,921 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000. | instruction | 0 | 104,461 | 14 | 208,922 |
Tags: brute force, data structures, sortings
Correct Solution:
```
import heapq
d=[]
n,k=[int(x) for x in input().split()]
c=[]
lena=0
for i in range(n):
a,b=[int(x) for x in input().split()]
c.append((b,a))
c.sort(reverse=True)
answer=[]
summa=0
for item in c:
if lena>=k:
a=heapq.heappop(d)
summa-=a
heapq.heappush(d,max(item[1],a))
summa+=max(item[1],a)
else:
heapq.heappush(d,item[1])
summa+=item[1]
lena+=1
answer.append(summa*item[0])
print(max(answer))
``` | output | 1 | 104,461 | 14 | 208,923 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000. | instruction | 0 | 104,462 | 14 | 208,924 |
Tags: brute force, data structures, sortings
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from heapq import *
n,k=map(int,input().split())
a=[]
for i in range(n):
y,x=map(int,input().split())
a.append([x,y])
a.sort(reverse=True)
ans=0
s=0
hp=[]
for i in range(n):
s+=a[i][1]
heappush(hp,a[i][1])
if i>=k :
s-=heappop(hp)
ans=max(ans,a[i][0]*s)
print(ans)
``` | output | 1 | 104,462 | 14 | 208,925 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000.
Submitted Solution:
```
from heapq import *
n,k=list(map(int,input().split()))
a=[]
for i in range(0,n):
b,p=list(map(int,input().split()))
a.append([p,b])
a.sort(reverse=True)
r=0
s=0
h=[]
for p,b in a:
s+=b
heappush(h,b)
if len(h)>k:
s-=heappop(h)
r=max(r,s*p)
print(r)
``` | instruction | 0 | 104,463 | 14 | 208,926 |
Yes | output | 1 | 104,463 | 14 | 208,927 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000.
Submitted Solution:
```
from heapq import heappush, heappop
heapObj = []
n,k = map(int, input().strip().split())
l = []
for i in range(n):
t,b = map(int, input().strip().split())
l.append([b,t])
l.sort()
ct = 0
ans = 0
sm = 0
for i in range(n-1,-1,-1):
if ct < k:
ct += 1
sm += l[i][1]
heappush(heapObj, l[i][1])
else:
mn = heapObj[0]
if l[i][1] > mn:
heappop(heapObj)
heappush(heapObj,l[i][1])
sm = sm - mn + l[i][1]
ans = max(ans,l[i][0]*sm)
print(ans)
``` | instruction | 0 | 104,464 | 14 | 208,928 |
Yes | output | 1 | 104,464 | 14 | 208,929 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000.
Submitted Solution:
```
from heapq import heappop, heappush
n, k = map(int, input().split())
d = []
for i in range(n):
t, b = map(int, input().split())
d.append((t, b))
d = sorted(d, key=lambda x: (x[1], x[0]), reverse=True)
num = 0
tmp = []
tmp2 = 0
for i in range(n):
num = max(num, (tmp2 + d[i][0]) * d[i][1])
heappush(tmp, d[i][0])
tmp2 += d[i][0]
if len(tmp) > k - 1:
tmp2 -= heappop(tmp)
print(num)
``` | instruction | 0 | 104,465 | 14 | 208,930 |
Yes | output | 1 | 104,465 | 14 | 208,931 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000.
Submitted Solution:
```
from heapq import heappush,heappop
n,k=map(int,input().split())
a=[]
for i in range(n):
x,y=map(int,input().split())
a.append([y,x])
a.sort(reverse=True)
h=[]
ans,sm=0,0
for b,l in a:
sm+=l
heappush(h,l)
if len(h)>k:
sm-=heappop(h)
ans=max(ans,sm*b)
print(ans)
``` | instruction | 0 | 104,466 | 14 | 208,932 |
Yes | output | 1 | 104,466 | 14 | 208,933 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000.
Submitted Solution:
```
n,k=map(int,input().split())
a=[]
for _ in range(n):
t=list(map(int,input().split()))
a.append(t)
a=sorted(a,key=lambda x: x[1])
ma=0
for i in range(n):
if(a[i][0]*a[i][1]>ma):
ma=a[i][0]*a[i][1]
p=i
su=a[p][0]
c=a[p][1]
ans=su*c
co=1
mi=a[p][0]
ans1=[]
for i in range(n):
if(n-1-i!=p):
if(((su+a[n-1-i][0])*a[n-1-i][1])>ans and co<k):
su+=a[n-1-i][0]
if(a[n-1-i][1]<c):
c=a[n-1-i][1]
ans=su*c
co+=1
ans1.append(a[n-1-i][0])
mi=min(ans1)
elif(co>=k):
if(((su+a[n-1-i][0]-mi)*a[n-1-i][1])>ans):
su+=a[n-1-i][0]
su-=mi
if(a[n-1-i][1]<c):
c=a[n-1-i][1]
ans=su*c
ans1.append(a[n-1-i][0])
ans1.remove(mi)
mi=min(ans1)
print(ans)
``` | instruction | 0 | 104,467 | 14 | 208,934 |
No | output | 1 | 104,467 | 14 | 208,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000.
Submitted Solution:
```
from sys import stdin
import heapq as hp
n,k=map(int,stdin.readline().strip().split())
s=[]
for i in range(n):
a,b=map(int,stdin.readline().strip().split())
s.append((b,a))
s.sort(reverse=True)
s1=[]
s2=[]
sm=0
mx=-10**7
mn=10**7
ans=0
for i in range(k):
sm+=s[i][1]
hp.heappush(s1,s[i][1])
ans=max(sm*s[i][0],ans)
for i in range(k+1,n):
x=hp.heappop(s1)
if s[i][1]>x:
sm-=x
sm+=s[i][1]
hp.heappush(s1,s[i][1])
else:
hp.heappush(s1,x)
ans=max(ans,sm*s[i][0])
print(ans)
``` | instruction | 0 | 104,468 | 14 | 208,936 |
No | output | 1 | 104,468 | 14 | 208,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000.
Submitted Solution:
```
songs = []
n, k = map(int, input().split())
for i in range(n):
a, b = map(int, input().split())
songs.append([b, a])
songs.sort(key=lambda p: (-p[0] * p[1], p[0], p[1]))
minn = 10 ** 7
nice = 10 ** 7
summ = 0
maxsum = 0
i = 0
while i <= k:
summ += songs[i][1]
nice = min(songs[i][0], nice)
maxsum = max(maxsum, summ * nice)
i += 1
print(maxsum)
``` | instruction | 0 | 104,469 | 14 | 208,938 |
No | output | 1 | 104,469 | 14 | 208,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) β
6 = 96.
You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 3 β
10^5) β the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next n lines contains two integers t_i and b_i (1 β€ t_i, b_i β€ 10^6) β the length and beauty of i-th song.
Output
Print one integer β the maximum pleasure you can get.
Examples
Input
4 3
4 7
15 1
3 6
6 8
Output
78
Input
5 3
12 31
112 4
100 100
13 55
55 50
Output
10000
Note
In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) β
6 = 78.
In the second test case we can choose song 3. The total pleasure will be equal to 100 β
100 = 10000.
Submitted Solution:
```
songs=[]
effect=[]
n,k=list(map(int,input().split()))
for i in range(n):
t,b=list(map(int,input().split()))
songs.append((t,b))
songs.sort(key=lambda x:x[1])
for i in range(k):
l=[]
for j in range(i+1):
l.append(songs[len(songs)-j-1][0])
effect.append(sum(l)*songs[len(songs)-i-1][1])
print(max(effect))
``` | instruction | 0 | 104,470 | 14 | 208,940 |
No | output | 1 | 104,470 | 14 | 208,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2. | instruction | 0 | 104,665 | 14 | 209,330 |
Tags: combinatorics, math, sortings
Correct Solution:
```
def cnk_mod(max_n, m):
cnk = [[1]]
for n in range(1, max_n + 1):
tmp = [1]
for k in range(1, n):
tmp.append((cnk[n - 1][k - 1] + cnk[n - 1][k]) % m)
tmp.append(1)
cnk.append(tmp)
return cnk
'''
if k == 0 or k == n:
return 1
r = cnk_mod(n - 1, k - 1, m) + cnk_mod(n - 1, k, m)
r %= m
return r'''
M = 10 ** 9 + 7
#print(M)
cnk = cnk_mod(1001, M)
#print(cnk)
tests = int(input())
for t in range(tests):
n, k = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
a.sort()
x = 0
i = n - k - 1
while i >= 0 and a[i] == a[n - k]:
x += 1
i -= 1
y = 0
i = n - k
while i < n and a[i] == a[n - k]:
y += 1
i += 1
#print(a, x, y)
print(cnk[x + y][x])
``` | output | 1 | 104,665 | 14 | 209,331 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has a_i followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1, 3, 1, 2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
* conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a_1 + a_2 + a_4 = 6.
* conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a_2 + a_3 + a_4 = 6.
Since the answer can be quite large, output it modulo 10^9+7.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1 β€ k β€ n β€ 1000) β the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a_1, a_2, β¦ a_n (1 β€ a_i β€ n) β the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, on a separate line output one integer β the number of ways to select k bloggers so that the total number of their followers is maximum possible.
Example
Input
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2
Output
2
6
1
Note
The test case is explained in the statements.
In the second test case, the following ways are valid:
* conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a_1 + a_2 = 2;
* conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a_1 + a_3 = 2;
* conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a_1 + a_4 = 2;
* conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a_2 + a_3 = 2;
* conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a_2 + a_4 = 2;
* conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a_3 + a_4 = 2.
In the third test case, the following ways are valid:
* concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a_2 = 2. | instruction | 0 | 104,666 | 14 | 209,332 |
Tags: combinatorics, math, sortings
Correct Solution:
```
"""
#If FastIO not needed, use this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
self.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")
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
#start_time = time.time()
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
def getMat(n):
return [getInts() for _ in range(n)]
def isInt(s):
return '0' <= s[0] <= '9'
MOD = 10**9 + 7
"""
"""
facs = [1]
curr = 1
for j in range(1,1005):
curr *= j
curr %= MOD
facs.append(curr)
inv_facs = []
for f in facs:
inv_facs.append(pow(f,MOD-2,MOD))
def ncr(n,k):
return (facs[n]*inv_facs[k]*inv_facs[n-k])%MOD
def solve():
N, K = getInts()
A = getInts()
A.sort(reverse=True)
ans = 0
ind = K-1
while ind >= 0:
if A[ind] == A[K-1]:
ind -= 1
else:
break
ind2 = K-1
while ind2 < N:
if A[ind2] == A[K-1]:
ind2 += 1
else:
break
ind2 -= 1
ind += 1
n = ind2-ind+1
k = K-ind
return ncr(n,k)
for _ in range(getInt()):
print(solve())
#solve()
#print(time.time()-start_time)
``` | output | 1 | 104,666 | 14 | 209,333 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.