message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Asya loves animals very much. Recently, she purchased n kittens, enumerated them from 1 and n and then put them into the cage. The cage consists of one row of n cells, enumerated with integers from 1 to n from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were n - 1 partitions originally. Initially, each cell contained exactly one kitten with some number.
Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day i, Asya:
* Noticed, that the kittens x_i and y_i, located in neighboring cells want to play together.
* Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells.
Since Asya has never putted partitions back, after n - 1 days the cage contained a single cell, having all kittens.
For every day, Asya remembers numbers of kittens x_i and y_i, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into n cells.
Input
The first line contains a single integer n (2 β€ n β€ 150 000) β the number of kittens.
Each of the following n - 1 lines contains integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i) β indices of kittens, which got together due to the border removal on the corresponding day.
It's guaranteed, that the kittens x_i and y_i were in the different cells before this day.
Output
For every cell from 1 to n print a single integer β the index of the kitten from 1 to n, who was originally in it.
All printed integers must be distinct.
It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.
Example
Input
5
1 4
2 5
3 1
4 5
Output
3 1 4 2 5
Note
The answer for the example contains one of several possible initial arrangements of the kittens.
The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells.
<image>
Submitted Solution:
```
n = int(input())
uni = {}
sol = []
for _ in range(n-1):
x, y = map(int, input().split())
if x in uni:
if y in uni:
uni[x] += uni[y]
else:
uni[x].append(y)
uni[y] = uni[x]
sol = uni[x].copy()
elif y in uni:
if x in uni:
uni[y] += uni[x]
else:
uni[y].append(x)
uni[x] = uni[y]
sol = uni[y].copy()
else:
uni[x] = [x, y]
uni[y] = uni[x]
sol = uni[x].copy()
# print(uni)
print(*sol)
``` | instruction | 0 | 100,383 | 14 | 200,766 |
No | output | 1 | 100,383 | 14 | 200,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
* (Operation A): you select any non-negative integer n and replace x with x β (2^n - 1), with β being a [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* (Operation B): replace x with x + 1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.
Input
The only line contains a single integer x (1 β€ x β€ 10^6).
Output
The first line should contain a single integer t (0 β€ t β€ 40) β the number of operations to apply.
Then for each odd-numbered operation print the corresponding number n_i in it. That is, print β t/2 β integers n_i (0 β€ n_i β€ 30), denoting the replacement x with x β (2^{n_i} - 1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
Input
39
Output
4
5 3
Input
1
Output
0
Input
7
Output
0
Note
In the first test, one of the transforms might be as follows: 39 β 56 β 57 β 62 β 63. Or more precisely:
1. Pick n = 5. x is transformed into 39 β 31, or 56.
2. Increase x by 1, changing its value to 57.
3. Pick n = 3. x is transformed into 57 β 7, or 62.
4. Increase x by 1, changing its value to 63 = 2^6 - 1.
In the second and third test, the number already satisfies the goal requirement. | instruction | 0 | 100,384 | 14 | 200,768 |
Tags: bitmasks, constructive algorithms, dfs and similar, math
Correct Solution:
```
i = int(input())
def getZ(n):
s = "{0:0b}".format(n)
return len(s) - s.find("0")
def check(n):
return "{0:0b}".format(n).find("0") == -1
steps = 0
ans = []
while not check(i):
m = getZ(i)
i = i ^ ((2 ** m) - 1)
steps +=1
ans.append(m)
if check(i):
print(steps)
print(" ".join(map(str, ans)))
exit(0)
i += 1
steps += 1
print(steps)
print(" ".join(map(str, ans)))
exit(0)
``` | output | 1 | 100,384 | 14 | 200,769 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
* (Operation A): you select any non-negative integer n and replace x with x β (2^n - 1), with β being a [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* (Operation B): replace x with x + 1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.
Input
The only line contains a single integer x (1 β€ x β€ 10^6).
Output
The first line should contain a single integer t (0 β€ t β€ 40) β the number of operations to apply.
Then for each odd-numbered operation print the corresponding number n_i in it. That is, print β t/2 β integers n_i (0 β€ n_i β€ 30), denoting the replacement x with x β (2^{n_i} - 1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
Input
39
Output
4
5 3
Input
1
Output
0
Input
7
Output
0
Note
In the first test, one of the transforms might be as follows: 39 β 56 β 57 β 62 β 63. Or more precisely:
1. Pick n = 5. x is transformed into 39 β 31, or 56.
2. Increase x by 1, changing its value to 57.
3. Pick n = 3. x is transformed into 57 β 7, or 62.
4. Increase x by 1, changing its value to 63 = 2^6 - 1.
In the second and third test, the number already satisfies the goal requirement. | instruction | 0 | 100,385 | 14 | 200,770 |
Tags: bitmasks, constructive algorithms, dfs and similar, math
Correct Solution:
```
import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
# sys.setrecursionlimit(200000)
# mod = 10**9+7
# mod = 998244353
decimal.getcontext().prec = 46
def primeFactors(n):
prime = set()
while n % 2 == 0:
prime.add(2)
n = n//2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
prime.add(i)
n = n//i
if n > 2:
prime.add(n)
return list(prime)
def getFactors(n) :
factors = []
i = 1
while i <= math.sqrt(n):
if (n % i == 0) :
if (n // i == i) :
factors.append(i)
else :
factors.append(i)
factors.append(n//i)
i = i + 1
return factors
def modefiedSieve():
mx=10**7+1
sieve=[-1]*mx
for i in range(2,mx):
if sieve[i]==-1:
sieve[i]=i
for j in range(i*i,mx,i):
if sieve[j]==-1:
sieve[j]=i
return sieve
def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
num = []
for p in range(2, n+1):
if prime[p]:
num.append(p)
return num
def lcm(a,b):
return (a*b)//math.gcd(a,b)
def sort_dict(key_value):
return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0]), reverse=True)
def list_input():
return list(map(int,input().split()))
def num_input():
return map(int,input().split())
def string_list():
return list(input())
def decimalToBinary(n):
return bin(n).replace("0b", "")
def binaryToDecimal(n):
return int(n,2)
def DFS(n,s,adj):
visited = [False for i in range(n+1)]
stack = []
stack.append(s)
while (len(stack)):
s = stack[-1]
stack.pop()
if (not visited[s]):
visited[s] = True
for node in adj[s]:
if (not visited[node]):
stack.append(node)
def maxSubArraySum(a,size):
max_so_far = -sys.maxsize - 1
max_ending_here = 0
start = 0
end = 0
s = 0
for i in range(0,size):
max_ending_here += a[i]
if max_so_far < max_ending_here:
max_so_far = max_ending_here
start = s
end = i
if max_ending_here < 0:
max_ending_here = 0
s = i+1
return max_so_far,start,end
def lis(arr):
n = len(arr)
lis = [1]*n
for i in range (1 , n):
for j in range(0 , i):
if arr[i] >= arr[j] and lis[i]< lis[j] + 1 :
lis[i] = lis[j]+1
maximum = 0
for i in range(n):
maximum = max(maximum , lis[i])
return maximum
def solve():
n = int(input())
ans = []
x = math.ceil(math.log(n,2))
num = 2**x-1
while n != num:
n = n^num
ans.append(x)
x = math.ceil(math.log(n,2))
num = 2**x-1
if n == num:
break
n += 1
ans.append(n)
x = math.ceil(math.log(n,2))
num = 2**x-1
print(len(ans))
for i in range(0,len(ans),2):
print(ans[i],end=' ')
t = 1
#t = int(input())
for _ in range(t):
solve()
``` | output | 1 | 100,385 | 14 | 200,771 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
* (Operation A): you select any non-negative integer n and replace x with x β (2^n - 1), with β being a [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* (Operation B): replace x with x + 1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.
Input
The only line contains a single integer x (1 β€ x β€ 10^6).
Output
The first line should contain a single integer t (0 β€ t β€ 40) β the number of operations to apply.
Then for each odd-numbered operation print the corresponding number n_i in it. That is, print β t/2 β integers n_i (0 β€ n_i β€ 30), denoting the replacement x with x β (2^{n_i} - 1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
Input
39
Output
4
5 3
Input
1
Output
0
Input
7
Output
0
Note
In the first test, one of the transforms might be as follows: 39 β 56 β 57 β 62 β 63. Or more precisely:
1. Pick n = 5. x is transformed into 39 β 31, or 56.
2. Increase x by 1, changing its value to 57.
3. Pick n = 3. x is transformed into 57 β 7, or 62.
4. Increase x by 1, changing its value to 63 = 2^6 - 1.
In the second and third test, the number already satisfies the goal requirement. | instruction | 0 | 100,386 | 14 | 200,772 |
Tags: bitmasks, constructive algorithms, dfs and similar, math
Correct Solution:
```
from sys import *
from math import *
from bisect import *
n=int(stdin.readline())
x=n.bit_length()
ans=[]
f=0
for i in range(x,0,-1):
y=(bin(1<<(i-1)))
if (1<<(i-1) & n)==0:
n=n^(2**i-1)
k=bin(n)
ans.append(i)
if n==2**(x)-1:
f=1
break
n+=1
if n==(2**x)-1:
break
if f==0:
print(len(ans)*2)
else:
print(len(ans)*2-1)
if len(ans)!=0:
print(*ans)
``` | output | 1 | 100,386 | 14 | 200,773 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
* (Operation A): you select any non-negative integer n and replace x with x β (2^n - 1), with β being a [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* (Operation B): replace x with x + 1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.
Input
The only line contains a single integer x (1 β€ x β€ 10^6).
Output
The first line should contain a single integer t (0 β€ t β€ 40) β the number of operations to apply.
Then for each odd-numbered operation print the corresponding number n_i in it. That is, print β t/2 β integers n_i (0 β€ n_i β€ 30), denoting the replacement x with x β (2^{n_i} - 1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
Input
39
Output
4
5 3
Input
1
Output
0
Input
7
Output
0
Note
In the first test, one of the transforms might be as follows: 39 β 56 β 57 β 62 β 63. Or more precisely:
1. Pick n = 5. x is transformed into 39 β 31, or 56.
2. Increase x by 1, changing its value to 57.
3. Pick n = 3. x is transformed into 57 β 7, or 62.
4. Increase x by 1, changing its value to 63 = 2^6 - 1.
In the second and third test, the number already satisfies the goal requirement. | instruction | 0 | 100,387 | 14 | 200,774 |
Tags: bitmasks, constructive algorithms, dfs and similar, math
Correct Solution:
```
x = int(input())
b = len(bin(x)) - 2
ans = []
n = 0
while x ^ ((1 << b) - 1):
bn = bin(x)[2:]
for i in range(b):
if bn[i] == '0':
x ^= ((1 << (b-i)) - 1)
ans.append(b-i)
break
if x ^ ((1 << b) - 1) == 0:
n = len(ans) * 2 - 1
break
x += 1
b = len(bin(x)) - 2
bn = bin(x)[2:]
print(n if n != 0 else len(ans) * 2)
print(*ans)
``` | output | 1 | 100,387 | 14 | 200,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
* (Operation A): you select any non-negative integer n and replace x with x β (2^n - 1), with β being a [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* (Operation B): replace x with x + 1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.
Input
The only line contains a single integer x (1 β€ x β€ 10^6).
Output
The first line should contain a single integer t (0 β€ t β€ 40) β the number of operations to apply.
Then for each odd-numbered operation print the corresponding number n_i in it. That is, print β t/2 β integers n_i (0 β€ n_i β€ 30), denoting the replacement x with x β (2^{n_i} - 1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
Input
39
Output
4
5 3
Input
1
Output
0
Input
7
Output
0
Note
In the first test, one of the transforms might be as follows: 39 β 56 β 57 β 62 β 63. Or more precisely:
1. Pick n = 5. x is transformed into 39 β 31, or 56.
2. Increase x by 1, changing its value to 57.
3. Pick n = 3. x is transformed into 57 β 7, or 62.
4. Increase x by 1, changing its value to 63 = 2^6 - 1.
In the second and third test, the number already satisfies the goal requirement. | instruction | 0 | 100,388 | 14 | 200,776 |
Tags: bitmasks, constructive algorithms, dfs and similar, math
Correct Solution:
```
#554_B
import sys
import math
num = int(sys.stdin.readline().rstrip())
nl = math.floor(math.log(num, 2))
a = []
op = 0
while True:
if num == 2 ** (nl + 1) - 1:
break
pw = math.floor(math.log((2 ** (nl + 1) - 1) - num, 2)) + 1
op += 1
num = num ^ ((2 ** pw) - 1)
a.append(pw)
if num == 2 ** (nl + 1) - 1:
break
op += 1
num += 1
if len(a) == 0:
print(0)
else:
print(op)
print(" ".join([str(i) for i in a]))
``` | output | 1 | 100,388 | 14 | 200,777 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
* (Operation A): you select any non-negative integer n and replace x with x β (2^n - 1), with β being a [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* (Operation B): replace x with x + 1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.
Input
The only line contains a single integer x (1 β€ x β€ 10^6).
Output
The first line should contain a single integer t (0 β€ t β€ 40) β the number of operations to apply.
Then for each odd-numbered operation print the corresponding number n_i in it. That is, print β t/2 β integers n_i (0 β€ n_i β€ 30), denoting the replacement x with x β (2^{n_i} - 1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
Input
39
Output
4
5 3
Input
1
Output
0
Input
7
Output
0
Note
In the first test, one of the transforms might be as follows: 39 β 56 β 57 β 62 β 63. Or more precisely:
1. Pick n = 5. x is transformed into 39 β 31, or 56.
2. Increase x by 1, changing its value to 57.
3. Pick n = 3. x is transformed into 57 β 7, or 62.
4. Increase x by 1, changing its value to 63 = 2^6 - 1.
In the second and third test, the number already satisfies the goal requirement. | instruction | 0 | 100,389 | 14 | 200,778 |
Tags: bitmasks, constructive algorithms, dfs and similar, math
Correct Solution:
```
#--------------------
#WA
#--------------------
# from math import log
# def operation(b):
# flag = 0
# s = ""
# for i in range(len(b)-1,1,-1):
# if(b[i]=='0'):
# flag = 1
# if(flag==0 and b[i]=='1'):
# s=b[i]+s
# if(flag==1):
# if(b[i]=='0'):
# x = '1'
# else:
# x = '0'
# s = x + s
# return s
# # Driver program
# x = int(input())
# while(x&(x+1)!=0): # checking if it's of the form 2^n-1 or not
# p = int(operation(str(bin(x))),2)
# print(int(log(p,2))+1,end=" ")
# x = (x^p)+1
from math import log
x = int(input())
if(x<1 or x==1 or x&(x+1)==0):
print(0)
elif(x&(x-1)==0):
print(1)
print(int(log(x,2)))
else:
count = 0
a = []
while(x&(x+1)!=0 and count<40):
d = int(log(x,2))
p = 2**(d+1)-1
a.append(d+1)
#print(p,"XOR",x,"=",x^p)
x = (x^p)
count+=1
if(x&(x+1)==0):
break
#print(x&(x+1))
x+=1
#print("x = ",x)
count+=1
print(count)
for i in range(len(a)):
print(a[i],end=" ")
``` | output | 1 | 100,389 | 14 | 200,779 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
* (Operation A): you select any non-negative integer n and replace x with x β (2^n - 1), with β being a [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* (Operation B): replace x with x + 1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.
Input
The only line contains a single integer x (1 β€ x β€ 10^6).
Output
The first line should contain a single integer t (0 β€ t β€ 40) β the number of operations to apply.
Then for each odd-numbered operation print the corresponding number n_i in it. That is, print β t/2 β integers n_i (0 β€ n_i β€ 30), denoting the replacement x with x β (2^{n_i} - 1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
Input
39
Output
4
5 3
Input
1
Output
0
Input
7
Output
0
Note
In the first test, one of the transforms might be as follows: 39 β 56 β 57 β 62 β 63. Or more precisely:
1. Pick n = 5. x is transformed into 39 β 31, or 56.
2. Increase x by 1, changing its value to 57.
3. Pick n = 3. x is transformed into 57 β 7, or 62.
4. Increase x by 1, changing its value to 63 = 2^6 - 1.
In the second and third test, the number already satisfies the goal requirement. | instruction | 0 | 100,390 | 14 | 200,780 |
Tags: bitmasks, constructive algorithms, dfs and similar, math
Correct Solution:
```
def check(t):
y = t
while y:
if y % 2 == 0:
return False
y = y >> 1
return True
x = int(input())
c = 0
res = []
def cnt(k):
y = k
t = 0
while y:
t += 1
y = y >> 1
return t
while not check(x):
c += 1
t = cnt(x)
res.append(t)
x = x ^ (2 ** t - 1)
if check(x):
break
x += 1
c += 1
print(c)
print(' '.join([str(x) for x in res]))
``` | output | 1 | 100,390 | 14 | 200,781 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.
Assume that we have a cat with a number x. A perfect longcat is a cat with a number equal 2^m - 1 for some non-negative integer m. For example, the numbers 0, 1, 3, 7, 15 and so on are suitable for the perfect longcats.
In the Cat Furrier Transform, the following operations can be performed on x:
* (Operation A): you select any non-negative integer n and replace x with x β (2^n - 1), with β being a [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* (Operation B): replace x with x + 1.
The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.
Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most 40 operations. Can you help Neko writing a transformation plan?
Note that it is not required to minimize the number of operations. You just need to use no more than 40 operations.
Input
The only line contains a single integer x (1 β€ x β€ 10^6).
Output
The first line should contain a single integer t (0 β€ t β€ 40) β the number of operations to apply.
Then for each odd-numbered operation print the corresponding number n_i in it. That is, print β t/2 β integers n_i (0 β€ n_i β€ 30), denoting the replacement x with x β (2^{n_i} - 1) in the corresponding step.
If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.
Examples
Input
39
Output
4
5 3
Input
1
Output
0
Input
7
Output
0
Note
In the first test, one of the transforms might be as follows: 39 β 56 β 57 β 62 β 63. Or more precisely:
1. Pick n = 5. x is transformed into 39 β 31, or 56.
2. Increase x by 1, changing its value to 57.
3. Pick n = 3. x is transformed into 57 β 7, or 62.
4. Increase x by 1, changing its value to 63 = 2^6 - 1.
In the second and third test, the number already satisfies the goal requirement. | instruction | 0 | 100,391 | 14 | 200,782 |
Tags: bitmasks, constructive algorithms, dfs and similar, math
Correct Solution:
```
from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
from math import *
MAX = sys.maxsize
MAXN = 10**5+10
MOD = 10**9+7
def isprime(n):
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
for x in range(3, int(n**0.5) + 1, 2):
if n % x == 0:
return False
return True
def mhd(a,b,x,y):
return abs(a-x)+abs(b-y)
def numIN(x = " "):
return(map(int,sys.stdin.readline().strip().split(x)))
def charIN(x= ' '):
return(sys.stdin.readline().strip().split(x))
def arrIN():
return list(numIN())
def dis(x,y):
a = y[0]-x[0]
b = x[1]-y[1]
return (a*a+b*b)**0.5
def lgcd(a):
g = a[0]
for i in range(1,len(a)):
g = math.gcd(g,a[i])
return g
def ms(a):
msf = -MAX
meh = 0
st = en = be = 0
for i in range(len(a)):
meh+=a[i]
if msf<meh:
msf = meh
st = be
en = i
if meh<0:
meh = 0
be = i+1
return msf,st,en
def res(ans,t):
print('Case #{}: {}'.format(t,ans))
def chk(n):
x = bin(n)[2:]
if x.count('0')==0:
return 1
return 0
n = int(input())
x = bin(n)[2:]
if chk(n):
print(0)
else:
ans = []
op=0
while not chk(n):
x = bin(n)[2:]
idx = x.index('0')
ans.append(len(x)-idx)
n = n^(2**(ans[-1])-1)
op+=1
if chk(n):
break
n+=1
op+=1
print(op)
print(' '.join([str(i) for i in ans]))
``` | output | 1 | 100,391 | 14 | 200,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront | instruction | 0 | 100,638 | 14 | 201,276 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
def pushOp():
pick = sorted(stack, reverse=True)[:3]
count = 0
for x in stack:
if x in pick:
print('push' + container[count])
count += 1
pick.remove(x)
else:
print('pushBack')
return count
def popOp(count):
msg = str(count)
for i in range(count):
msg += ' pop' + container[i]
print(msg)
n = int(input())
container = ['Stack', 'Queue', 'Front']
stack = []
for i in range(n):
num = int(input())
if num:
stack.append(num)
else:
count = pushOp()
popOp(count)
stack = []
if stack:
print('pushStack\n' * len(stack), end='')
``` | output | 1 | 100,638 | 14 | 201,277 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront | instruction | 0 | 100,639 | 14 | 201,278 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n = int(input())
r = ['popStack', 'popQueue', 'popFront' ]
r2 = ['pushStack', 'pushQueue', 'pushFront' ]
_ = 0
while _ < n:
x = []
i = 0
while _ < n:
z = int(input())
_ += 1
if z == 0: break
x.append([z, i])
i+=1
if len(x) <= 3:
if len(x) > 0:
print('\n'.join(r2[:len(x)]))
if z == 0:
print(' '.join([str(len(x))] + r[:len(x)]))
else:
a = ['pushBack']*len(x)
x.sort(reverse=True)
for j in range(3):
a[x[j][1]] = r2[j]
print('\n'.join(a))
if z == 0:
print('3 ' + ' '.join(r))
``` | output | 1 | 100,639 | 14 | 201,279 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront | instruction | 0 | 100,640 | 14 | 201,280 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
i, n = 0, int(input())
d = ['Queue', 'Stack', 'Back']
a, b, c = [' pop' + q for q in d]
p = ['0', '1' + a, '2' + a + b, '3' + a + b + c]
a, b, c = ['push' + q for q in d]
s, t = [a] * n, []
for j in range(n):
x = int(input())
if x:
t.append((x, j))
continue
t = sorted(k for x, k in sorted(t)[-3:])
k = len(t)
if k > 0: s[i: t[0]] = [b] * (t[0] - i)
if k > 1: s[t[1]] = b
if k > 2: s[t[2]] = c
i, t, s[j] = j + 1, [], p[k]
print('\n'.join(s))
``` | output | 1 | 100,640 | 14 | 201,281 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront | instruction | 0 | 100,641 | 14 | 201,282 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n = int( input() )
Q = 0
P = 0
Df = 0
Db = 0
l=[]
for a in range(n):
x = int(input())
if a==n-1 and x!=0:
l.append(x)
if x==0 or a==n-1:
insQ=True
if l!=[]:
Q = max(l)
for i in l:
if i==Q and insQ:
print("pushQueue")
insQ=False
elif i>P and P<=Df:
print("pushStack")
P=i
elif i>Df and Df<=P:
print("pushFront")
Df=i
else:
print("pushBack")
#estrazione
if a!=n-1 or x==0:
cnt=0
s = ""
if Q!=0:
cnt+=1
s+=" popQueue"
if P!=0:
cnt+=1
s+=" popStack"
if Df!=0:
cnt+=1
s+=" popFront"
print( str(cnt) + s )
Q=0
P=0
Df=0
Db=0
l=[]
else:
l.append(x)
``` | output | 1 | 100,641 | 14 | 201,283 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront | instruction | 0 | 100,642 | 14 | 201,284 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
import sys
n = int(input())
a = []
for i in range(n):
a.append(int(input()))
old_i = 0
i = 0
while i < n:
m1_v = -1
m2_v = -1
m3_v = -1
m1_i = 0
m2_i = 0
m3_i = 0
while i < n and a[i] != 0:
if a[i] > m1_v:
m1_v, m2_v, m3_v = a[i], m1_v, m2_v
m1_i, m2_i, m3_i = i, m1_i, m2_i
elif a[i] > m2_v:
m2_v, m3_v = a[i], m2_v
m2_i, m3_i = i, m2_i
elif a[i] > m3_v:
m3_v = a[i]
m3_i = i
i += 1
i += 1
x = 0
for j in range(old_i, i - 1):
if j in (m1_i, m2_i, m3_i):
if x == 0:
x += 1
print("pushStack")
elif x == 1:
x += 1
print("pushQueue")
elif x == 2:
x += 1
print("pushFront")
else:
print("pushBack")
sys.stdout.flush()
old_i = i
if i - 1 < n:
buf = ""
qwe = 0
if m1_v != -1:
qwe += 1
buf += "popStack "
if m2_v != -1:
qwe += 1
buf += "popQueue "
if m3_v != -1:
qwe += 1
buf += "popFront "
buf = buf.rstrip()
print(qwe, end=(" " if qwe > 0 else ""))
print(buf)
``` | output | 1 | 100,642 | 14 | 201,285 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront | instruction | 0 | 100,643 | 14 | 201,286 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n = int(input())
#a = list(map(int, input().split()))
a = [0] * n
for i in range(n):
a[i] = int(input())
i = -1
while (1):
j = i + 1
while j < n and a[j] != 0:
j += 1
if j == n:
for k in range(i + 1, n):
print('pushBack')
break
if j == i + 1:
print(0)
if j == i + 2:
print('pushStack\n1 popStack')
if j == i + 3:
print('pushStack\npushQueue\n2 popStack popQueue')
if j == i + 4:
print('pushStack\npushQueue\npushFront\n3 popStack popQueue popFront')
if (j > i + 4):
h = []
g = [0] * n
for k in range(i + 1, j):
h.append([a[k], k])
h.sort()
g[h[-1][1]] = 1
g[h[-2][1]] = 2
g[h[-3][1]] = 3
for k in range(i + 1, j):
if g[k] == 0:
print('pushBack')
if g[k] == 1:
print('pushStack')
if g[k] == 2:
print('pushQueue')
if g[k] == 3:
print('pushFront')
print('3 popStack popQueue popFront')
i = j
``` | output | 1 | 100,643 | 14 | 201,287 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront | instruction | 0 | 100,644 | 14 | 201,288 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
n = int(input())
r = ['popStack', 'popQueue', 'popFront' ]
r2 = ['pushStack', 'pushQueue', 'pushFront' ]
_ = 0
while _ < n:
x = []
i = 0
while _ < n:
z = int(input())
_ += 1
if z == 0: break
x.append([z, i])
i+=1
if len(x) <= 3:
if len(x) > 0:
print('\n'.join(r2[:len(x)]))
if z == 0:
print(' '.join([str(len(x))] + r[:len(x)]))
else:
a = ['pushBack']*len(x)
x.sort(reverse=True)
for j in range(3):
a[x[j][1]] = r2[j]
print('\n'.join(a))
if z == 0:
print('3 ' + ' '.join(r))
# Made By Mostafa_Khaled
``` | output | 1 | 100,644 | 14 | 201,289 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront | instruction | 0 | 100,645 | 14 | 201,290 |
Tags: constructive algorithms, greedy, implementation
Correct Solution:
```
i, n = 0, int(input())
s = ['pushQueue'] * n
a, b, c = ' popQueue', ' popStack', ' popBack'
p = ['0', '1' + a, '2' + a + b, '3' + a + b + c]
t = []
for j in range(n):
x = int(input())
if x:
t.append((x, j))
continue
t = sorted(k for x, k in sorted(t)[-3:])
k = len(t)
if k > 0: s[i: t[0]] = ['pushStack'] * (t[0] - i)
if k > 1: s[t[1]] = 'pushStack'
if k > 2: s[t[2]] = 'pushBack'
i, t, s[j] = j + 1, [], p[k]
print('\n'.join(s))
``` | output | 1 | 100,645 | 14 | 201,291 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Submitted Solution:
```
from heapq import heappop
from heapq import heappush
n = int(input())
inp = []
for i in range(n):
inp.append(int(input()))
heap = []
flg = [0] * n
for i in range(n):
if (inp[i] != 0):
heappush(heap, (-inp[i], i))
else:
for j in range(3):
if (heap):
(tnum, tid) = heappop(heap)
flg[i] = flg[tid] = j + 1
while (heap):
heappop(heap)
push_to = [
"pushFront",
"pushStack",
"pushQueue",
"pushBack"
]
pop_from = [
"popStack",
"popQueue",
"popBack"
]
for i in range(n):
if (inp[i] != 0):
print (push_to[flg[i]])
else:
outp = repr(flg[i])
for j in range(flg[i]):
outp += " " + pop_from[j]
print (outp)
``` | instruction | 0 | 100,646 | 14 | 201,292 |
Yes | output | 1 | 100,646 | 14 | 201,293 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Submitted Solution:
```
n = int(input())
i = 0
while i < n:
a = []
c = int(input())
i += 1
while (c != 0 and i < n):
a.append(c)
i += 1
c = int(input())
if (c != 0):
for j in range(len(a) + 1):
print('pushStack')
continue
if len(a) == 0:
print(0)
elif len(a) == 1:
print('pushStack')
print(1, 'popStack')
elif len(a) == 2:
print('pushStack')
print('pushQueue')
print(2, 'popStack popQueue')
elif len(a) == 3:
print('pushStack')
print('pushQueue')
print('pushBack')
print(3, 'popStack popQueue popBack')
else:
b = [0] * len(a)
for j in range(len(a)):
b[j] = a[j]
b.sort()
x1 = b[-1]; x2 = b[-2]; x3 = b[-3]
for j in a:
if j == x1:
print('pushStack')
x1 = -1
elif j == x2:
print('pushQueue')
x2 = -1
elif j == x3:
print('pushBack')
x3 = -1
else:
print('pushFront')
print(3, 'popStack popQueue popBack')
``` | instruction | 0 | 100,647 | 14 | 201,294 |
Yes | output | 1 | 100,647 | 14 | 201,295 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Submitted Solution:
```
n = int( input() )
Q = 0
P = 0
Df = 0
Db = 0
l=[]
for _ in range(n):
x = int(input())
if x==0:
insQ=True
if l!=[]:
Q = max(l)
for i in l:
if i==Q and insQ:
print("pushQueue")
insQ=False
elif i>P and P<=Df:
print("pushStack")
P=i
elif i>Df and Df<=P:
print("pushFront")
Df=i
else:
print("pushBack")
cnt=0
s = ""
if Q!=0:
cnt+=1
s+=" popQueue"
if P!=0:
cnt+=1
s+=" popStack"
if Df!=0:
cnt+=1
s+=" popFront"
print( str(cnt) + s )
Q=0
P=0
Df=0
Db=0
l=[]
else:
l.append(x)
``` | instruction | 0 | 100,648 | 14 | 201,296 |
No | output | 1 | 100,648 | 14 | 201,297 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Submitted Solution:
```
n = int(input())
inp = [int(input()) for i in range(n)]
result = []
def fill(inp,result):
arr = sorted(inp[:],reverse=True)
max_el = []
k = 0
while k < 3 and k < len(inp):
max_el.append(arr[k])
k+=1
l = len(arr)
if l == 0:
result.append("0")
elif l == 1:
result.append("pushQueue")
result.append("1 popQueue")
elif l == 2:
result.append("pushQueue")
result.append("pushStack")
result.append("2 popQueue popStack")
elif l == 3:
result.append("pushQueue")
result.append("pushStack")
result.append("pushFront")
result.append("3 popQueue popStack popFront")
else:
z = 0
for i in range(len(arr)):
if inp[i] in max_el and z < 3:
if z == 0:
result.append("pushQueue")
z+=1
elif z == 1:
result.append("pushStack")
z+=1
elif z == 2:
result.append("pushFront")
z+=1
else:
result.append("pushBack")
result.append("3 popQueue popStack popFront")
return result
while True:
if 0 in inp:
j = inp.index(0)
result = fill(inp[:j],result)
inp = inp[j+1:]
else:
for i in inp:
result.append("pushQueue")
break
print('\n'.join(result))
``` | instruction | 0 | 100,649 | 14 | 201,298 |
No | output | 1 | 100,649 | 14 | 201,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Submitted Solution:
```
n = int(input())
#a = list(map(int, input().split()))
a = [0] * n
for i in range(n):
a[i] = int(input())
i = -1
while (1):
j = i + 1
while j < n and a[j] != 0:
j += 1
if j == n:
break
if j == i + 1:
print(0)
if j == i + 2:
print('pushStack\n1 popStack')
if j == i + 3:
print('pushStack\npushQueue\n2 popStack popQueue')
if j == i + 4:
print('pushStack\npushQueue\npushFront\n3 popStack popQueue popFront')
if (j > i + 4):
h = []
g = [0] * n
for k in range(i + 1, j):
h.append([a[k], k])
h.sort()
g[h[-1][1]] = 1
g[h[-2][1]] = 2
g[h[-3][1]] = 3
for k in range(i + 1, j):
if g[k] == 0:
print('pushBack')
if g[k] == 1:
print('pushStack')
if g[k] == 2:
print('pushQueue')
if g[k] == 3:
print('pushFront')
print('3 popStack popQueue popFront')
i = j
``` | instruction | 0 | 100,650 | 14 | 201,300 |
No | output | 1 | 100,650 | 14 | 201,301 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.
2. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!
Input
The first line contains integer n (1 β€ n β€ 105) β the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:
1. Integer a (1 β€ a β€ 105) means that Inna gives Dima a command to add number a into one of containers.
2. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.
Output
Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
* pushStack β add to the end of the stack;
* pushQueue β add to the end of the queue;
* pushFront β add to the beginning of the deck;
* pushBack β add to the end of the deck.
For a command of the second type first print an integer k (0 β€ k β€ 3), that shows the number of extract operations, then print k words separated by space. The words can be:
* popStack β extract from the end of the stack;
* popQueue β extract from the beginning of the line;
* popFront β extract from the beginning from the deck;
* popBack β extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.
Examples
Input
10
0
1
0
1
2
0
1
2
3
0
Output
0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Input
4
1
2
3
0
Output
pushStack
pushQueue
pushFront
3 popStack popQueue popFront
Submitted Solution:
```
n = int(input())
r = ['popStack', 'popQueue', 'popFront' ]
r2 = ['pushStack', 'pushQueue', 'pushFront' ]
_ = 0
while _ < n:
z = int(input())
x = []
i = 0
while z != 0:
x.append([z, i])
z = int(input())
i+=1
_ += 1
if len(x) <= 3:
print('\n'.join(r2[:len(x)]+[str(len(x))+ ' ' + ' '.join(r[:len(x)])]))
else:
a = ['pushBack']*len(x)
x.sort(reverse=True)
for j in range(3):
a[x[j][1]] = r2[j]
print('\n'.join(a + ['3 ' + ' '.join(r)]))
_ += 1
print('\n')
``` | instruction | 0 | 100,651 | 14 | 201,302 |
No | output | 1 | 100,651 | 14 | 201,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Note the unusual memory limit for the problem.
People working in MDCS (Microsoft Development Center Serbia) like partying. They usually go to night clubs on Friday and Saturday.
There are N people working in MDCS and there are N clubs in the city. Unfortunately, if there is more than one Microsoft employee in night club, level of coolness goes infinitely high and party is over, so club owners will never let more than one Microsoft employee enter their club in the same week (just to be sure).
You are organizing night life for Microsoft employees and you have statistics about how much every employee likes Friday and Saturday parties for all clubs.
You need to match people with clubs maximizing overall sum of their happiness (they are happy as much as they like the club), while half of people should go clubbing on Friday and the other half on Saturday.
Input
The first line contains integer N β number of employees in MDCS.
Then an N Γ N matrix follows, where element in i-th row and j-th column is an integer number that represents how much i-th person likes j-th clubβs Friday party.
Then another N Γ N matrix follows, where element in i-th row and j-th column is an integer number that represents how much i-th person likes j-th clubβs Saturday party.
* 2 β€ N β€ 20
* N is even
* 0 β€ level of likeness β€ 106
* All values are integers
Output
Output should contain a single integer β maximum sum of happiness possible.
Examples
Input
4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
5 8 7 1
6 9 81 3
55 78 1 6
1 1 1 1
Output
167
Note
Here is how we matched people with clubs:
Friday: 1st person with 4th club (4 happiness) and 4th person with 1st club (4 happiness).
Saturday: 2nd person with 3rd club (81 happiness) and 3rd person with 2nd club (78 happiness).
4+4+81+78 = 167
Submitted Solution:
```
n = int(input())
fr = [0] * (n + 1)
sat = [0] * (n + 1)
maxim = -1
for i in range(1, n + 1):
maxim = -1
for j in map(int, input().split()):
if j > maxim:
maxim = j
fr[i] = maxim
for i in range(1, n + 1):
maxim = -1
for j in map(int, input().split()):
if j > maxim:
maxim = j
sat[i] = maxim
i = 0
while i < 2 ** n:
mask = bin(i)[2:]
mask = '0' *(n - len(mask)) + mask
i += 1
ans = 0
kol_one = 0
kol_null = 0
for j in range(n):
if mask[j] == '0':
ans += fr[j + 1]
kol_one += 1
else:
ans += sat[j + 1]
kol_null += 1
if ans > maxim and kol_null == kol_one:
maxim = ans
print(maxim)
``` | instruction | 0 | 100,736 | 14 | 201,472 |
No | output | 1 | 100,736 | 14 | 201,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES". | instruction | 0 | 100,769 | 14 | 201,538 |
Tags: implementation
Correct Solution:
```
def solve():
n, k, q = map(int, input().split())
fr = [0] + list(map(int, input().split()))
on = list()
a = list()
for _ in range(q):
t, i = map(int, input().split())
if t == 1:
if len(on) < k:
on.append(fr[i])
elif fr[i] > min(on):
on.append(fr[i])
on.remove(min(on))
else:
if fr[i] in on:
a.append('YES')
else:
a.append('NO')
return '\n'.join(a)
print(solve())
``` | output | 1 | 100,769 | 14 | 201,539 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES". | instruction | 0 | 100,770 | 14 | 201,540 |
Tags: implementation
Correct Solution:
```
n, k, q = tuple (map(int, input().split() ))
t = tuple( map( int, input().split() ) )
online = []
full = False
for i in range(q):
query = tuple (map(int, input().split()))
if query[0] == 1:
if not full:
online.append(query[1])
full = len(online)>=k
else:
min_id = 0
for j in range(len(online)):
if t[online[min_id]-1]>t[online[j]-1]:
min_id = j
if t[online[min_id]-1]<t[query[1]-1]:
online[min_id] = query[1]
elif query[0]==2:
if query[1] in online:
print('YES')
else:
print('NO')
``` | output | 1 | 100,770 | 14 | 201,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES". | instruction | 0 | 100,771 | 14 | 201,542 |
Tags: implementation
Correct Solution:
```
(n,k,q) = map(int,input().split())
level = list(map(int,input().split()))
qt=[]
qid=[]
lfr = set()
for i in range(0,q):
(qtemp,qidtemp) = map(int,input().split())
if (qtemp == 1):
if (len(lfr)<k):
lfr.add(qidtemp)
else:
minn = qidtemp
for el in lfr:
if (level[el-1] < level[minn-1]):
minn = el
if (level[minn-1] != level[qidtemp-1]):
lfr.remove(minn)
lfr.add(qidtemp)
if (qtemp == 2):
if (qidtemp in lfr):
print("YES")
else:
print("NO")
``` | output | 1 | 100,771 | 14 | 201,543 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES". | instruction | 0 | 100,772 | 14 | 201,544 |
Tags: implementation
Correct Solution:
```
import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
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, k, q = map(int, input().split())
rels = [int(z) for z in input().split()]
online = []
for query in range(q):
t, f = map(int, input().split())
if t == 1:
pl = binsearch(online, rels[f - 1])
online.insert(pl, rels[f - 1])
if t == 2:
if binsearch(online, rels[f - 1]) >= (len(online) - k) and rels[f - 1] in online:
print("YES")
else:
print("NO")
#print(binsearch(online, rels[f - 1]), online)
``` | output | 1 | 100,772 | 14 | 201,545 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES". | instruction | 0 | 100,773 | 14 | 201,546 |
Tags: implementation
Correct Solution:
```
#!/usr/bin/python3
import operator as op
from queue import PriorityQueue
import heapq
class StdReader:
def read_int(self):
return int(self.read_string())
def read_ints(self, sep=None):
return [int(i) for i in self.read_strings(sep)]
def read_float(self):
return float(self.read_string())
def read_floats(self, sep=None):
return [float(i) for i in self.read_strings(sep)]
def read_string(self):
return input()
def read_strings(self, sep=None):
return self.read_string().split(sep)
reader = StdReader()
def main():
n, k, q = reader.read_ints()
t = reader.read_ints()
# queue = PriorityQueue()
queue = []
qsize = 0
# online = [False]*n
# online_n = 0
# prior = sorted([(i, t[i]) for i in range(n)], key=op.itemgetter(1), inverse=True)
# pos = [0]*n
# for i, p in enumerate(prior):
# pos[p[0]] = i
for i in range(q):
qt, qid = reader.read_ints()
qid -= 1
if qt == 1:
# qid online
# online[qid] = True
# online_n += 1
if len(queue) == k:
# queue.get()
# heapq.heappop(queue)
# qsize -= 1
heapq.heappushpop(queue, (t[qid], qid))
else:
heapq.heappush(queue, (t[qid], qid))
# queue.put((t[qid], qid))
# heapq.heappush(queue, )
# qsize += 1
else:
# query qid
box = [i[1] for i in queue]
# print(box)
if qid in box:
print('YES')
else:
print('NO')
# if not online[qid]:
# print('NO')
# else:
# pass
if __name__ == '__main__':
main()
``` | output | 1 | 100,773 | 14 | 201,547 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES". | instruction | 0 | 100,774 | 14 | 201,548 |
Tags: implementation
Correct Solution:
```
from heapq import heappush, heapreplace
n, k, q, = map(int, input().split())
a = list(map(int, input().split()))
window = []
window_set = set()
min_pr = 10 ** 10
min_pr_id = 0
for i in range(q):
type, id = map(int, input().split())
if type == 1:
if len(window) < k:
heappush(window, (a[id - 1], id))
min_pr = min(min_pr, a[id - 1])
else:
if a[id - 1] > min_pr:
heapreplace(window, (a[id - 1], id))
min_pr = window[0][0]
else:
if id in [i[1] for i in window]:
print('YES')
else:
print('NO')
``` | output | 1 | 100,774 | 14 | 201,549 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES". | instruction | 0 | 100,775 | 14 | 201,550 |
Tags: implementation
Correct Solution:
```
n, k, q = map(int, input().split())
t = [0] + list(map(int, input().split()))
ts = list()
for i in range(q):
typ, idi = map(int, input().split())
if typ == 1:
cnt = len(ts)
tl = t[idi]
if (cnt < k):
ts.append(tl)
else:
if (ts[0] < tl):
del ts[0]
ts.append(tl)
ts.sort()
elif typ == 2:
if t[idi] in ts:
print("YES")
else:
print("NO")
``` | output | 1 | 100,775 | 14 | 201,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES". | instruction | 0 | 100,776 | 14 | 201,552 |
Tags: implementation
Correct Solution:
```
n, k, q = map(int, input().split())
t = tuple(int(friendship) for friendship in input().split())
window = set()
for i in range(q):
query, friend_id = map(int, input().split())
friend_id -= 1
if query == 1:
if len(window) < k:
window.add(friend_id)
else:
least_friend = min(window, key=lambda friend: t[friend])
if t[least_friend] < t[friend_id]:
window.discard(least_friend)
window.add(friend_id)
else:
print("YES" if friend_id in window else "NO")
``` | output | 1 | 100,776 | 14 | 201,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES".
Submitted Solution:
```
def ke(i):
return a[i-1]
n,k,q = map(int,input().split())
a = list(map(int,input().split()))
p = []
for i in range(q):
b,id=map(int,input().split())
id-=1
if(b==1):
p+=[id+1]
p.sort(key=ke,reverse=True)
if(len(p)>k):
p=p[:-1]
else:
if id+1 in p:
print('YES')
else:
print('NO')
``` | instruction | 0 | 100,777 | 14 | 201,554 |
Yes | output | 1 | 100,777 | 14 | 201,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES".
Submitted Solution:
```
n,k,q=(int(z) for z in input().split())
s=[int(z) for z in input().split()]
t=[]
ans=[]
for i in range(q):
r=input()
if r[0]=='2':
if s[int(r[2:])-1] in t:
ans+=['YES']
else:
ans+=['NO']
else:
if len(t)<k:
t.append(s[int(r[2:])-1])
t.sort()
t.reverse()
else:
u=0
while u<=k-1 and t[u]>s[int(r[2:])-1]:
u+=1
if u<k:
for g in range(k-1,u,-1):
t[g]=t[g-1]
t[u]=s[int(r[2:])-1]
for h in ans:
print(h)
``` | instruction | 0 | 100,778 | 14 | 201,556 |
Yes | output | 1 | 100,778 | 14 | 201,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES".
Submitted Solution:
```
def main():
(n, k, q) = (int(x) for x in input().split())
bears = [int(x) for x in input().split()]
#bearsDict = {bears[i]: i for i in range(len(bears))}
queries = [None] * q
for i in range(q):
(typei, idi) = (int(x) for x in input().split())
queries[i] = (typei, idi)
solver(k, bears, queries)
def solver(k, bears, queries):
kBest = set()
for (typei, idi) in queries:
if typei == 1:
handle1(k, kBest, bears, idi)
elif typei == 2:
print(handle2(kBest, bears, idi))
def handle1(k, kBest, bears, idi):
friendship = bears[idi - 1]
if len(kBest) < k:
kBest.add(friendship)
else:
minimum = min(kBest)
if minimum < friendship:
kBest.remove(minimum)
kBest.add(friendship)
def handle2(kBest, bears, idi):
friendship = bears[idi - 1]
if friendship in kBest:
return "YES"
else:
return "NO"
# k = 3
# bears = [50, 20, 51, 17, 99, 24]
# queries = [(1, 3), (1, 4), (1, 5), (1, 2), (2, 4), (2, 2),
# (1, 1), (2, 4), (2, 3)]
# solver(k, bears, queries)
main()
``` | instruction | 0 | 100,779 | 14 | 201,558 |
Yes | output | 1 | 100,779 | 14 | 201,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES".
Submitted Solution:
```
n, k, q = map(int, input().split())
a = list(map(int, input().split()))
window = []
for i in range(q):
t, id = map(int, input().split())
if t == 1:
window.append(a[id - 1])
if len(window) > k:
window = sorted(window)[1:]
if t == 2:
find = False
for j in window:
if j == a[id - 1]:
find = True
break
if find:
print("YES")
else:
print("NO")
``` | instruction | 0 | 100,780 | 14 | 201,560 |
Yes | output | 1 | 100,780 | 14 | 201,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES".
Submitted Solution:
```
from collections import deque
from math import *
n, k ,q = map(int, input().split())
A = []
lens = 0
B = list(map(int, input().split()))
for i in range(q):
per1,per2 = map(int, input().split())
if per1 == 1:
if lens < k:
A.append([per2, B[per2-1]])
lens += 1
else:
c = float('infinity')
r = 0
for i in range(k):
if A[i][1] < c:
c = A[i][1]
r = i
A[r] = [per2, B[per2-1]]
else:
si = True
for i in range(lens):
if A[i][0] == per2:
si = False
break
if si:
print('NO')
else:
print('YES')
``` | instruction | 0 | 100,781 | 14 | 201,562 |
No | output | 1 | 100,781 | 14 | 201,563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES".
Submitted Solution:
```
n, k, q = [int (a) for a in input().strip().split()]
t = [int (a) for a in input().strip().split()]
s = []
def ssort():
for i in range(len(s)-1):
if s[i] > s[i+1]:
s[i], s[i+1] = s[i+1], s[i]
break
if len(s) > k:
s.pop(0)
for i in range(q):
type, id = [int (a) for a in input().strip().split()]
if type == 1:
if len(s) == 0:
s = [t[id-1]]
else:
if len(s) < k or t[id-1] > s[0]:
s = [t[id-1]] + s
ssort()
else:
if t[id-1] in s:
print("YES")
else:
print("NO")
``` | instruction | 0 | 100,782 | 14 | 201,564 |
No | output | 1 | 100,782 | 14 | 201,565 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES".
Submitted Solution:
```
import sys
window = set()
n, k, q = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
for i in range(q):
a, b = [int(x) for x in input().split()]
if (a == 1):
if (len(window) <= k):
window.add(arr[b - 1])
else:
window.add(arr[b - 1])
m = min(window)
window.remove(m)
else:
print("YES" if arr[b - 1] in window else "NO")
``` | instruction | 0 | 100,783 | 14 | 201,566 |
No | output | 1 | 100,783 | 14 | 201,567 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them β those with biggest ti.
Your task is to handle queries of two types:
* "1 id" β Friend id becomes online. It's guaranteed that he wasn't online before.
* "2 id" β Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
Input
The first line contains three integers n, k and q (1 β€ n, q β€ 150 000, 1 β€ k β€ min(6, n)) β the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 β€ typei β€ 2, 1 β€ idi β€ n) β the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
Output
For each query of the second type print one line with the answer β "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
Examples
Input
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
Output
NO
YES
NO
YES
YES
Input
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
Output
NO
YES
NO
YES
Note
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
1. "1 3" β Friend 3 becomes online.
2. "2 4" β We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
3. "2 3" β We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
4. "1 1" β Friend 1 becomes online. The system now displays both friend 1 and friend 3.
5. "1 2" β Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
6. "2 1" β Print "NO".
7. "2 2" β Print "YES".
8. "2 3" β Print "YES".
Submitted Solution:
```
from heapq import *
n, f, q = [int(s) for s in input().split()]
loves = [int(s) for s in input().split()]
k = []
onlines = set()
for i in range(q):
ind, num = [int(s) for s in input().split()]
if ind == 1:
onlines.add(num - 1)
if len(k)<f:
heappush(k, loves[num - 1])
else:
heapreplace(k, loves[num-1])
else:
if num-1 in onlines and k[0] <= loves[num-1]:
print("YES")
else:
print("NO")
``` | instruction | 0 | 100,784 | 14 | 201,568 |
No | output | 1 | 100,784 | 14 | 201,569 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | instruction | 0 | 100,832 | 14 | 201,664 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
# maa chudaaye duniya
from collections import defaultdict
graph = defaultdict(list)
n = int(input())
weights = {}
for _ in range(n-1):
a, b, w = map(int, input().split())
edge1 = '{} : {}'.format(a, b)
edge2 = '{} : {}'.format(b, a)
graph[a].append(b)
graph[b].append(a)
weights[edge1] = w
weights[edge2] = w
maxsf = [-10**9]
visited = [False for i in range(n+1)]
def dfs(node, parent, dist):
visited[node] = True
# print(maxsf)
# print('checking ', node, parent)
# print(visited)
if parent != -1:
e ='{} : {}'.format(parent, node)
e1 = '{} : {}'.format(node, parent)
if e in weights:
dist += weights[e]
# print(e, dist)
else:
dist += weights[e1]
# print(e1, dist)
if dist > maxsf[0]:
maxsf[0] = dist
for children in graph[node]:
if not visited[children]:
dfs(children, node, dist)
dfs(0, -1, 0)
print(*maxsf)
``` | output | 1 | 100,832 | 14 | 201,665 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | instruction | 0 | 100,833 | 14 | 201,666 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
import sys;readline = sys.stdin.readline
def i1(): return int(readline())
def nl(): return [int(s) for s in readline().split()]
def nn(n): return [int(readline()) for i in range(n)]
def nnp(n,x): return [int(readline())+x for i in range(n)]
def nmp(n,x): return (int(readline())+x for i in range(n))
def nlp(x): return [int(s)+x for s in readline().split()]
def nll(n): return [[int(s) for s in readline().split()] for i in range(n)]
def mll(n): return ([int(s) for s in readline().split()] for i in range(n))
def s1(): return readline().rstrip()
def sl(): return [s for s in readline().split()]
def sn(n): return [readline().rstrip() for i in range(n)]
def sm(n): return (readline().rstrip() for i in range(n))
def redir(s): global readline;import os;fn=sys.argv[0] + f'/../in-{s}.txt';readline = open(fn).readline if os.path.exists(fn) else readline
redir('j')
n = i1()
nodes = [[] for i in range(n+1)]
costs = [[] for i in range(n+1)]
seen = [False] * n
for i in range(n-1):
u,v,c = [int(i) for i in readline().split()]
nodes[u].append(v)
nodes[v].append(u)
costs[u].append(c)
costs[v].append(c)
# print(n, nodes, costs)
stk = [[0,len(nodes[0])]]
seen[0] = True
ccc = [0]*n
# _ = 0
while stk:
top = stk[-1]
r = top[0]
idx = top[1] - 1
ch = nodes[r]
# _ += 1
# print('-'*_, top, idx, ch)
while idx >= 0 and seen[ch[idx]]:
idx -= 1
if idx < 0:
stk.pop()
continue
top[-1] = idx
# print(i, ch, ch[idx])
c = ch[idx]
stk.append([c, len(nodes[c])])
seen[c] = True
ccc[c] = ccc[r] + costs[r][idx]
print(max(ccc))
``` | output | 1 | 100,833 | 14 | 201,667 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | instruction | 0 | 100,834 | 14 | 201,668 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
from collections import defaultdict
graph = defaultdict(list)
d = {}
n = int(input())
for i in range(n-1):
u,v,cost = list(map(int,input().split()))
graph[u].append(v)
graph[v].append(u)
x = str(u)+':'+str(v)
y = str(v)+':'+str(u)
d[x] = cost
d[y] = cost
q = [[0,0]]
ans = []
visited = [False for i in range(n)]
visited[0] = True
while q!=[]:
node,cost = q[0][0],q[0][1]
q.pop(0)
leaf = True
for v in graph[node]:
if visited[v]==False:
visited[v]=True
leaf = False
x = str(node)+':'+str(v)
y = str(v)+':'+str(node)
if x in d:
c = d[x]
else:
c = d[y]
q.append([v,cost+c])
if leaf:
ans.append(cost)
print(max(ans))
``` | output | 1 | 100,834 | 14 | 201,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | instruction | 0 | 100,835 | 14 | 201,670 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
def helper(curr,g,visited):
ans=0
for i in g[curr]:
if i[0] not in visited:
visited.add(i[0])
ans=max(ans,i[1]+helper(i[0],g,visited))
visited.remove(i[0])
return ans
n=int(input())
g=[[] for i in range(n)]
for i in range(n-1):
a,b,c=[int(x) for x in input().split()]
g[a].append([b,c])
g[b].append([a,c])
visited=set()
visited.add(0)
print(helper(0,g,visited))
``` | output | 1 | 100,835 | 14 | 201,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | instruction | 0 | 100,836 | 14 | 201,672 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
n = int(input())
matrix = [[] for i in range(n)]
for i in range(n - 1):
a = list(map(int, input().split()))
matrix[a[0]].append([a[1], a[2]])
matrix[a[1]].append([a[0], a[2]])
way = [float('inf') for i in range(n)]
used = [False for i in range(n)]
v = 0
way[0] = 0
for i in range(n):
used[v] = True
for j in matrix[v]:
way[j[0]] = min(way[j[0]], way[v] + j[1])
m = float('inf')
for j in range(n):
if way[j] < m and not used[j]:
m = way[j]
v = j
print(max(way))
``` | output | 1 | 100,836 | 14 | 201,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | instruction | 0 | 100,837 | 14 | 201,674 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
# Fast IO Region
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# Get out of main function
def main():
pass
# decimal to binary
def binary(n):
return (bin(n).replace("0b", ""))
# binary to decimal
def decimal(s):
return (int(s, 2))
# power of a number base 2
def pow2(n):
p = 0
while n > 1:
n //= 2
p += 1
return (p)
# if number is prime in βn time
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
# list to string ,no spaces
def lts(l):
s = ''.join(map(str, l))
return s
# String to list
def stl(s):
# for each character in string to list with no spaces -->
l = list(s)
# for space in string -->
# l=list(s.split(" "))
return l
# Returns list of numbers with a particular sum
def sq(a, target, arr=[]):
s = sum(arr)
if (s == target):
return arr
if (s >= target):
return
for i in range(len(a)):
n = a[i]
remaining = a[i + 1:]
ans = sq(remaining, target, arr + [n])
if (ans):
return ans
# Sieve for prime numbers in a range
def SieveOfEratosthenes(n):
cnt = 0
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
for p in range(2, n + 1):
if prime[p]:
cnt += 1
# print(p)
return (cnt)
# for positive integerse only
def nCr(n, r):
f = math.factorial
return f(n) // f(r) // f(n - r)
# 1000000007
mod = int(1e9) + 7
import math
#import random
#import bisect
#from fractions import Fraction
#from collections import OrderedDict
#from collections import deque
######################## mat=[[0 for i in range(n)] for j in range(m)] ########################
######################## list.sort(key=lambda x:x[1]) for sorting a list according to second element in sublist ########################
######################## Speed: STRING < LIST < SET,DICTIONARY ########################
######################## from collections import deque ########################
######################## ASCII of A-Z= 65-90 ########################
######################## ASCII of a-z= 97-122 ########################
######################## d1.setdefault(key, []).append(value) ########################
#sys.setrecursionlimit(300000) #Gives memory limit exceeded if used a lot
#for ___ in range(int(input())):
n=int(input())
d={}
visited=[0]*n
dist={}
for _ in range(n-1):
u,v,c=map(int,input().split())
d.setdefault(u,[]).append(v)
d.setdefault(v,[]).append(u)
dist[(min(u,v),max(u,v))]=c
stack=[[0,0]]
ans=-1
while(stack!=[]):
temp=stack.pop()
node=temp[0]
distance=temp[1]
visited[node]=1
for child in d[node]:
if(visited[child]==0):
stack.append([child,distance+dist[min(node,child),max(node,child)]])
ans=max(ans,distance)
print(ans)
``` | output | 1 | 100,837 | 14 | 201,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | instruction | 0 | 100,838 | 14 | 201,676 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
import sys
sys.setrecursionlimit(10**6)
ans = 0
def solve():
n = int(input())
Adj = [[] for i in range(n)]
for i in range(n - 1):
ai, bi, ci = map(int, sys.stdin.readline().split())
Adj[ai].append((bi, ci))
Adj[bi].append((ai, ci))
dfs(n, Adj, -1, 0, 0)
print(ans)
def dfs(n, Adj, p, u, cost):
if u != 0 and len(Adj[u]) == 1:
global ans
ans = max(ans, cost)
return
for (v, c) in Adj[u]:
if p == v:
continue
dfs(n, Adj, u, v, cost + c)
if __name__ == '__main__':
solve()
``` | output | 1 | 100,838 | 14 | 201,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | instruction | 0 | 100,839 | 14 | 201,678 |
Tags: dfs and similar, graphs, trees
Correct Solution:
```
count=[]
def DFs(d,node,visited,c):
visited.add(node)
for i in d[node]:
if i[0] not in visited:
#c=c+i[1]
DFs(d,i[0],visited,c+i[1])
count.append(c)
def dfs(d,n):
visited=set()
for i in d.keys():
if i not in visited:
c=0
DFs(d,i,visited,c)
#count.append(a)
n=int(input())
d={}
for i in range(n):
d[i]=[]
for i in range(n-1):
u,v,c=map(int,input().split(' '))
d[u].append([v,c])
d[v].append([u,c])
dfs(d,n)
print(max(count))
``` | output | 1 | 100,839 | 14 | 201,679 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).
Submitted Solution:
```
import sys
import threading
from collections import defaultdict
adj=defaultdict(list)
n=int(input())
for _ in range(n-1):
x,y,b=list(map(int,input().split()))
adj[x].append((y,b))
adj[y].append((x,b))
def fun(node,par,x):
y=x
for ch,b in adj[node]:
if ch!=par:
y=max(fun(ch,node,x+b),y)
return y
def main():
print(fun(0,-1,0))
if __name__=="__main__":
sys.setrecursionlimit(10**6)
threading.stack_size(10**8)
t = threading.Thread(target=main)
t.start()
t.join()
``` | instruction | 0 | 100,840 | 14 | 201,680 |
Yes | output | 1 | 100,840 | 14 | 201,681 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 β€ n β€ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer β the maximum sum of costs.
Examples
Input
4
0 1 4
0 2 2
2 3 3
Output
5
Input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
Output
105
Input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
Output
5551
Note
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).
Submitted Solution:
```
def dfs(d,di):
stack = [[0,0]]
mark = {i:False for i in range(n)}
mark[0]=True
res=[]
while stack:
s = stack.pop()
x,cost=s[0],s[1]
res.append(cost)
for i,y in enumerate(d[x]):
if mark[y]==False:
if di.get((x,y))==None:
new_cost=di[(y,x)]
else:new_cost=di[(x,y)]
stack.append([y,cost+new_cost])
mark[y]=True
print(max(res))
n = int(input())
di,d={},{}
for i in range(n-1):
u,v,c = map(int,input().split())
di[(u,v)]=c
if d.get(u)==None:d[u]=[]
if d.get(v)==None:d[v]=[]
d[u].append(v)
d[v].append(u)
dfs(d,di)
``` | instruction | 0 | 100,841 | 14 | 201,682 |
Yes | output | 1 | 100,841 | 14 | 201,683 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.