text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Tags: greedy
Correct Solution:
```
n = int(input())
res,cost = 0,2e9
for i in range(n):
a,b = map(int,input().split(' '))
if(b < cost):
cost = b
res+=a*cost
print(res)
```
| 103,300 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Tags: greedy
Correct Solution:
```
n = int(input())
price = 10000
sum = 0
for i in range(n):
a, b = [int(a) for a in input().split()]
if price > b:
price = b
sum += price*a
print(sum)
```
| 103,301 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Submitted Solution:
```
n = int(input())
a, p = [0]*n, [0]*n
for i in range(n):
a[i], p[i] = [int(x) for x in input().split()]
curr_min = p[0]
cost = a[0] * p[0]
for i in range(1, n):
if p[i] < curr_min:
curr_min = p[i]
cost += a[i] * curr_min
print(cost)
```
Yes
| 103,302 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Submitted Solution:
```
n = int(input())
l = [list(map(int, input().split())) for _ in range(n)]
j = l[0][1]
price = 0
for i in range(n-1):
if j < l[i+1][1]:
price += j*l[i][0]
else:
price += j*l[i][0]
j = l[i+1][1]
print(price+j*l[-1][0])
```
Yes
| 103,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Submitted Solution:
```
n = int(input())
prices = []
amounts = []
min_price = 1000000
for _ in range(n):
a, p = map(int, input().split())
if p < min_price:
prices.append(p)
amounts.append(a)
min_price = p
else:
amounts[-1] += a
print(sum([a * p for a, p in zip(amounts, prices)]))
```
Yes
| 103,304 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Submitted Solution:
```
#588A Tutorial
n = int(input())
ans = 0
mn = 200
for i in range(n):
a, p = map(int, input().split())
mn = min(mn, p)
ans += a * mn
print(ans)
```
Yes
| 103,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Submitted Solution:
```
n = int(input())
x, y = [*map(int, input().split())]
minp = y
ans = x * y
for i in range(n - 1):
if minp > y:
minp = y
ans += x * minp
x, y = [*map(int, input().split())]
print(ans)
```
No
| 103,306 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Submitted Solution:
```
__author__ = 'cmashinho'
n = int(input())
needMeat = []
meatPrice = []
for _ in range(n):
a, b = map(int, input().split())
needMeat.append(a)
meatPrice.append(b)
answer = 0
minPrice = min(meatPrice)
for i in range(n):
if i == 0:
if meatPrice[i] == minPrice:
answer += sum(needMeat) * meatPrice[i]
break
answer += needMeat[i] * meatPrice[i]
else:
if meatPrice[i] == minPrice:
for j in range(i, n):
answer += needMeat[j] * minPrice
break
else:
answer += needMeat[i] * meatPrice[i]
print(answer)
```
No
| 103,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Submitted Solution:
```
n = int(input())
minimun = float("Inf")
money = 0
for i in range(0, n):
amountMeat, price = [int(x) for x in input().split()]
if price < minimun:
minimun = price
money += minimun * amountMeat
```
No
| 103,308 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
<image>
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
3
1 3
2 2
3 1
Output
10
Input
3
1 3
2 1
3 2
Output
8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
Submitted Solution:
```
lst1=[]
lst2=[]
cost=0
for a in range(int(input())):
lst=[int(x) for x in input().split()]
lst1.append(lst[0])
lst2.append(lst[1])
for i in range(len(lst2)):
if lst2[i] == min(lst2):
cost+= sum(lst1[i:])*lst2[i]
break
else:
cost+=lst1[i]*lst2[i]
print(cost)
```
No
| 103,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Tags: combinatorics, math
Correct Solution:
```
n= int(input())
a=n*(n-1)*(n-2)*(n-3)*(n-4)//120
b=a*n*(n-1)*(n-2)*(n-3)*(n-4)
print(b)
```
| 103,310 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Tags: combinatorics, math
Correct Solution:
```
from math import *
from decimal import *
M = Decimal(120)
def mul(n):
ans = Decimal(1)
for i in range(n-4, n + 1):
ans *= Decimal(i)
return ans
n = int(input())
ans = mul(n) / M
ans *= mul(n)
print(ans)
```
| 103,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Tags: combinatorics, math
Correct Solution:
```
from math import factorial as fact
n=int(input())
answer=(n*(n-1)*(n-2)*(n-3)*(n-4))//(fact(5))
answer*=(n*(n-1)*(n-2)*(n-3)*(n-4))
print(answer)
```
| 103,312 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Tags: combinatorics, math
Correct Solution:
```
import math
n=int(input())
print((int(math.factorial(n)/(math.factorial(n-5)*120))**2)*120)
```
| 103,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Tags: combinatorics, math
Correct Solution:
```
import math
def choose(n, k):
return int(math.factorial(n) / (math.factorial(k) * math.factorial(n - k)))
n = int(input())
print(120 * choose(n, 5) * choose(n, 5))
```
| 103,314 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Tags: combinatorics, math
Correct Solution:
```
from math import*
n=int(input())
tmp=int(factorial(n)/(factorial(n-5)*factorial(5)))
print(tmp*tmp*factorial(5))
```
| 103,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Tags: combinatorics, math
Correct Solution:
```
def solve():
n = int(input())
x = n * (n - 1) * (n - 2) * (n - 3) * (n - 4)
print(x * x // 120)
if __name__ == '__main__':
solve()
```
| 103,316 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Tags: combinatorics, math
Correct Solution:
```
from math import factorial
import math
# def c(n, k):
# return factorial(n) // (factorial(k) * factorial(n-k))
n = int(input())
ans = factorial(n)//factorial(n-5)
ans **= 2
ans //= factorial(5)
print(ans)
```
| 103,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Submitted Solution:
```
import sys
def fact(n):
ret = 1
for x in range(1, n + 1):
ret = ret * x
return ret
def C(n, k):
return fact(n) // (fact(k) * (fact(n - k)))
n = int(input())
print(C(n, 5) * C(n, 5) * 120)
```
Yes
| 103,318 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Submitted Solution:
```
n = int(input())
print(n ** 2 * (n - 1) ** 2 * (n - 2) ** 2 * (n - 3) ** 2 * (n - 4) ** 2 // 120)
```
Yes
| 103,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Submitted Solution:
```
def fact(n):
return 1 if n < 2 else n * fact(n - 1)
def C(n, k):
return fact(n) // fact(n - k) // fact(k)
n = int(input())
print(C(n, 5) * n * (n - 1) * (n - 2) * (n - 3) * (n - 4))
```
Yes
| 103,320 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Submitted Solution:
```
from sys import stdin
import functools
import operator
num = int(stdin.readline())
def fac(n):
if n == 0:
return 1
return functools.reduce(operator.mul, range(1, n + 1))
def combinations(n, k):
return fac(n) // (fac(k) * fac(n - k))
print(combinations(num, 5) * fac(num) // fac(num - 5))
```
Yes
| 103,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Submitted Solution:
```
a=int(input())
print(a*(a-1)*(a-2)*(a-3)*(a-4))
```
No
| 103,322 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Submitted Solution:
```
import operator
import math
def c(n,k):
return int(math.factorial(n)*math.factorial(n)/(math.factorial(k)*math.factorial(n-k)*math.factorial(n-k)))
x=int(input())
print(c(x,5))
```
No
| 103,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Submitted Solution:
```
n=int(input())
res=1
for i in range(2,n+1,+1):
res*=i
if n==6:
res=res*n
print(int(res))
```
No
| 103,324 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The city park of IT City contains n east to west paths and n north to south paths. Each east to west path crosses each north to south path, so there are n2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches.
Input
The only line of the input contains one integer n (5 ≤ n ≤ 100) — the number of east to west paths and north to south paths.
Output
Output one integer — the number of ways to place the benches.
Examples
Input
5
Output
120
Submitted Solution:
```
n=int(input())
T=1;
for i in range(1,n+1):
T=T*i
print(T)
```
No
| 103,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contribution — an integer number, not necessarily positive. There are n registered users and the i-th of them has contribution ti.
Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments.
* Limak can spend b minutes to read one blog and upvote it. Author's contribution will be increased by 5.
* Limak can spend c minutes to read one comment and upvote it. Author's contribution will be increased by 1.
Note that it's possible that Limak reads blogs faster than comments.
Limak likes ties. He thinks it would be awesome to see a tie between at least k registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value x that at least k registered users have contribution exactly x.
How much time does Limak need to achieve his goal?
Input
The first line contains four integers n, k, b and c (2 ≤ k ≤ n ≤ 200 000, 1 ≤ b, c ≤ 1000) — the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively.
The second line contains n integers t1, t2, ..., tn (|ti| ≤ 109) where ti denotes contribution of the i-th registered user.
Output
Print the minimum number of minutes Limak will spend to get a tie between at least k registered users.
Examples
Input
4 3 100 30
12 2 6 1
Output
220
Input
4 3 30 100
12 2 6 1
Output
190
Input
6 2 987 789
-8 42 -4 -65 -8 -8
Output
0
Note
In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows.
* He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6.
* Then he spends 4·30 = 120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1).
In the given scenario, Limak spends 100 + 4·30 = 220 minutes and after that each of users 2, 3, 4 has contribution 6.
In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100 + 3·30 = 190 minutes:
* Spend 2·30 = 60 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12.
* Spend 30 + 100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6 + 5 + 1 = 12.
Submitted Solution:
```
def main():
def ta(k_g):
tim = 0
while k_g >= 5:
if preferred_c:
tim += c * 5
else:
tim += b
k_g -= 5
while k_g >= 1:
tim += c
k_g -= 1
return tim
line = input().split()
n = int(line[0]) # кол-во зарег пользователей
k = int(line[1]) # мин кол-во группы один. по вкладу
b = int(line[2]) # время блога
c = int(line[3]) # время комента
line = input().split()
t = [] # вклад пользователя ti
for _ in range(len(line)):
t.append(int(line[_]))
t.sort()
# check the most efficient thing
preferred_c = False # min, points
if c * 5 < b:
preferred_c = True
t_min = 10000000000
for ti in range(len(t) - k + 1):
t_temp = 0
for _ in range(k):
k_temp = abs(t[ti + k - 1] - t[ti + _])
t_temp += ta(k_temp)
if t_temp < t_min:
t_min = t_temp
print(t_min)
main()
```
No
| 103,326 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contribution — an integer number, not necessarily positive. There are n registered users and the i-th of them has contribution ti.
Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments.
* Limak can spend b minutes to read one blog and upvote it. Author's contribution will be increased by 5.
* Limak can spend c minutes to read one comment and upvote it. Author's contribution will be increased by 1.
Note that it's possible that Limak reads blogs faster than comments.
Limak likes ties. He thinks it would be awesome to see a tie between at least k registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value x that at least k registered users have contribution exactly x.
How much time does Limak need to achieve his goal?
Input
The first line contains four integers n, k, b and c (2 ≤ k ≤ n ≤ 200 000, 1 ≤ b, c ≤ 1000) — the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively.
The second line contains n integers t1, t2, ..., tn (|ti| ≤ 109) where ti denotes contribution of the i-th registered user.
Output
Print the minimum number of minutes Limak will spend to get a tie between at least k registered users.
Examples
Input
4 3 100 30
12 2 6 1
Output
220
Input
4 3 30 100
12 2 6 1
Output
190
Input
6 2 987 789
-8 42 -4 -65 -8 -8
Output
0
Note
In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows.
* He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6.
* Then he spends 4·30 = 120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1).
In the given scenario, Limak spends 100 + 4·30 = 220 minutes and after that each of users 2, 3, 4 has contribution 6.
In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100 + 3·30 = 190 minutes:
* Spend 2·30 = 60 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12.
* Spend 30 + 100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6 + 5 + 1 = 12.
Submitted Solution:
```
def main():
def ta(k_g):
tim = 0
while k_g >= 5:
if preferred_c:
tim += c * 5
else:
tim += b
k_g -= 5
while k_g >= 1:
tim += c
k_g -= 1
return tim
line = input().split()
n = int(line[0]) # кол-во зарег пользователей
k = int(line[1]) # мин кол-во группы один. по вкладу
b = int(line[2]) # время блога
c = int(line[3]) # время комента
line = input().split()
t = [] # вклад пользователя ti
for _ in range(n):
t.append(int(line[_]))
t.sort()
# check the most efficient thing
preferred_c = False # min, points
if c * 5 < b:
preferred_c = True
t_min = 10000000000
for ti in range(len(t) - k + 1):
for tj in range(ti, len(t) - k + 1):
if tj > ti:
break
t_temp = 0
for _ in range(k):
k_temp = abs(t[ti + k - 1] - t[ti + _])
t_temp += ta(k_temp)
if t_temp < t_min:
t_min = t_temp
print(t_min)
main()
```
No
| 103,327 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Tags: brute force
Correct Solution:
```
n=int(input())
a=b=c=0
while a*1234567<=n:
cur=a*1234567
b=0
while cur+b*123456<=n:
if (n-cur-b*123456)%1234==0:print('YES');exit()
b+=1
a+=1
print('NO')
```
| 103,328 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Tags: brute force
Correct Solution:
```
n = int(input())
A = 1234567
B = 123456
C = 1234
for a in range(n // A + 1):
for b in range(n // B + 1):
c = n - a * A - b * B
if c >= 0 and c % C == 0:
exit(print("YES"))
print("NO")
```
| 103,329 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Tags: brute force
Correct Solution:
```
n = int(input())
i, t = [0, 0]
a, b = [1234567, 123456]
while a * i <= n:
j = 0
while b * j + a * i <= n:
if (n - a * i - b * j) % 1234 == 0:
t = 1
break
j += 1
i += 1
if t: break
if t: print('YES', end = '')
else: print('NO', end = '')
```
| 103,330 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Tags: brute force
Correct Solution:
```
n = int(input())
for i in range(900):
for j in range(900):
cur = n - 1234567*i - 123456*j
if cur >= 0 and cur % 1234 == 0:
print('YES')
exit(0)
print('NO')
```
| 103,331 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Tags: brute force
Correct Solution:
```
# Economy Game
from math import*
kolya = int(input())
houses = 1234567
cars = 123456
computers = 1234
if 1 <= kolya <= (10**9):
yes = False
for i in range(0, kolya+1, houses):
for ii in range(0, (kolya - i) + 1, cars):
c_computers = (kolya - (i + ii)) / computers
if floor(c_computers) == ceil(c_computers):
yes = True
break
if yes:
print("YES")
break
else:
print("NO")
else:
print("NO")
```
| 103,332 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Tags: brute force
Correct Solution:
```
n = int(input())
sol = False
for i in range(max(n//1234567 + 1,1)):
n2 = n - i*1234567
for j in range(max(n2//123456 + 1, 1)):
n3 = n2 - j*123456
if n3 % 1234 == 0:
sol = True
break
print(["NO","YES"][sol])
```
| 103,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Tags: brute force
Correct Solution:
```
n = int(input())
X = 1234567
Y = 123456
Z = 1234
li = n//X
flag = 0
for i in range(li+1):
lj = (n-X*i)//Y
if flag==1 :
break
for j in range(lj+1):
if (n-i*X-j*Y)%Z==0:
flag = 1
break
if flag == 1:
print("YES")
else :
print("NO")
```
| 103,334 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Tags: brute force
Correct Solution:
```
n=int(input())
a=b=c=0
while a*1234567<=n:
cur=a*1234567
b=0
while cur+b*123456<=n:
if (n-cur-b*123456)%1234==0:print('YES');exit()
b+=1
a+=1
print('NO')
# Made By Mostafa_Khaled
```
| 103,335 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Submitted Solution:
```
n=int(input())
casa=1234567
carro=123456
comp=1234
resposta=False
for a in range(0,n+1,casa):
if a>n or resposta: break
for b in range(0,(n-a)+1,carro):
if a+b>n or resposta: break
elif(n-a-b) % comp == 0:
resposta = True
if resposta:
print("YES")
else:
print("NO")
```
Yes
| 103,336 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Submitted Solution:
```
n=int(input())
from sys import *
l=[1234567,123456,1234]
m=n
j=0
t=1
while j*l[0]<=m and t:
mm=m
m-=j*l[0]
jj=0
while jj*l[1]<=m and t:
mmm=m
m-=jj*l[1]
if m%l[2]==0:
print('YES')
t=0
m=mmm
jj+=1
m=mm
j+=1
if t:
print('NO')
```
Yes
| 103,337 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Submitted Solution:
```
n = int(input())
at_best = 1000
for i in range(0, at_best):
for j in range(0, at_best):
current = (i * 1234567) + (j * 123456)
remain = n - current
if (n % 1234 == 0) or (current == n) or (remain % 1234 == 0 and remain > 0):
print("YES")
quit()
print("NO")
```
Yes
| 103,338 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Submitted Solution:
```
su=int(input())
a=0
b=0
c=0
amax=su//1234567
bmax=su//123456
for a in range(amax+1):
if c>0 or su-a*1234567<0:
break
for b in range(bmax+1):
if su-(a*1234567+b*123456)<0:
break
if (su-(a*1234567+b*123456))%1234 == 0:
c+=1
break
if c>0:
print('YES')
else:
print('NO')
```
Yes
| 103,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Submitted Solution:
```
import sys
# http://codeforces.com/problemset/problem/681/B
# a * 1234567 + b * 123456 + c * 1234 = n?
n = int(input())
# a only
if n%1234567 == 0:
print ("YES")
sys.exit()
# b only
elif n%123456 == 0:
print ("YES")
sys.exit()
# c only
elif n%1234 == 0:
print ("YES")
sys.exit()
if n < 1234:
print ("NO")
sys.exit()
# no a
if n < 1234567:
# non può essere dispari, perche' somma di due pari
if n%2 != 0:
print ("NO")
sys.exit()
else:
b = 1
c = (n-123456*b)/1234
while (b*123456 + c*1234) < n:
if (n-123456*b)%1234 == 0:
print ("YES")
sys.exit()
b += 1
c = 1
b = (n-c*1234)/123456
while (b*123456 + c*1234) < n:
if (n-c*1234)%123456 == 0:
print ("YES")
sys.exit()
c += 1
print ("NO")
sys.exit()
# ultimo caso: ci sono tutti e 3
# non puo' essere pari
if n%2 == 0:
print ("NO")
sys.exit()
b = 1
c = 1
a = (n-b*123456-c*1234)/1234567
while (a * 1234567 + b * 123456 + c * 1234) < n:
if (n-b*123456-c*1234)%1234567 == 0:
print ("YES")
sys.exit()
b += 1
b -= 1
while (b > 1):
while (a * 1234567 + b * 123456 + c * 1234) < n:
if (n-b*123456-c*1234)%1234567 == 0:
print ("YES")
sys.exit()
c += 1
c = 1
b -= 1
print ("NO")
```
No
| 103,340 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Submitted Solution:
```
1359257
```
No
| 103,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Submitted Solution:
```
x = int(input())
o = [1234567, 123456, 1234]
c = 1
t = x
inc = 0
def ok():
global c
c = 0
def test(g):
for p in range(3):
if g % o[p] == 0:
ok()
break
for i in range (int(x/123456)):
test(x-(inc*1234567))
test(x - (inc * 123456))
if c != 0:
for i in range (int(x/1234567)):
temp = 1234567*i
for p in range (i*11):
test(x-(p*123456)-temp)
elif c != 0:
for i in range (int(x/1234567)):
temp = 1234567*i
for p in range (i*1001):
test(x-(p*1234)-temp)
elif c != 0:
for i in range (int(x/123456)):
temp = 1234567*i
for p in range (i*1001):
test(x-(p*1234)-temp)
if c == 0:
print("YES")
else:
print("NO")
```
No
| 103,342 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0.
Kolya remembers that at the beginning of the game his game-coin score was equal to n and that he have bought only some houses (for 1 234 567 game-coins each), cars (for 123 456 game-coins each) and computers (for 1 234 game-coins each).
Kolya is now interested, whether he could have spent all of his initial n game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers a, b and c such that a × 1 234 567 + b × 123 456 + c × 1 234 = n?
Please help Kolya answer this question.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 109) — Kolya's initial game-coin score.
Output
Print "YES" (without quotes) if it's possible that Kolya spent all of his initial n coins buying only houses, cars and computers. Otherwise print "NO" (without quotes).
Examples
Input
1359257
Output
YES
Input
17851817
Output
NO
Note
In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1 234 567 + 123 456 + 1234 = 1 359 257 game-coins in total.
Submitted Solution:
```
x = int(input())
o = [1234567, 123456, 1234]
c = 1
t = x
def ok():
global c
c = 0
def test(g):
for p in range(3):
if g % o[p] == 0 and g >= 0:
ok()
break
#for i in range (int(x/1234)):
# test(x-(i*1234567))
# test(x - (i * 123456))
# test(x - (i * 1234))
if c != 0:
for i in range (int(x/1234567)+1):
if c == 0:
break
temp = 1234567*i
test(x-temp)
for p in range (i*11):
if c == 0:
break
test(x-(p*123456)-temp)
elif c != 0:
for i in range(int(x/1234567)+1):
if c == 0:
break
temp = 1234567*i
test(x - temp)
for p in range(i*1001):
if c == 0:
break
test(x-(p*1234)-temp)
elif c != 0:
for i in range (int(x/123456)+1):
if c == 0:
break
temp = 1234567*i
test(x - temp)
for p in range (i*1001):
if c == 0:
break
test(x-(p*1234)-temp)
if c == 0:
print("YES")
else:
print("NO")
```
No
| 103,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Tags: brute force, data structures, implementation
Correct Solution:
```
import os
import io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n, q = map(int, input().split())
last_2 = [-1] * n
unread_count = [0] * n
total_unread = 0
ev = []
max_3 = 0
ans = []
for i in range(q):
t, x = map(int, input().split())
x -= 1
if t==1:
ev.append(x)
unread_count[x] += 1
total_unread += 1
elif t==2:
total_unread -= unread_count[x]
unread_count[x] = 0
last_2[x] = len(ev)-1
else:
for j in range(max_3, x+1):
el = ev[j]
if last_2[el] < j:
total_unread -= 1
unread_count[el] -= 1
max_3 = max(max_3, x+1)
ans.append(total_unread)
for a in ans:
print(a)
```
| 103,344 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Tags: brute force, data structures, implementation
Correct Solution:
```
from collections import defaultdict, deque
q = deque()
idx, total = 0, 0
vis = defaultdict(deque)
not_valid = set()
max_x = -1
ans = []
N, Q = map(int, input().split())
for _ in range(Q):
t, x = map(int, input().split())
if t == 1:
q.append((idx, x))
vis[x].append(idx)
idx += 1
total += 1
elif t == 2:
while vis[x]:
# All element in vis[x] should be popped out from q, and It will do it later.
# The index are collected in the not_valid set.
i = vis[x].pop()
not_valid.add(i)
# If the element is expired, then skip it.
if i <= max_x - 1:
continue
total -= 1
else:
# Check the element should be expired or popped out.
while q and (q[0][0] <= x - 1 or q[0][0] in not_valid):
j, y = q.popleft()
if j not in not_valid:
total -= 1
vis[y].popleft()
max_x = max(max_x, x)
ans.append(str(total))
# Trick: print the answer at the end to avoid TLE
# by keep commnicating with kernel space.
print("\n".join(ans))
```
| 103,345 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Tags: brute force, data structures, implementation
Correct Solution:
```
from heapq import *
from collections import defaultdict, deque
total = 0
N, Q = map(int, input().split())
q = deque()
idx = 0
vis = defaultdict(deque)
total = 0
not_valid = set()
max_d = -1
ans = []
for _ in range(Q):
t, x = map(int, input().split())
if t == 1:
q.append((idx, x))
vis[x].append(idx)
idx += 1
total += 1
elif t == 2:
while vis[x]:
i = vis[x].pop()
not_valid.add(i)
if i <= max_d:
continue
total -= 1
else:
while q and (q[0][0] <= x - 1 or q[0][0] in not_valid):
j, y = q.popleft()
if j not in not_valid:
total -= 1
if len(vis[y]):
vis[y].popleft()
max_d = max(max_d, x - 1)
ans.append(str(total))
print("\n".join(ans))
```
| 103,346 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Tags: brute force, data structures, implementation
Correct Solution:
```
import collections
import sys
n, q = map(int, input().split())
# Key: app number, value: list of indexes in the arr
hash = {}
# arr = []
deque = collections.deque()
messageNumber = 1
unread = 0
L = []
for i in range(q):
c, x = map(int, input().split())
if c == 1:
if x not in hash:
hash[x] = set()
hash[x].add(messageNumber)
deque.append((x, messageNumber))
messageNumber += 1
unread += 1
# print("case 1")
# print(unread)
elif c == 2:
if x in hash:
xUnread = len(hash[x])
hash[x] = set()
unread -= xUnread
# print("case 2")
# print(unread)
else:
t = x
read = 0
while len(deque) != 0 and deque[0][1] <= t:
app, message = deque.popleft()
if message in hash[app]:
read += 1
hash[app].remove(message)
unread -= read
# print("case 3")
# print(unread)
L.append(unread)
sys.stdout.write('\n'.join(map(str, L)))
```
| 103,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Tags: brute force, data structures, implementation
Correct Solution:
```
#!/usr/bin/env python
#-*-coding:utf-8 -*-
import collections
n,q=map(int,input().split())
Q=collections.deque()
A=n*[0]
B=A[:]
L=[]
s=n=0
for _ in range(q):
y,x=map(int,input().split())
if 2>y:
x-=1
Q.append(x)
B[x]+=1
A[x]+=1
s+=1
elif 3>y:
x-=1
s-=A[x]
A[x]=0
else:
while x>n:
n+=1
y=Q.popleft()
B[y]-=1
if(B[y]<A[y]):
A[y]-=1
s-=1
L.append(s)
print('\n'.join(map(str,L)))
```
| 103,348 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Tags: brute force, data structures, implementation
Correct Solution:
```
#!/usr/bin/env python
#-*-coding:utf-8 -*-
import sys,collections
n,q=map(int,input().split())
M=collections.defaultdict(collections.deque)
Q=collections.deque()
L=[]
s=n=m=0
for _ in range(q):
y,x=map(int,input().split())
if 2>y:
s+=1
Q.append(x)
M[x].append(n)
n+=1
elif 3>y:
y=M.get(x)
if y:
s-=len(y)
del M[x]
else:
while x>m:
z=Q.popleft()
y=M.get(z)
if y and y[0]<x:
s-=1
y.popleft()
if not y:del M[z]
m+=1
L.append(s)
sys.stdout.write('\n'.join(map(str,L)))
```
| 103,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Tags: brute force, data structures, implementation
Correct Solution:
```
import collections
n, q = map(int ,input().split())
Q = collections.deque()
A = [0] * n
B = A[:]
L = []
s = n = 0
for k in range(q):
type1, x = map(int, input().split())
if type1 == 1:
x -= 1
Q.append(x)
B[x] += 1
A[x] += 1
s += 1
if type1 == 2:
x -= 1
s -= A[x]
A[x] = 0
if type1 == 3:
while x > n:
n += 1
y = Q.popleft()
B[y] -= 1
if B[y] < A[y]:
A[y] -= 1
s -= 1
L.append(str(s))
print('\n'.join(L))
```
| 103,350 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Tags: brute force, data structures, implementation
Correct Solution:
```
from sys import stdin
input=stdin.readline
n,q=map(int,input().split())
is_read_index=0
is_read=[]
l=[[] for i in range(n)]
ans=0
prev=0
for _ in range(q):
t,v=map(int,input().split())
if t==1:
l[v-1].append(is_read_index)
is_read_index+=1
is_read.append(False)
ans+=1
elif t==2:
for idx in l[v-1]:
if not is_read[idx]:
is_read[idx]=True
ans-=1
l[v-1]=[]
else:
if v>prev:
for idx in range(prev,v):
if not is_read[idx]:
is_read[idx]=True
ans-=1
prev=v
print(ans)
```
| 103,351 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
def main():
n, q = map(int, input().split())
vol, tot, l, res = [0] * (n + 1), [0] * (n + 1), [], []
z = m = 0
for _ in range(q):
t, x = map(int, input().split())
if t == 1:
l.append(x)
tot[x] += 1
vol[x] += 1
z += 1
elif t == 2:
z -= vol[x]
vol[x] = 0
else:
if m < x:
r, m = range(m, x), x
for i in r:
x = l[i]
tot[x] -= 1
if vol[x] > tot[x]:
vol[x] -= 1
z -= 1
res.append(z)
print('\n'.join(map(str, res)))
if __name__ == '__main__':
main()
```
Yes
| 103,352 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
n,q = map(int,input().split())
xi = [[0]*(n+1) for i in range(2)]
noti = []
ans = ""
num = 0
num2 = 0
num3 = 0
while q > 0:
typ,xt = map(int,input().split())
if typ == 1:
xi[0][xt] += 1
noti += [xt]
num += 1
num2 += 1
elif typ == 3:
for i in range(num3,xt):
if i+1 > xi[1][noti[i]]:
xi[0][noti[i]] -= 1
num -= 1
num3 = max(num3,xt)
else:
num -= xi[0][xt]
xi[0][xt] = 0
xi[1][xt] = num2
ans += str(num)
ans += "\n"
q -= 1
print(ans)
```
Yes
| 103,353 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
"""
#If FastIO not needed, used 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
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
import math, string
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())
MOD = 10**9+7
"""
We need to maintain a set of unread notifications from which we can remove the first max(ti), and also the first xi for each application
[3,4,4,1,1,1,2,2,1,3,3,2,4,4,1,2,3,2,4,4]
N applications. At any given time the number of unread notifications is the sum of the number of unread notifications for each application, which lends itself to a Fenwick Tree
But how do we maintain the order?
As we delete the first N elements, we can also subtract one from the rolling sum and from the element total.
As we clear an element of type X, we can subtract that many from the rolling total
"""
def solve():
N, Q = getInts()
order = dq([])
cur_total = 0
cur_del = -1
els = [0]*(N+1)
del_up_to = [-1]*(N+1)
for q in range(Q):
T, X = getInts()
if T == 1:
order.append(X)
els[X] += 1
cur_total += 1
print(cur_total)
elif T == 2:
cur_total -= els[X]
els[X] = 0
del_up_to[X] = max(len(order)-1,del_up_to[X])
print(cur_total)
elif T == 3:
if X-1 > cur_del:
for i in range(cur_del+1,X):
if del_up_to[order[i]] < i:
cur_total -= 1
del_up_to[order[i]] = i
els[order[i]] -= 1
#print(order)
#print(els)
#print(del_up_to)
cur_del = X-1
print(cur_total)
return
#for _ in range(getInt()):
solve()
```
Yes
| 103,354 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
from sys import stdout
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
print = stdout.write
n, q = map(int, input().split())
d = [[] for i in range(n+1)]
mark = []
ans = 0
inde = 0
t = 0
for i in range(q):
event, x = map(int, input().split())
if event == 1:
ans += 1
d[x].append(inde)
inde += 1
mark.append(False)
elif event == 2:
for j in d[x]:
if not mark[j]:
mark[j] = True
ans -= 1
d[x] = []
else:
for j in range(t, x):
if not mark[j]:
mark[j] = True
ans -= 1
if x > t: t = x
print(str(ans) + '\n')
```
Yes
| 103,355 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
from collections import deque
n,u=map(int,input().split())
count=0
d={i:0 for i in range(1,n+1)}
q=deque([])
for i in range(u):
t,x=map(int,input().split())
if t==1:
count+=1
d[x]+=1
q.append(x)
elif t==2:
val=d[x]
d[x]=0
count-=val
else:
while x>0 and q:
ele=q.popleft()
if d[ele]>0:
d[ele]-=1
count-=1
x-=1
print(count)
```
No
| 103,356 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
def binsearch(nums, target):
left = 0
right = len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
elif nums[mid] > target:
right = mid - 1
elif nums[mid] < target:
left = mid + 1
return left
n, q = map(int, input().split())
apps = {}
notifs = []
unread = 0
read = []
k = 0
for query in range(q):
t, x = map(int, input().split())
if t == 1:
k += 1
notifs.append(query)
if not apps.get(x):
apps[x] = []
apps[x].append(k)
unread += 1
if t == 2:
if not apps.get(x):
apps[x] = []
for i in range(len(apps[x])):
read.append(apps[x][i])
unread -= len(apps[x])
apps[x] = []
if t == 3:
timestamp = x
remplace = binsearch(read, timestamp+.1)
unread -= (x - remplace)
print(unread)
```
No
| 103,357 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
"""
#If FastIO not needed, used 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
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
import math, string
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())
MOD = 10**9+7
"""
We need to maintain a set of unread notifications from which we can remove the first max(ti), and also the first xi for each application
[3,4,4,1,1,1,2,2,1,3,3,2,4,4,1,2,3,2,4,4]
N applications. At any given time the number of unread notifications is the sum of the number of unread notifications for each application, which lends itself to a Fenwick Tree
But how do we maintain the order?
As we delete the first N elements, we can also subtract one from the rolling sum and from the element total.
As we clear an element of type X, we can subtract that many from the rolling total
"""
def solve():
N, Q = getInts()
order = dq([])
cur_total = 0
cur_del = -1
els = [0]*(N+1)
del_up_to = [-1]*(N+1)
for q in range(Q):
T, X = getInts()
if T == 1:
order.append(X)
els[X] += 1
cur_total += 1
print(cur_total)
elif T == 2:
cur_total -= els[X]
els[X] = 0
del_up_to[X] = q
print(cur_total)
elif T == 3:
for i in range(cur_del+1,X):
if del_up_to[order[i]] < i:
cur_total -= 1
del_up_to[order[i]] = max(i,del_up_to[order[i]])
cur_del = X-1
print(cur_total)
return
#for _ in range(getInt()):
solve()
```
No
| 103,358 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
Submitted Solution:
```
from heapq import *
from bisect import *
from collections import defaultdict
import heapq
total = 0
N, Q = map(int, input().split())
q = []
for i in range(Q):
t, x = map(int, input().split())
if t == 1:
heappush(q, (i, x))
elif t == 2:
next_q = []
for j, y in q:
if y == x:
continue
next_q.append((j, y))
if len(next_q) != len(q):
q = next_q
heapify(q)
else:
while q and q[0][0] <= x - 1:
heappop(q)
print(len(q))
```
No
| 103,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first.
There are n stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks.
Each photo is described by two non-negative integers a and b, indicating that it is worth a units of happiness to Alice and b units of happiness to Bonnie. Values of a and b might differ for different photos.
It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively.
The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has x happiness and Bonnie has y happiness at the end, you should print x - y.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the number of two-photo stacks. Then follow n lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers a1, b1, a2 and b2, each not exceeding 109. a1 and b1 describe the top photo in the stack, while a2 and b2 describe the bottom photo in the stack.
Output
Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.
Examples
Input
2
12 3 4 7
1 15 9 1
Output
1
Input
2
5 4 8 8
4 12 14 0
Output
4
Input
1
0 10 0 10
Output
-10
Tags: games, greedy
Correct Solution:
```
num_stacks = int(input())
answer = 0
array = []
for i in range(num_stacks):
a1, b1, a2, b2 = map(int, input().split(' '))
if a1 + b1 >= a2 + b2:
answer += a1 + a2
array.append(a2 + b2)
array.append(a1 + b1)
elif a1 > b2:
answer += a1 - b2
elif b1 > a2:
answer += a2 - b1
# Elif both side skip
array.sort()
for i in range(0, len(array), 2):
answer -= array[i]
print(answer)
```
| 103,360 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first.
There are n stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks.
Each photo is described by two non-negative integers a and b, indicating that it is worth a units of happiness to Alice and b units of happiness to Bonnie. Values of a and b might differ for different photos.
It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively.
The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has x happiness and Bonnie has y happiness at the end, you should print x - y.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the number of two-photo stacks. Then follow n lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers a1, b1, a2 and b2, each not exceeding 109. a1 and b1 describe the top photo in the stack, while a2 and b2 describe the bottom photo in the stack.
Output
Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.
Examples
Input
2
12 3 4 7
1 15 9 1
Output
1
Input
2
5 4 8 8
4 12 14 0
Output
4
Input
1
0 10 0 10
Output
-10
Tags: games, greedy
Correct Solution:
```
num_stacks = int(input())
answer = 0
array = []
for i in range(num_stacks):
a1, b1, a2, b2 = input().split(' ')
a1 = int(a1)
a2 = int(a2)
b1 = int(b1)
b2 = int(b2)
if a1 + b1 >= a2 + b2:
answer += a1 + a2
array.append(a2 + b2)
array.append(a1 + b1)
elif a1 > b2:
answer += a1 - b2
elif b1 > a2:
answer += a2 - b1
# Elif both side skip
array.sort()
for i in range(0, len(array), 2):
answer -= array[i]
print(answer)
```
| 103,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Tags: *special, dfs and similar, graphs, implementation
Correct Solution:
```
cycle = False
def DFS(s):
global cycle
visited[s] = 1
stack = [s]
while len(stack) != 0:
u = stack[-1]
visited[u] = 1
if len(graph[u]) != 0:
v = graph[u].pop()
if visited[v] == 1:
cycle = True
return
if visited[v] == 2:
continue
stack.append(v)
else:
result.append(u)
stack.pop()
visited[u] = 2
def TopoSort(graph, result, visited):
for i in range (m):
if not visited[requiredCourse[i]]:
DFS(requiredCourse[i])
return result
n, m = map(int, input().split())
requiredCourse = list(map(int, input().split()))
visited = [0 for i in range (n + 1)]
graph = [[] for i in range (n + 1)]
result = []
for i in range (1, n + 1):
tmp = list(map(int, input().split()))
if tmp[0] == 0:
continue
for j in range (1, tmp[0] + 1):
graph[i].append(tmp[j])
res = TopoSort(graph, result, visited)
if cycle:
print(-1)
else:
print(len(res))
for i in res:
print(i, end = " ")
```
| 103,362 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Tags: *special, dfs and similar, graphs, implementation
Correct Solution:
```
# https://codeforces.com/problemset/problem/770/C
n, k = map(int, input().split())
K = set(list(map(int, input().split())))
g = {}
rg = {}
deg = {}
def push_d(deg, u, val):
if u not in deg:
deg[u] = 0
deg[u] += val
def push_g(g, u, v):
if u not in g:
g[u] = []
g[u].append(v)
for u in range(1, n+1):
list_v = list(map(int, input().split()))[1:]
deg[u] = 0
for v in list_v:
push_d(deg, u, 1)
push_g(g, v, u)
push_g(rg, u, v)
S = [x for x in K]
used = [0] * (n+1)
i = 0
while i<len(S):
u = S[i]
if u in rg:
for v in rg[u]:
if used[v] == 0:
used[v] = 1
S.append(v)
i+=1
S = {x:1 for x in S}
deg0 = [x for x in S if deg[x]==0]
ans = []
def process(g, deg, deg0, u):
if u in g:
for v in g[u]:
if v in S:
push_d(deg, v, -1)
if deg[v] == 0:
deg0.append(v)
while len(deg0) > 0 and len(K) > 0:
u = deg0.pop()
ans.append(u)
if u in K:
K.remove(u)
process(g, deg, deg0, u)
if len(K) > 0:
print(-1)
else:
print(len(ans))
print(' '.join([str(x) for x in ans]))
#6 2
#5 6
#0
#1 1
#1 4 5
#2 2 1
#1 4
#2 5 3
```
| 103,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Tags: *special, dfs and similar, graphs, implementation
Correct Solution:
```
def dfs(start_node, edges, colors, result):
stack = [start_node]
while stack:
current_node = stack[-1]
if colors[current_node] == 2:
stack.pop()
continue
colors[current_node] = 1
children = edges[current_node]
if not children:
colors[current_node] = 2
result.append(stack.pop())
else:
child = children.pop()
if colors[child] == 1:
return False
stack.append(child)
return True
def find_courses_sequence(member_of_node, find_nodes, edges):
colors = [0] * member_of_node
result = []
for node in find_nodes:
if not dfs(node, edges, colors, result):
return []
return result
if __name__ == '__main__':
n, k = map(int, input().split())
main_courses = [int(c)-1 for c in input().split()]
courses = dict()
for index in range(n):
courses[index] = [int(d)-1 for d in input().split()[1:]]
result = find_courses_sequence(n, main_courses, courses)
if result:
print(len(result))
for v in result:
print(v+1, end=" ")
else:
print(-1)
```
| 103,364 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Tags: *special, dfs and similar, graphs, implementation
Correct Solution:
```
n,k=list(map(lambda x: int(x), input().split()))
m=list(map(lambda x: int(x), input().split()))
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
class Graph:
def __init__(self, V):
self.V = V
self.adj = [[] for i in range(V)]
@bootstrap
def DFSUtil(self, temp, v, visited):
visited[v] = True
for i in self.adj[v]:
if visited[i] == False:
yield self.DFSUtil(temp, i, visited)
temp.append(v)
yield temp
def addEdge(self, v, w):
self.adj[v].append(w)
# self.adj[w].append(v)
@bootstrap
def isCyclicUtil(self, v, visited, recStack):
# Mark current node as visited and
# adds to recursion stack
visited[v] = True
recStack[v] = True
# Recur for all neighbours
# if any neighbour is visited and in
# recStack then graph is cyclic
for neighbour in self.adj[v]:
if visited[neighbour] == False:
ans =yield self.isCyclicUtil(neighbour, visited, recStack)
if ans == True:
yield True
elif recStack[neighbour] == True:
yield True
# The node needs to be poped from
# recursion stack before function ends
recStack[v] = False
yield False
# Returns true if graph is cyclic else false
def isCyclic(self,nodes):
visited = [False] * self.V
recStack = [False] * self.V
for node in nodes:
if visited[node] == False:
if self.isCyclicUtil(node, visited, recStack) == True:
return True
return False
G=Graph(n)
for i in range(0,n):
x=list(map(lambda x: int(x), input().split()))
if x[0]==0:
continue
else:
for k in range(1,x[0]+1):
G.addEdge(i,x[k]-1)
visited=[False for _ in range(n)]
path=[]
# print(G.adj)
for subj in m:
temp = []
if visited[subj-1]==False:
G.DFSUtil(temp,subj-1,visited)
path.extend(temp)
if G.isCyclic([x-1 for x in m]):
print(-1)
else:
print(len(path))
for p in path:
print(p+1,end=" ")
print()
```
| 103,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Tags: *special, dfs and similar, graphs, implementation
Correct Solution:
```
import sys
def main():
n,k = map(int,sys.stdin.readline().split())
courses = list(map(int,sys.stdin.readline().split()))
courses = [x-1 for x in courses]
visited = [False]*n
used = [False]*n
ans = []
t = []
for i in range(n):
temp = list(map(int,sys.stdin.readline().split()))
temp = [x-1 for x in temp]
t.append(temp[1:])
for i in range(k):
c = courses[i]
if used[c]:
continue
q = [c]
visited[c]=True
while len(q)>0:
cur = q[-1]
if len(t[cur])!=0:
s = t[cur].pop()
if visited[s] and not used[s]:
print(-1)
return
if used[s]:
continue
q.append(s)
visited[s]=True
else:
ans.append(cur)
q.pop()
used[cur] = True
ans = [str(x+1) for x in ans]
print(len(ans))
print(" ".join(ans))
main()
```
| 103,366 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Tags: *special, dfs and similar, graphs, implementation
Correct Solution:
```
import collections as col
import itertools as its
import sys
import operator
from copy import copy, deepcopy
class Solver:
def __init__(self):
pass
def solve(self):
n, k = map(int, input().split())
q = list(map(lambda x: int(x) - 1, input().split()))
used = [False] * n
for e in q:
used[e] = True
edges = [[] for _ in range(n)]
redges = [[] for _ in range(n)]
for i in range(n):
l = list(map(lambda x: int(x) - 1, input().split()))[1:]
edges[i] = l
for e in l:
redges[e].append(i)
degs = [len(edges[i]) for i in range(n)]
d = 0
while d < len(q):
v = q[d]
d += 1
for e in edges[v]:
if not used[e]:
used[e] = True
q.append(e)
q = q[::-1]
nq = []
for v in q:
if degs[v] == 0:
nq.append(v)
d = 0
while d < len(nq):
v = nq[d]
d += 1
for e in redges[v]:
if not used[e]:
continue
degs[e] -= 1
if degs[e] == 0:
nq.append(e)
#print(nq)
if len(q) != len(nq):
print(-1)
return
print(len(nq))
print(' '.join(map(lambda x: str(x + 1), nq)))
if __name__ == '__main__':
s = Solver()
s.solve()
```
| 103,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Tags: *special, dfs and similar, graphs, implementation
Correct Solution:
```
f = lambda: map(int, input().split())
g = lambda: [int(q) - 1 for q in f()]
class T:
def __init__(s, i):
s.i, s.t = i, g()[1:]
s.a = s.q = 0
n, k = f()
d = g()
p = [T(i) for i in range(n)]
s = []
while d:
x = p[d.pop()]
if x.a: continue
d.append(x.i)
q = 1
for i in x.t:
y = p[i]
if y.a: continue
d.append(y.i)
q = 0
if q:
s.append(x.i + 1)
x.a = 1
elif x.q:
print(-1)
exit()
else: x.q = 1
print(len(s), *s)
```
| 103,368 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Tags: *special, dfs and similar, graphs, implementation
Correct Solution:
```
from sys import *
f = lambda: list(map(int, stdin.readline().split()))
class T:
def __init__(self, i):
self.i, self.t = i, f()[1:]
self.a = self.q = 0
n, k = f()
d = f()
p = [None] + [T(i + 1) for i in range(n)]
s = []
while d:
x = p[d.pop()]
if x.a: continue
d.append(x.i)
q = 1
for i in x.t:
y = p[i]
if y.a: continue
d.append(y.i)
q = 0
if q:
s.append(x.i)
x.a = 1
elif x.q:
print(-1)
exit()
else: x.q = 1
print(len(s), *s)
```
| 103,369 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 8)
cycle = False
def DFS(i, graph, result, visited):
global cycle
stack = [i]
while len(stack) != 0:
u = stack[-1]
visited[u] = 1
if len(graph[u]) != 0:
v = graph[u].pop()
if visited[v] == 1:
cycle = True
return
if visited[v] == 2:
continue
stack.append(v)
visited[u] = 1
else:
result.append(u)
stack.pop()
visited[u] = 2
def TopoSort(graph, result):
for i in range(m):
if not visited[requiredCourse[i]]:
DFS(requiredCourse[i], graph, result, visited)
return result
n, m = map(int, input().split())
requiredCourse = list(map(int, input().split()))
graph = [[] for i in range(n + 1)]
result = []
visited = [False for i in range(n + 1)]
for i in range(1, n + 1):
tmp = list(map(int, input().split()))
if tmp[0] == 0:
continue
for j in range(1, tmp[0] + 1):
graph[i].append(tmp[j])
res = TopoSort(graph, result)
if cycle == True:
print(-1)
else:
print(len(res))
for i in res:
print(i, end=" ")
```
Yes
| 103,370 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Submitted Solution:
```
'''import sys
flag=True
sys.setrecursionlimit(2000000)
c=[];st=[];
def topo(s):#Traversing the array and storing the vertices
global c,st,flag;
c[s]=1; #Being Visited
for i in adjli[s]:#visiting neighbors
if c[i]==0:
topo(i)
if c[i]==1:
flag=False# If Back Edge , Then Not Possible
st.append(str(s))
c[s]=2 # Visited
try:
n,k=map(int,input().split(' '))#Number Of Courses,Dependencies
main=list(map(int,input().split(' ')))#Main Dependencies
depen=[]#Dependencies List
for i in range(n):
depen.append(list(map(int,input().split(' ')))[1:]);c.append(0)#Append Input To Dependencies List, Marking Visited as 0(False)
c.append(0)
adjli=[]
adjli.append(main)#Assuming Main Course at index 0 with dependencies as Main Dependency(main)
for i in range(len(depen)):
adjli.append(depen[i])#Appending Other Dependencies
topo(0)#TopoLogical Sort Order
st.pop(-1)#popping the assumed Main Couse
if flag:# IF possible then print
print(len(st))
print(' '.join(st))
else:
print(-1)
except Exception as e:
print(e,"error")'''
import sys
flag=True
sys.setrecursionlimit(2000000000)
c=[];st=[];
cur_adj=[]
def topo(s):#Traversing the array and storing the vertices
global c,st,flag;
stack = [s]
while(stack):
s = stack[-1]
c[s]=1; #Being Visited
if(cur_adj[s] < len(adjli[s])):
cur = adjli[s][cur_adj[s]]
if(c[cur]==0):
stack.append(cur)
if(c[cur]==1):
flag=False# If Back Edge , Then Not Possible
cur_adj[s]+=1
else:
c[s]=2
st.append(str(s))
del stack[-1]
try:
n,k=map(int,input().split(' '))
main=list(map(int,input().split(' ')))
depen=[]
for i in range(n):
depen.append(list(map(int,input().split(' ')))[1:]);c.append(0)
cur_adj.append(0)
c.append(0)
cur_adj.append(0)
adjli=[]
adjli.append(main)#Assuming Main Course at index 0 with dependencies as Main Dependency(main)
for i in range(len(depen)):
adjli.append(depen[i])#Appending Other Dependencies
topo(0)#TopoLogical Sort Order
st.pop(-1)#popping the assumed Main Couse
if flag:# IF possible then print
print(len(st))
print(' '.join(st))
else:
print(-1)
except Exception as e:
print(e,"error")
```
Yes
| 103,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Submitted Solution:
```
import sys
flag=True
sys.setrecursionlimit(2000000000)
c=[];st=[];
cur_adj=[]
def topo(s):#Traversing the array and storing the vertices
global c,st,flag;
stack = [s]
while(stack):
s = stack[-1]
c[s]=1; #Being Visited
if(cur_adj[s] < len(adjli[s])):
cur = adjli[s][cur_adj[s]]
if(c[cur]==0):
stack.append(cur)
if(c[cur]==1):
flag=False# If Back Edge , Then Not Possible
cur_adj[s]+=1
else:
c[s]=2
st.append(str(s))
del stack[-1]
try:
n,k=map(int,input().split(' '))
main=list(map(int,input().split(' ')))
depen=[]
for i in range(n):
depen.append(list(map(int,input().split(' ')))[1:]);c.append(0)
cur_adj.append(0)
c.append(0)
cur_adj.append(0)
adjli=[]
adjli.append(main)#Assuming Main Course at index 0 with dependencies as Main Dependency(main)
for i in range(len(depen)):
adjli.append(depen[i])#Appending Other Dependencies
topo(0)#TopoLogical Sort Order
st.pop(-1)#popping the assumed Main Couse
if flag:# IF possible then print
print(len(st))
print(' '.join(st))
else:
print(-1)
except Exception as e:
print(e,"error")
```
Yes
| 103,372 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Submitted Solution:
```
#This code is dedicated to Vlada S.
class Course:
def __init__(self, reqs, number):
self.reqs = list(map(int, reqs.split()[1:]))
self.available = False
self.in_stack = False
self.number = number
n, k = list(map(int, input().split()))
requirements = list(map(int, input().split()))
courses = {}
answer = ""
for i in range(n):
courses[i + 1]= Course(input(), i + 1)
for i in range(len(requirements)):
requirements[i] = courses[requirements[i]]
while requirements:
data = {}
course = requirements.pop()
if not course.available:
requirements.append(course)
done = True
for c in course.reqs:
c = courses[c]
if not c.available:
requirements.append(c)
done = False
if done:
answer += " " + str(course.number)
course.available = True
else:
if course.in_stack:
print(-1)
break
course.in_stack = True
else:
print(answer.count(" "))
print(answer[1:])
# Made By Mostafa_Khaled
```
Yes
| 103,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Submitted Solution:
```
putninja=[]
courses=[]
B=True
def mff(A, num):
global putninja
global courses
if A[0]=='0':
putninja.append(num)
courses[int(num)-1][0]='stop'
elif A[0]=='stop':
pass
else:
for j in range(int(A[0])):
mff(courses[int(A[j+1])-1],A[j+1])
putninja.append(num)
courses[int(num)-1][0]='stop'
kn=input().split()
k=int(kn[0])
n=int(kn[1])
mk=input().split()
for i in range(k):
s=input()
courses.append(s.split(' '))
for i in range(n):
try:
mff(courses[int(mk[i])-1],mk[i])
except:
B=False
break
if B:
print(len(putninja))
print(' '.join(putninja))
else:
print(-1)
```
No
| 103,374 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Submitted Solution:
```
from sys import stdin, stdout
n,k = stdin.readline().split()
n = int(n)
k = int(k)
toLearn = [-1] * n
kList = input().split()
for i in range(len(kList)):
kList[i] = int(kList[i])
nList = []
flag = True
for i in range(n):
nL = stdin.readline().split()
nL.pop(0)
for j in range(len(nL)):
nL[j] = int(nL[j])
if len(nL) == 0:
flag = False
nList.append(nL)
# n = 100000
# k = 1
# kList = [100000]
# nList =[[]]
# flag = False
# for i in range(1, n):
# nList.append([i])
res = []
if k == 0:
print(0)
print("")
elif flag:
print(-1)
else:
res = []
notCircle = True
current = 0
while len(kList) > 0 and notCircle:
temp = []
for i in kList:
res.append(i)
toLearn[i - 1] = current
for j in nList[i - 1]:
if toLearn[j - 1] >= 0:
notCircle = False
break
break
if toLearn[j - 1] == -1:
temp.append(j)
kList = temp
current += 1
if notCircle:
res.reverse()
s = ""
for i in res:
s += str(i) + " "
stdout.write(str(len(res)) + '\n')
stdout.write(s[: -1] + '\n')
else:
stdout.write('-1\n')
```
No
| 103,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Submitted Solution:
```
itog = ''
result = []
iskl = 0
control = 0
maxway = -1
wayStolb = 0
table = []
fs = input()
inp = fs.split(' ')
nk = [int(i) for i in inp]
ss = input()
inp = ss.split(' ')
maincour = [int(i) for i in inp]
for i in range(0,nk[0]):
table.append([])
s = input()
inp = s.split(' ')
t = [int(i) for i in inp]
for j in range(0,t[0]+1):
table[i].append(t[j])
for i in range (0,len(maincour)):
#print(table[maincour[i]-1])
if table[maincour[i]-1][0] > maxway:
wayStolb = maincour[i] - 1
maxway = table[maincour[i]-1][0]
#print(wayStolb)
#print(maxway)
e = maxway
stolb = wayStolb #4
startStolb = stolb
while control != len(maincour) :
while table[wayStolb][0] != 0:
maxway = -1
e = table[stolb][0]
for i in range(1,e+1):
if table[table[stolb][i]-1][0] > maxway:
wayStolb = table[stolb][i] - 1
maxway = table[table[stolb][i]-1][0]
e = maxway
if e > 0:
stolb = wayStolb
iskl = iskl + 1;
if iskl > 100000:
iskl = -1
break
#print('WayStolb = '+str(wayStolb))
#print('Был здесь'+str(stolb))
if iskl == -1:
result = '-1'
break
#print('Остановка ' +str(wayStolb))
result.append(wayStolb+1)
for i in range(0,len(maincour)):
if wayStolb == maincour[i] - 1:
control = control + 1;
if control != len(maincour):
table[stolb][0] = table[stolb][0] - 1
try:
table[stolb].remove(wayStolb+1)
except ValueError:
for i in range(0,len(maincour)):
if table[maincour[i]-1][0] == 0 and result.count(maincour[i]) == 0:
result.append(maincour[i])
control = control + 1
if control != len(maincour):
for i in range (0,len(maincour)):
#print(table[maincour[i]-1])
if table[maincour[i]-1][0] > maxway:
wayStolb = maincour[i] - 1
maxway = table[maincour[i]-1][0]
#print(wayStolb)
#print(maxway)
e = maxway
stolb = wayStolb #4
startStolb = stolb
#print(table[stolb])
#print(table[stolb][1])
wayStolb = startStolb
#print('wayStolb = '+str(wayStolb))
stolb = wayStolb
if result != '-1':
itog = itog + str(result[0])
for i in range(1,len(result)):
itog = itog + ' ' + str(result[i])
print(len(result))
print(itog)
else:
print(result)
```
No
| 103,376 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
Input
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values ti doesn't exceed 105.
Output
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
Examples
Input
6 2
5 3
0
0
0
2 2 1
1 4
1 5
Output
5
1 2 3 4 5
Input
9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2
Output
6
1 2 9 4 5 3
Input
3 3
1 2 3
1 2
1 3
1 1
Output
-1
Note
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
Submitted Solution:
```
import collections as col
import itertools as its
import sys
import operator
from copy import copy, deepcopy
class Solver:
def __init__(self):
pass
def solve(self):
n, k = map(int, input().split())
q = list(map(lambda x: int(x) - 1, input().split()))
used = [False] * n
for e in q:
used[e] = True
edges = [[] for _ in range(n)]
redges = [[] for _ in range(n)]
for i in range(n):
l = list(map(lambda x: int(x) - 1, input().split()))[1:]
edges[i] = l
for e in l:
redges[e].append(i)
degs = [len(edges[i]) for i in range(n)]
d = 0
while d < len(q):
v = q[d]
d += 1
for e in edges[v]:
if not used[e]:
used[e] = True
q.append(e)
q = q[::-1]
nq = []
for v in q:
if degs[v] == 0:
nq.append(v)
d = 0
while d < len(nq):
v = nq[d]
d += 1
for e in redges[v]:
degs[e] -= 1
if degs[e] == 0:
nq.append(e)
if len(q) != len(nq):
print(-1)
return
print(len(nq))
print(' '.join(map(lambda x: str(x + 1), nq)))
if __name__ == '__main__':
s = Solver()
s.solve()
```
No
| 103,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bankopolis, the city you already know, finally got a new bank opened! Unfortunately, its security system is not yet working fine... Meanwhile hacker Leha arrived in Bankopolis and decided to test the system!
Bank has n cells for clients' money. A sequence from n numbers a1, a2, ..., an describes the amount of money each client has. Leha wants to make requests to the database of the bank, finding out the total amount of money on some subsegments of the sequence and changing values of the sequence on some subsegments. Using a bug in the system, Leha can requests two types of queries to the database:
* 1 l r x y denoting that Leha changes each digit x to digit y in each element of sequence ai, for which l ≤ i ≤ r is holds. For example, if we change in number 11984381 digit 8 to 4, we get 11944341. It's worth noting that Leha, in order to stay in the shadow, never changes digits in the database to 0, i.e. y ≠ 0.
* 2 l r denoting that Leha asks to calculate and print the sum of such elements of sequence ai, for which l ≤ i ≤ r holds.
As Leha is a white-hat hacker, he don't want to test this vulnerability on a real database. You are to write a similar database for Leha to test.
Input
The first line of input contains two integers n and q (1 ≤ n ≤ 105, 1 ≤ q ≤ 105) denoting amount of cells in the bank and total amount of queries respectively.
The following line contains n integers a1, a2, ..., an (1 ≤ ai < 109) denoting the amount of money in each cell initially. These integers do not contain leading zeros.
Each of the following q lines has one of the formats:
* 1 l r x y (1 ≤ l ≤ r ≤ n, 0 ≤ x ≤ 9, 1 ≤ y ≤ 9), denoting Leha asks to change each digit x on digit y for each element ai of the sequence for which l ≤ i ≤ r holds;
* 2 l r (1 ≤ l ≤ r ≤ n), denoting you have to calculate and print the sum of elements ai for which l ≤ i ≤ r holds.
Output
For each second type query print a single number denoting the required sum.
Examples
Input
5 5
38 43 4 12 70
1 1 3 4 8
2 2 4
1 4 5 0 8
1 2 5 8 7
2 1 5
Output
103
207
Input
5 5
25 36 39 40 899
1 1 3 2 7
2 1 2
1 3 5 9 1
1 4 4 0 9
2 1 5
Output
111
1002
Note
Let's look at the example testcase.
Initially the sequence is [38, 43, 4, 12, 70].
After the first change each digit equal to 4 becomes 8 for each element with index in interval [1; 3]. Thus, the new sequence is [38, 83, 8, 12, 70].
The answer for the first sum's query is the sum in the interval [2; 4], which equal 83 + 8 + 12 = 103, so the answer to this query is 103.
The sequence becomes [38, 83, 8, 12, 78] after the second change and [38, 73, 7, 12, 77] after the third.
The answer for the second sum's query is 38 + 73 + 7 + 12 + 77 = 207.
Submitted Solution:
```
import sys
lines = sys.stdin.readlines()
print(lines)
l = lines[1].split()
edit = lambda l, i, j, a, b: [l[x].replace(a, b) for x in range(i - 1, j)]
_sum = lambda l, i, j: sum(map(int, l[i - 1:j]))
```
No
| 103,378 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's been long after the events of the previous problems, and Karen has now moved on from student life and is looking to relocate to a new neighborhood.
<image>
The neighborhood consists of n houses in a straight line, labelled 1 to n from left to right, all an equal distance apart.
Everyone in this neighborhood loves peace and quiet. Because of this, whenever a new person moves into the neighborhood, he or she always chooses the house whose minimum distance to any occupied house is maximized. If there are multiple houses with the maximum possible minimum distance, he or she chooses the leftmost one.
Note that the first person to arrive always moves into house 1.
Karen is the k-th person to enter this neighborhood. If everyone, including herself, follows this rule, which house will she move into?
Input
The first and only line of input contains two integers, n and k (1 ≤ k ≤ n ≤ 1018), describing the number of houses in the neighborhood, and that Karen was the k-th person to move in, respectively.
Output
Output a single integer on a line by itself, the label of the house Karen will move into.
Examples
Input
6 4
Output
2
Input
39 3
Output
20
Note
In the first test case, there are 6 houses in the neighborhood, and Karen is the fourth person to move in:
1. The first person moves into house 1.
2. The second person moves into house 6.
3. The third person moves into house 3.
4. The fourth person moves into house 2.
In the second test case, there are 39 houses in the neighborhood, and Karen is the third person to move in:
1. The first person moves into house 1.
2. The second person moves into house 39.
3. The third person moves into house 20.
Submitted Solution:
```
dict1 = {}
a = input("enter two integers separated by space:")
c, d = a.split()
n = int(c)
k = int(d)
if k == 1:
print(1)
for i in range(1, n+1):
dict1[i] = ""
dict1[1] = "occupied"
for i in range(2, k+1):
ans1 = []
minDistDict = {}
occupiedList = []
for j in range(1, n+1):
if dict1[j] == "occupied":
occupiedList.append(j)
availableList = [x for x in range(1, n+1) if x not in occupiedList]
for l in availableList:
distList = []
for o in occupiedList:
distList.append(abs(l-o))
minDistDict[l] = min(distList)
for key in minDistDict:
if minDistDict[key] == max(list(minDistDict.values())):
ans1.append(key)
dict1[min(ans1)] = "occupied"
if i == k:
print(min(ans1))
```
No
| 103,379 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's been long after the events of the previous problems, and Karen has now moved on from student life and is looking to relocate to a new neighborhood.
<image>
The neighborhood consists of n houses in a straight line, labelled 1 to n from left to right, all an equal distance apart.
Everyone in this neighborhood loves peace and quiet. Because of this, whenever a new person moves into the neighborhood, he or she always chooses the house whose minimum distance to any occupied house is maximized. If there are multiple houses with the maximum possible minimum distance, he or she chooses the leftmost one.
Note that the first person to arrive always moves into house 1.
Karen is the k-th person to enter this neighborhood. If everyone, including herself, follows this rule, which house will she move into?
Input
The first and only line of input contains two integers, n and k (1 ≤ k ≤ n ≤ 1018), describing the number of houses in the neighborhood, and that Karen was the k-th person to move in, respectively.
Output
Output a single integer on a line by itself, the label of the house Karen will move into.
Examples
Input
6 4
Output
2
Input
39 3
Output
20
Note
In the first test case, there are 6 houses in the neighborhood, and Karen is the fourth person to move in:
1. The first person moves into house 1.
2. The second person moves into house 6.
3. The third person moves into house 3.
4. The fourth person moves into house 2.
In the second test case, there are 39 houses in the neighborhood, and Karen is the third person to move in:
1. The first person moves into house 1.
2. The second person moves into house 39.
3. The third person moves into house 20.
Submitted Solution:
```
from sys import stdin
from collections import deque
def main():
t = list(stdin.readline())
b = False
n = ""
k = ""
for i in t:
if i == " " :
b = True
if b == False :
n += i
else :
k += i
n = int(n)
k = int(k)
x = deque()
answer = 0
m = int(n / 2)
x.append(n)
x.append(0)
if k > 2:
k = k - 2
while k > 0 :
temp = x.pop()
if temp == n :
m = int(m/2)
x.appendleft(n)
continue
x.appendleft(temp)
x.appendleft(temp + m)
answer = temp + m
k = k - 1
print(answer + 1)
elif k == 1:
print(1)
else:
print(n)
main()
```
No
| 103,380 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's been long after the events of the previous problems, and Karen has now moved on from student life and is looking to relocate to a new neighborhood.
<image>
The neighborhood consists of n houses in a straight line, labelled 1 to n from left to right, all an equal distance apart.
Everyone in this neighborhood loves peace and quiet. Because of this, whenever a new person moves into the neighborhood, he or she always chooses the house whose minimum distance to any occupied house is maximized. If there are multiple houses with the maximum possible minimum distance, he or she chooses the leftmost one.
Note that the first person to arrive always moves into house 1.
Karen is the k-th person to enter this neighborhood. If everyone, including herself, follows this rule, which house will she move into?
Input
The first and only line of input contains two integers, n and k (1 ≤ k ≤ n ≤ 1018), describing the number of houses in the neighborhood, and that Karen was the k-th person to move in, respectively.
Output
Output a single integer on a line by itself, the label of the house Karen will move into.
Examples
Input
6 4
Output
2
Input
39 3
Output
20
Note
In the first test case, there are 6 houses in the neighborhood, and Karen is the fourth person to move in:
1. The first person moves into house 1.
2. The second person moves into house 6.
3. The third person moves into house 3.
4. The fourth person moves into house 2.
In the second test case, there are 39 houses in the neighborhood, and Karen is the third person to move in:
1. The first person moves into house 1.
2. The second person moves into house 39.
3. The third person moves into house 20.
Submitted Solution:
```
from sys import stdin
from collections import deque
def main():
t = list(stdin.readline())
b = False
n = ""
k = ""
for i in t:
if i == " " :
b = True
if b == False :
n += i
else :
k += i
n = int(n)
k = int(k)
x = deque()
answer = 0
m = int(n / 2)
x.append(n)
x.append(0)
k = k - 2
while k > 0 :
temp = x.pop()
if temp == n :
m = int(m/2)
x.appendleft(n)
continue
x.appendleft(temp)
x.appendleft(temp + m)
answer = temp + m
k = k - 1
if answer == 0:
print(n)
else:
print(answer + 1)
main()
```
No
| 103,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
It's been long after the events of the previous problems, and Karen has now moved on from student life and is looking to relocate to a new neighborhood.
<image>
The neighborhood consists of n houses in a straight line, labelled 1 to n from left to right, all an equal distance apart.
Everyone in this neighborhood loves peace and quiet. Because of this, whenever a new person moves into the neighborhood, he or she always chooses the house whose minimum distance to any occupied house is maximized. If there are multiple houses with the maximum possible minimum distance, he or she chooses the leftmost one.
Note that the first person to arrive always moves into house 1.
Karen is the k-th person to enter this neighborhood. If everyone, including herself, follows this rule, which house will she move into?
Input
The first and only line of input contains two integers, n and k (1 ≤ k ≤ n ≤ 1018), describing the number of houses in the neighborhood, and that Karen was the k-th person to move in, respectively.
Output
Output a single integer on a line by itself, the label of the house Karen will move into.
Examples
Input
6 4
Output
2
Input
39 3
Output
20
Note
In the first test case, there are 6 houses in the neighborhood, and Karen is the fourth person to move in:
1. The first person moves into house 1.
2. The second person moves into house 6.
3. The third person moves into house 3.
4. The fourth person moves into house 2.
In the second test case, there are 39 houses in the neighborhood, and Karen is the third person to move in:
1. The first person moves into house 1.
2. The second person moves into house 39.
3. The third person moves into house 20.
Submitted Solution:
```
from sys import stdin
from collections import deque
def main():
t = list(stdin.readline())
b = False
n = ""
k = ""
for i in t:
if i == " " :
b = True
if b == False :
n += i
else :
k += i
n = int(n)
k = int(k)
x = deque()
answer = 0
m = int(n / 2)
x.append(n)
x.append(0)
k = k - 2
while k > 0 :
temp = x.pop()
if temp == n :
m = int(m/2)
x.appendleft(n)
continue
x.appendleft(temp)
x.appendleft(temp + m)
answer = temp + m
k = k - 1
print(answer + 1)
main()
```
No
| 103,382 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Tags: greedy, implementation
Correct Solution:
```
_, x = [int(x) for x in input().split(' ')]
s = set([int(i) for i in input().split(' ') if int(i) <= x])
d = set(range(x)).symmetric_difference(s)
print(len(d))
```
| 103,383 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Tags: greedy, implementation
Correct Solution:
```
n,x=map(int,input().split())
a=set(map(int,input().split()))
s=int(x in a)
for i in range(x):
if not i in a:s+=1
print(s)
```
| 103,384 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Tags: greedy, implementation
Correct Solution:
```
import sys
str1 = sys.stdin.readline()
num1, num2 = int(str1.split(" ")[0]), int(str1.split(" ")[1])
str2 = sys.stdin.readline()
lst = []
for i in range(num1):
lst.append(int(str2.split(" ")[i]))
result = 0
com = list(range(num2))
for i in range(num2):
if i not in lst:
result += 1
for i in range(num1):
if lst[i] > num2:
continue
if lst[i] == num2:
result += 1
print(result)
```
| 103,385 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Tags: greedy, implementation
Correct Solution:
```
from bisect import bisect
n, k = [int(x) for x in input().strip().split()]
arr = [False for _ in range(101)]
inp = [int(x) for x in input().strip().split()]
for i in inp:
arr[i] = True
count = 1 if arr[k] else 0
for i in range(0, k):
if not (arr[i]):
count+=1
print(count)
```
| 103,386 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Tags: greedy, implementation
Correct Solution:
```
n,m = map(int,input().split())
l = list(map(int,input().split()))
c = 0
for i in range(m):
if i not in l:
c+=1
if m in l:
c+=1
print(c)
```
| 103,387 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Tags: greedy, implementation
Correct Solution:
```
n,x = [int(i) for i in input().split(" ")]
l = [int(i) for i in input().split(" ")]
result = 0
for i in range(x):
if i not in l:
result += 1
if x in l:
result += 1
print(result)
```
| 103,388 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Tags: greedy, implementation
Correct Solution:
```
n, x = map(int, input().split())
s = list(map(int, input().split()))
s.sort()
i = 0
while i < n and s[i] < x:
i += 1
if (i < n and s[i] == x) or (i - 1 >= 0 and s[i - 1] == x):
print(x - i + 1)
else:
print(x - i)
```
| 103,389 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Tags: greedy, implementation
Correct Solution:
```
n,x=map(int,input().split())
L=list(map(int,input().split()))
o=0
if x in L:
L.remove(x)
o+=1
for i in range(x):
if i not in L:
L.append(i)
o+=1
print(o)
```
| 103,390 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
_ , X = [int(x) for x in input().split()]
s = [int(x) for x in input().split()]
o = len(s)
s = [x for x in s if x >= 0]
acc = o - len(s)
if X in s: acc += 1
s = [x for x in s if x < X]
acc += X - len(s)
print(acc)
```
Yes
| 103,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
n,x=list(map(int,input().split(" ")))
arr=list(map(int,input().split(" ")))
arr1=range(0,x)
num=arr.count(x)
for i in arr1:
if i not in arr:
num+=1
print(num)
```
Yes
| 103,392 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
a = [int(x) for x in input().split()]
b = [int(y) for y in input().split()]
c = [int(z) for z in range(a[1])]
d = [x for x in b if x!=a[1]]
count=0
if len(b)-len(d):
count=1
set1=set(d)
set1.update(set(c))
print(count+(len(set1)-len(d)))
```
Yes
| 103,393 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
mex = int(input().split(" ")[1])
numbers = [int(x) for x in input().split(" ")]
numbers.sort()
count = 0
for i in range(mex):
if i not in numbers:
count += 1
if mex in numbers:
count += 1
print(count)
```
Yes
| 103,394 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
d=input()
a=int(d.split(' ')[0])
mex=int(d.split(' ')[1])
d=input()
arr=[]
for i in d.split(' '):
arr.append(int(i))
arr=sorted(arr)
res=0
for i in range(0,len(arr)-1):
if arr[i]<mex and arr[i+1]>mex:
res=mex-arr[i]-1
break
if(arr[0]>mex):
res=mex
if arr[0]==mex:
res=1
if arr[-1]==mex:
res=1
if(arr[-1]<mex):
res=mex-arr[-1]-1
print(res)
```
No
| 103,395 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
n,m=map(int,input().split())
c=0
ls= sorted(list(map(int,input().split())))
for i in ls:
if i > m: break
c+=1
print(m-(c+(1 if m in ls else 0)))
```
No
| 103,396 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
_ ,b = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
small = 0
s = 0
for i in range(len(l)):
if l[i] < b:
small += 1
elif l[i] == b:
s = 1
else:
break
print(b-small +s)
```
No
| 103,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go.
Dr. Evil is interested in sets, He has a set of n integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly x. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0 .
Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil?
Input
The first line contains two integers n and x (1 ≤ n ≤ 100, 0 ≤ x ≤ 100) — the size of the set Dr. Evil owns, and the desired MEX.
The second line contains n distinct non-negative integers not exceeding 100 that represent the set.
Output
The only line should contain one integer — the minimal number of operations Dr. Evil should perform.
Examples
Input
5 3
0 4 5 6 7
Output
2
Input
1 0
0
Output
1
Input
5 0
1 2 3 4 5
Output
0
Note
For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations.
For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0.
In the third test case the set is already evil.
Submitted Solution:
```
n,x=map(int,input().split())
a=list(map(int,input().split()))
t=0
count=0
for i in range(n):
if a[i]<x:
count+=1
if a[i]==x:
t=t+1
r=x-count-t
if r>0:
print(r)
else:
print(0)
```
No
| 103,398 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Masha's little brother draw two points on a sheet of paper. After that, he draws some circles and gave the sheet to his sister.
Masha has just returned from geometry lesson so she instantly noticed some interesting facts about brother's drawing.
At first, the line going through two points, that brother drew, doesn't intersect or touch any circle.
Also, no two circles intersect or touch, and there is no pair of circles such that one circle is located inside another.
Moreover, for each circle, Masha drew a square of the minimal area with sides parallel axis such that this circle is located inside the square and noticed that there is no two squares intersect or touch and there is no pair of squares such that one square is located inside other.
Now Masha wants to draw circle of minimal possible radius such that it goes through two points that brother drew and doesn't intersect any other circle, but other circles can touch Masha's circle and can be located inside it.
It's guaranteed, that answer won't exceed 1012. It should be held for hacks as well.
Input
First line contains four integers x1, y1, x2, y2 ( - 105 ≤ x1, y1, x2, y2 ≤ 105) — coordinates of points that brother drew. First point has coordinates (x1, y1) and second point has coordinates (x2, y2). These two points are different.
The second line contains single integer n (1 ≤ n ≤ 105) — the number of circles that brother drew.
Next n lines contains descriptions of circles. Each line contains three integers xi, yi, ri ( - 105 ≤ xi, yi ≤ 105, 1 ≤ ri ≤ 105) describing circle with center (xi, yi) and radius ri.
Output
Output smallest real number, that it's possible to draw a circle with such radius through given points in such a way that it doesn't intersect other circles.
The output is considered correct if it has a relative or absolute error of at most 10 - 4.
Examples
Input
2 4 7 13
3
3 0 1
12 4 2
-4 14 2
Output
5.1478150705
Input
-2 3 10 -10
2
7 0 3
-5 -5 2
Output
9.1481831923
Note
<image> <image>
Submitted Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
#import matplotlib.pyplot as plt
"""
created by shhuan at 2017/11/4 01:33
"""
x1, y1, x2, y2 = map(int, input().split())
n = int(input())
C = []
for i in range(n):
C.append([int(x) for x in input().split()])
def check(X, Y, R):
for x, y, r in C:
d = math.sqrt((X-x)**2 + (Y-y)**2)
if abs(R-r) < d < R+r:
return False
return True
hi = 10**12
lo = 0
L = math.sqrt((x1-x2)**2 + (y1-y2)**2) / 2
L2 = L**2
xm, ym = (x1+x2)/2, (y1+y2)/2
xm2, ym2 = xm**2, ym**2
vectv = x2-x1, y2-y1
dy, dx = y2-y1, x2-x1
c = dy/dx
c2 = c**2
c21 = c2+1
d = c*ym + xm
e = d - xm
e2 = e**2
f = (e*c+ym)/c21
f2 = f**2
rd = L2+ym2+e2
if check(xm, ym, L):
print(L)
exit(0)
XY = []
# plt.figure()
# fig, ax = plt.subplots()
# circles = []
while abs(hi-lo) > 10**-7:
R = lo + (hi-lo)/2
g = (R**2-rd)/c21 + f2
if g <= 0:
lo = R
continue
Y = math.sqrt(g)
Y1 = Y + f
Y2 = -Y + f
X1 = -c*Y1 + d
X2 = -c*Y2 + d
# if R < 20:
# circles.append(plt.Circle((X1, Y1), R, color='g'))
# circles.append(plt.Circle((X2, Y2), R, color='g'))
if check(X1, Y1, R):
XY = X1, Y1
hi = R
elif check(X2, Y2, R):
XY = X2, Y2
hi = R
else:
lo = R
print(lo)
# xs = [x for x, _, _ in C]
# ys = [y for _, y, _ in C]
# xlim = [min(xs), max(xs)]
# ylim = [min(ys), max(ys)]
# ll, lr = min(xlim[0], ylim[0], XY[0], XY[1])-R, max(xlim[1], ylim[1], XY[0], XY[1])+R
# ax.set_xlim([ll, lr])
# ax.set_ylim([ll, lr])
#
# for x, y, r in C:
# circles.append(plt.Circle((x, y), r, color='b'))
# plt.plot([x1, x2], [y1, y2])
#
# circles.append(plt.Circle(XY, R, color='r'))
# for c in circles:
# ax.add_artist(c)
# c.set_clip_box(ax.bbox)
# # c.set_edgecolor(color)
# c.set_facecolor("none") # "none" not None
# # c.set_alpha(alpha)
#
# plt.show()
```
No
| 103,399 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.