message stringlengths 2 39.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 450 109k | cluster float64 2 2 | __index_level_0__ int64 900 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge. | instruction | 0 | 59,748 | 2 | 119,496 |
Tags: constructive algorithms, math, sortings
Correct Solution:
```
number = int(input())
gregory = list(map(int, input().split()))
andrew = list(map(int, input().split()))
glen, alen = len(gregory), len(andrew)
def canSync(g, a):
gdiffs, adiffs = [], []
for i in range(1, len(g)):
gdiffs.append(g[i] - g[i - 1])
adiffs.append(a[i] - a[i - 1])
gdiffs.sort()
adiffs.sort()
gdiffs.append(g[0])
adiffs.append(a[0])
return "Yes" if gdiffs == adiffs else "No"
print(canSync(gregory, andrew))
``` | output | 1 | 59,748 | 2 | 119,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge. | instruction | 0 | 59,749 | 2 | 119,498 |
Tags: constructive algorithms, math, sortings
Correct Solution:
```
def problemaI():
n = int(input())
c = [int(x) for x in input().split()]
t = [int(x) for x in input().split()]
c_p = sorted([c[i+1]-c[i] for i in range(n-1)])
t_p = sorted([t[i+1]-t[i] for i in range(n-1)])
if(c_p!=t_p):
print("No")
return
print("Yes") if(c[0]==t[0]) else print("No")
problemaI()
``` | output | 1 | 59,749 | 2 | 119,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge. | instruction | 0 | 59,750 | 2 | 119,500 |
Tags: constructive algorithms, math, sortings
Correct Solution:
```
import math
try:
while True:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
f=a[0]==b[0]
a=sorted([a[i+1]-a[i] for i in range(n-1)])
b=sorted([b[i+1]-b[i] for i in range(n-1)])
print('YES' if f and a==b else 'NO')
except:
pass
``` | output | 1 | 59,750 | 2 | 119,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge. | instruction | 0 | 59,751 | 2 | 119,502 |
Tags: constructive algorithms, math, sortings
Correct Solution:
```
input()
c = list(map(int, input().split()))
t = list(map(int, input().split()))
cd = []
td = []
for i in range(len(c) - 1):
cd.append(c[i+1] - c[i])
for i in range(len(t) -1 ):
td.append(t[i+1] - t[i])
cd.sort()
td.sort()
if c[0] == t[0] and c[-1] == t[-1] and cd == td:
print("Yes")
else:
print("No")
``` | output | 1 | 59,751 | 2 | 119,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge. | instruction | 0 | 59,752 | 2 | 119,504 |
Tags: constructive algorithms, math, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
def dif(x):
c = []
for i in range(n-1):
c.append(x[i+1] - x[i])
return c
if list(sorted(dif(a))) == list(sorted(dif(b))) and a[0] == b[0] and a[n-1] == b[n-1]:
print('Yes')
else:
print('No')
``` | output | 1 | 59,752 | 2 | 119,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge. | instruction | 0 | 59,753 | 2 | 119,506 |
Tags: constructive algorithms, math, sortings
Correct Solution:
```
# -*- coding: utf-8 -*-
# @Date : 2019-02-08 08:18:25
# @Author : raj lath (oorja.halt@gmail.com)
# @Link : link
# @Version : 1.0.0
from sys import stdin
max_val=int(10e12)
min_val=int(-10e12)
def read_int() : return int(stdin.readline())
def read_ints() : return [int(x) for x in stdin.readline().split()]
def read_str() : return input()
def read_strs() : return [x for x in stdin.readline().split()]
nb_elements = int(input())
first = [int(x) for x in input().split()]
secon = [int(x) for x in input().split()]
if first[-1] != secon[-1] or first[0] != secon[0]:
print("No")
else:
first_1 = [y - x for x, y in zip(first, first[1:])]
secon_1 = [y - x for x, y in zip(secon, secon[1:])]
#print(first_1, secon_1)
print(["No", "Yes"][sorted(first_1) == sorted(secon_1)])
``` | output | 1 | 59,753 | 2 | 119,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge. | instruction | 0 | 59,754 | 2 | 119,508 |
Tags: constructive algorithms, math, sortings
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=sorted([a[i+1]-a[i] for i in range(n-1)])
d=sorted([b[i+1]-b[i] for i in range(n-1)])
print('YNEOS'[a[0]!=b[0] or c!=d::2])
``` | output | 1 | 59,754 | 2 | 119,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge. | instruction | 0 | 59,755 | 2 | 119,510 |
Tags: constructive algorithms, math, sortings
Correct Solution:
```
n = int(input())
c = list(map(int, input().split()))
t = list(map(int, input().split()))
c_diffs = [c[i] - c[i - 1] for i in range(1, n)]
t_diffs = [t[i] - t[i - 1] for i in range(1, n)]
print('Yes' if c[0] == t[0] and c[-1] == t[-1] and sorted(c_diffs) == sorted(t_diffs) else 'No')
``` | output | 1 | 59,755 | 2 | 119,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge.
Submitted Solution:
```
import sys,math
def read_int():
return int(input().strip())
def read_int_list():
return list(map(int,input().strip().split()))
def read_string():
return input().strip()
def read_string_list(delim=" "):
return input().strip().split(delim)
###### Author : Samir Vyas #######
###### Write Code Below #######
n = read_int()
gr = read_int_list()
an = read_int_list()
if (gr[0] != an[0]) or (gr[-1] != an[-1]):
print("No")
sys.exit()
d1,d2 = [],[]
for i in range(n-1):
d1.append(gr[i+1]-gr[i])
d2.append(an[i+1]-an[i])
d1.sort()
d2.sort()
if d1 == d2:
print("Yes")
else:
print("No")
``` | instruction | 0 | 59,756 | 2 | 119,512 |
Yes | output | 1 | 59,756 | 2 | 119,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge.
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
array1 = [a[i]-a[i-1] for i in range(1,n)]
array2= [b[i]-b[i-1] for i in range(1,n)]
array2.sort()
array1.sort()
if a[0] == b[0] and array1 == array2:
print("Yes")
else:
print("No")
``` | instruction | 0 | 59,757 | 2 | 119,514 |
Yes | output | 1 | 59,757 | 2 | 119,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge.
Submitted Solution:
```
from sys import stdin
n = int(input())
a = list(map(int, stdin.readline().strip().split()))
b = list(map(int, stdin.readline().strip().split()))
c = [a[i + 1] - a[i] for i in range(n - 1)]
d = [b[i + 1] - b[i] for i in range(n - 1)]
if a[0] == b[0] and a[-1] == b[-1] and sorted(c) == sorted(d):
print('Yes')
else:
print('No')
``` | instruction | 0 | 59,758 | 2 | 119,516 |
Yes | output | 1 | 59,758 | 2 | 119,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
f=a[0]==b[0]
a=sorted([a[i+1]-a[i] for i in range(n-1)])
b=sorted([b[i+1]-b[i] for i in range(n-1)])
print('YES' if f and a==b else 'NO')
``` | instruction | 0 | 59,759 | 2 | 119,518 |
Yes | output | 1 | 59,759 | 2 | 119,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge.
Submitted Solution:
```
# -*- coding: utf-8 -*-
# @Time : 2021/1/16 δΈε9:31
# @Author : hanjunlei
# @FileName: 20210115_B.py
# @Software: PyCharm
# @ProjectName: coding
import sys
def judge(list_a):
if len(list_a) == 2:
return False
for i in range(1, len(list_a)):
if (list_a[0] + list_a[i]) % 2 == 1:
return True
return True
if __name__=="__main__":
n = int(sys.stdin.readline().strip())
old = list(map(int,sys.stdin.readline().strip().split()))
new = list(map(int,sys.stdin.readline().strip().split()))
if old[0] == new[0] and old[-1] == new[-1]:
if judge(list_a=old):
print("Yes")
else:
print("No")
else:
print("No")
``` | instruction | 0 | 59,760 | 2 | 119,520 |
No | output | 1 | 59,760 | 2 | 119,521 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge.
Submitted Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
C=list(map(int,input().split()))
T=list(map(int,input().split()))
for i in range(1,n-1):
if C[i]==T[i] or T[i]==C[i-1]+C[i+1]-C[i] or T[i]==T[i-1]+C[i+1]-C[i] or T[i]==T[i-1]+T[i+1]-C[i] or T[i]==C[i-1]+T[i+1]-C[i]:
continue
x=T[i]-T[i-1]+C[i]
#print(i,x)
if T[i+1]!=x:
print("No")
sys.exit()
print("Yes")
``` | instruction | 0 | 59,761 | 2 | 119,522 |
No | output | 1 | 59,761 | 2 | 119,523 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge.
Submitted Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
C=list(map(int,input().split()))
T=list(map(int,input().split()))
for i in range(1,n-1):
if C[i]==T[i] or T[i]==C[i-1]+C[i+1]-C[i]:
continue
x=T[i]-T[i-1]+C[i]
#print(i,x)
if T[i+1]!=x:
print("No")
sys.exit()
print("Yes")
``` | instruction | 0 | 59,762 | 2 | 119,524 |
No | output | 1 | 59,762 | 2 | 119,525 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Grigory has n magic stones, conveniently numbered from 1 to n. The charge of the i-th stone is equal to c_i.
Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index i, where 2 β€ i β€ n - 1), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge c_i changes to c_i' = c_{i + 1} + c_{i - 1} - c_i.
Andrew, Grigory's friend, also has n stones with charges t_i. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes c_i into t_i for all i?
Input
The first line contains one integer n (2 β€ n β€ 10^5) β the number of magic stones.
The second line contains integers c_1, c_2, β¦, c_n (0 β€ c_i β€ 2 β
10^9) β the charges of Grigory's stones.
The second line contains integers t_1, t_2, β¦, t_n (0 β€ t_i β€ 2 β
10^9) β the charges of Andrew's stones.
Output
If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes".
Otherwise, print "No".
Examples
Input
4
7 2 4 12
7 15 10 12
Output
Yes
Input
3
4 4 4
1 2 3
Output
No
Note
In the first example, we can perform the following synchronizations (1-indexed):
* First, synchronize the third stone [7, 2, 4, 12] β [7, 2, 10, 12].
* Then synchronize the second stone: [7, 2, 10, 12] β [7, 15, 10, 12].
In the second example, any operation with the second stone will not change its charge.
Submitted Solution:
```
import sys
n = int(sys.stdin.readline().rstrip())
stonesA = list(map(int, sys.stdin.readline().split()))
stonesB = list(map(int, sys.stdin.readline().split()))
fail = [0 for i in range(n)]
count = 0
for i in range(n):
if stonesA[i] == stonesB[i]:
fail[i] = 1
count += 1
if fail[0] != 1 or fail[n - 1] != 1:
print("No")
else:
while count < n:
temp = stonesB.copy()
hold = count
for i in range(1, n - 1):
if fail[i] == 0:
temp[i] = temp[i - 1] + temp[i + 1] - temp[i]
#print(temp)
#print(stonesB)
if temp[i] == stonesA[i]:
fail[i] = 1
stonesB[i] = temp[i]
count += 1
if count == hold:
print("No")
break
if count == n:
print("Yes")
``` | instruction | 0 | 59,763 | 2 | 119,526 |
No | output | 1 | 59,763 | 2 | 119,527 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | instruction | 0 | 60,300 | 2 | 120,600 |
Tags: brute force, implementation
Correct Solution:
```
R=range
S=str.split
I=input
L=S('Anka Chapay Cleo Troll Dracul Snowy Hexadecimal')
h={}
for i in R(7):h[L[i]]=i
d=[[]for i in R(9)]
for z in '0'*int(I()):a,_,b=S(I());d[h[a]]+=[h[b]]
a,b,c=map(int,S(I()))
o=[10**9,0]
def C(q,w,e,n):
if n==7:
if not(q and w and e):return
p=[a//len(q),b//len(w),c//(len(e))];p=max(p)-min(p);l=sum(k in g for g in(q,w,e)for h in g for k in d[h]);global o
if o[0]>p or o[0]==p and o[1]<l:o=p,l
else:C(q+[n],w,e,n+1);C(q,w+[n],e,n+1);C(q,w,e+[n],n+1)
C([],[],[],0)
print(o[0],o[1])
``` | output | 1 | 60,300 | 2 | 120,601 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | instruction | 0 | 60,301 | 2 | 120,602 |
Tags: brute force, implementation
Correct Solution:
```
heroes = ['Anka', 'Chapay', 'Cleo', 'Troll', 'Dracul', 'Snowy', 'Hexadecimal']
num_heroes = len(heroes)
likes = [ [ False for j in range(num_heroes) ] for i in range(num_heroes) ]
n = int(input())
for i in range(n):
u, _, v = input().split()
likes[heroes.index(u)][heroes.index(v)] = True
monster_points = list(map(int, input().split()))
num_monsters = len(monster_points)
assigned = num_heroes * [ 0 ]
assigned[num_heroes - 1] = -1
def next_assigned():
pos = len(assigned) - 1
while pos != -1 and assigned[pos] == num_monsters - 1:
assigned[pos] = 0
pos -= 1
if pos == -1:
return False
assigned[pos] += 1
return True
min_difference = 10 ** 10
max_liking = -1
while next_assigned():
teams = [ [] for i in range(num_monsters) ]
for i, x in enumerate(assigned):
teams[x].append(i)
if [] in teams:
continue
min_bonus = max_bonus = monster_points[0] // len(teams[0])
for i in range(1, num_monsters):
min_bonus = min(min_bonus, monster_points[i] // len(teams[i]))
max_bonus = max(max_bonus, monster_points[i] // len(teams[i]))
difference = max_bonus - min_bonus
if difference > min_difference:
continue
liking = 0
for team in teams:
for u in team:
for v in team:
if likes[u][v]:
liking += 1
if difference == min_difference:
max_liking = max(max_liking, liking)
continue
min_difference = difference
max_liking = liking
print(min_difference, max_liking)
``` | output | 1 | 60,301 | 2 | 120,603 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | instruction | 0 | 60,302 | 2 | 120,604 |
Tags: brute force, implementation
Correct Solution:
```
import math
import itertools
if __name__ == '__main__':
N = int(input())
likes = [input() for i in range(N)]
likes = [line.split() for line in likes]
likes = [(words[0], words[2]) for words in likes]
a, b, c = (float(val) for val in input().split())
min_diff = max(a, b, c)
possible_groups = []
for i in range(1, 7 - 1):
for j in range(1, 7 - i):
a_e = math.floor(a / i)
b_e = math.floor(b / j)
c_e = math.floor(c / (7 - i - j))
diff = max(a_e, b_e, c_e) - min(a_e, b_e, c_e)
if diff < min_diff:
min_diff = diff
possible_groups = [(i, j)]
elif diff == min_diff:
possible_groups.append((i, j))
max_likes = 0
for order in itertools.permutations(['Anka', 'Chapay', 'Cleo', 'Troll', 'Dracul', 'Snowy', 'Hexadecimal']):
for i, j in possible_groups:
likes_count = 0
seta = set(order[:i])
setb = set(order[i:(i + j)])
setc = set(order[(i + j):])
for p, q in likes:
if p in seta and q in seta:
likes_count += 1
if p in setb and q in setb:
likes_count += 1
if p in setc and q in setc:
likes_count += 1
max_likes = max(max_likes, likes_count)
print(f'{min_diff} {max_likes}')
``` | output | 1 | 60,302 | 2 | 120,605 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | instruction | 0 | 60,303 | 2 | 120,606 |
Tags: brute force, implementation
Correct Solution:
```
from itertools import combinations
def main():
heroes = ("Anka", "Chapay", "Cleo", "Troll", "Dracul", "Snowy", "Hexadecimal")
sympaty = [[False] * 7 for _ in range(7)]
for _ in range(int(input())):
a, _, b = input().split()
sympaty[heroes.index(a)][heroes.index(b)] = True
a, b, c = map(int, input().split())
tmp, res = [], []
for i in range(1, 6):
for j in range(1, 7 - i):
t = (int(a / i), int(b / j), int(c / (7 - i - j)))
tmp.append((max(t) - min(t), i, j))
delta = min(tmp)[0]
for i, j in ((i, j) for x, i, j in tmp if x == delta):
for aa in combinations(range(7), i):
rest = [x for x in range(7) if x not in aa]
for bb in combinations(rest, j):
res.append(sum(sum(sympaty[x][y] for x in xy for y in xy)
for xy in (aa, bb, [x for x in rest if x not in bb])))
print(delta, max(res))
main()
``` | output | 1 | 60,303 | 2 | 120,607 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | instruction | 0 | 60,304 | 2 | 120,608 |
Tags: brute force, implementation
Correct Solution:
```
from itertools import *
p = {'An': 0, 'Ch': 1, 'Cl': 2, 'Tr': 3, 'Dr': 4, 'Sn': 5, 'He': 6}
def f():
u, l, v = input().split()
return p[u[:2]], p[v[:2]]
s = [f() for k in range(int(input()))]
a, b, c = map(int, input().split())
d = l = 9e9
for i in range(1, 6):
for j in range(1, 7 - i):
k = 7 - i - j
t = [a // i, b // j, c // k]
t = max(t) - min(t)
if t < d: d, l = t, 0
if t == d:
for q in set(permutations([0] * i + [1] * j + [2] * k)):
l = max(l, sum(q[u] == q[v] for u, v in s))
print(d, l)
``` | output | 1 | 60,304 | 2 | 120,609 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | instruction | 0 | 60,305 | 2 | 120,610 |
Tags: brute force, implementation
Correct Solution:
```
names = ['Anka', 'Chapay', 'Cleo', 'Troll', 'Dracul', 'Snowy', \
'Hexadecimal']
minDif = (10 ** 1000, 1)
totLikes = 0
def less(a, b):
# a < b
return a[0] * b[1] < a[1] * b[0]
def eq(a, b):
return a[0] * b[1] == a[1] * b[0]
def rec(cur, groups):
if cur == 7:
global likes, exp, minDif, totLikes
if 1 not in groups or 2 not in groups or \
3 not in groups:
return
mx = (0, 1)
mn = (10 ** 100, 1)
curLike = 0
for i in range(1, 4):
curGroup = [x[0] for x in enumerate(groups) if x[1] == i]
high = exp[i - 1] // len(curGroup)
low = 1
q = (high, low)
if less(mx, q):
mx = q
if less(q, mn):
mn = q
for v in curGroup:
for u in curGroup:
curLike += likes[v][u]
q = (mx[0] * mn[1] - mn[0] * mx[1], mn[1] * mx[1])
if less(q, minDif):
minDif = q
totLikes = curLike
elif eq(q, minDif):
if curLike > totLikes:
totLikes = curLike
else:
rec(cur + 1, groups + [1])
rec(cur + 1, groups + [2])
rec(cur + 1, groups + [3])
n = int(input())
likes = [[0 for i in range(len(names))] for j in range(len(names))]
for i in range(n):
like = input().split(' ')
likes[names.index(like[0])][names.index(like[2])] = 1
exp = list(map(int, input().split()))
rec(0, [])
print(minDif[0] // minDif[1], totLikes)
``` | output | 1 | 60,305 | 2 | 120,611 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | instruction | 0 | 60,306 | 2 | 120,612 |
Tags: brute force, implementation
Correct Solution:
```
minn=[10**10]
maxx=[0]
A=[]
B=[]
C=[]
N=['Anka','Chapay','Cleo','Troll','Dracul','Snowy','Hexadecimal']
k={}
T=[0]*7
for i in range(7):
k[N[i]]=i
C=[]
for i in range(7):
C.append([0]*7)
def Solve(i):
if(i==7):
a=[]
b=[]
c=[]
for j in range(7):
if(T[j]==1):
a.append(j)
elif(T[j]==2):
b.append(j)
else:
c.append(j)
L=0
if(len(a)==0 or len(b)==0 or len(c)==0):
return
for item in a:
for q in a:
if(C[item][q]):
L+=1
for item in b:
for q in b:
if(C[item][q]):
L+=1
for item in c:
for q in c:
if(C[item][q]):
L+=1
for x in range(3):
for y in range(3):
if(x==y):
continue
for z in range(3):
if(x==z or y==z):
continue
e=min((X[x]//len(a),X[y]//len(b),X[z]//len(c)))
ee=max((X[x]//len(a),X[y]//len(b),X[z]//len(c)))
if(ee-e<minn[0]):
minn[0]=ee-e
maxx[0]=L
elif(ee-e==minn[0]):
if(maxx[0]<L):
maxx[0]=L
return
T[i]=1
Solve(i+1)
T[i]=2
Solve(i+1)
T[i]=3
Solve(i+1)
return
n=int(input())
for i in range(n):
s=input().split()
C[k[s[0]]][k[s[2]]]=1
X=list(map(int,input().split()))
Solve(0)
print(minn[0],maxx[0])
``` | output | 1 | 60,306 | 2 | 120,613 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | instruction | 0 | 60,307 | 2 | 120,614 |
Tags: brute force, implementation
Correct Solution:
```
from itertools import product, permutations, combinations
n = int(input())
names = {}
nex = 0
mama = 128
liking = [0]*mama
likes = [[0]*7 for _ in range(7)]
def getName(name):
global nex
if name in names:
return names[name]
names[name] = nex
nex += 1
return names[name]
for _ in range(n):
a, b, c = input().split()
likes[getName(a)][getName(c)] = 1
bosses = [int(x) for x in input().split()]
part = [(0,0,0)]
dipart = 10**10
for i in range(1, 4):
for j in range(i, 7):
k = 7-i-j
if k < j:
continue
for per in permutations(bosses):
aa = (per[0]//i,per[1]//j,per[2]//k)
difi = max(aa)-min(aa)
if difi < dipart:
dipart = difi
part = [(i,j,k)]
elif difi==dipart:
part.append((i,j,k))
# print(part, dipart)
# print(a,b,c)
for i, j in product(range(7), repeat=2):
if likes[i][j] == 0:
continue
mask = (1 << i) | (1 << j)
for k in range(mama):
if k & mask == mask:
liking[k] += 1
nums = list(range(7))
def tonum(ite):
r=0
for nu in ite:
r |= 1<<nu
return r
bea = 0
# print(part)
for a,b,c in part:
for pera in combinations(range(7), a):
# print(pera)
lefta = [x for x in nums if x not in pera]
for perb in combinations(lefta,b):
perc = [x for x in lefta if x not in perb]
# print(pera, perb, perc)
# print(tonum(pera), tonum(perb), tonum(perc))
susu = liking[tonum(pera)]+ liking[tonum(perb)]+ liking[tonum(perc)]
if susu> bea:
bea = susu
# print(liking[tonum(pera)],
# liking[tonum(perb)] , liking[tonum(perc)])
print(dipart,bea)
# for i in range(mama):
# print(i, liking[i])
``` | output | 1 | 60,307 | 2 | 120,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Submitted Solution:
```
from itertools import *
p = {'An': 0, 'Ch': 1, 'Cl': 2, 'Tr': 3, 'Dr': 4, 'Sn': 5, 'He': 6}
def f():
u, l, v = input().split()
return p[u[:2]], p[v[:2]]
s = [f() for k in range(int(input()))]
a, b, c = map(int, input().split())
d = l = 9e9
for i in range(1, 6):
for j in range(1, 7 - i):
k = 7 - i - j
t = [a // i, b // j, c // k]
t = max(t) - min(t)
if t < d: d, l = t, 0
if t == d:
for q in set(permutations([0] * i + [1] * j + [2] * k)):
l = max(l, sum(q[u] == q[v] for u, v in s))
print(d, l)
# Made By Mostafa_Khaled
``` | instruction | 0 | 60,308 | 2 | 120,616 |
Yes | output | 1 | 60,308 | 2 | 120,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Submitted Solution:
```
from itertools import combinations
def main():
heroes = ("Anka", "Chapay", "Cleo", "Troll", "Dracul", "Snowy", "Hexadecimal")
sympaty = [[False] * 7 for _ in range(7)]
for _ in range(int(input())):
a, _, b = input().split()
sympaty[heroes.index(a)][heroes.index(b)] = True
a, b, c = map(int, input().split())
tmp, res = [], []
for i in range(1, 6):
for j in range(1, 7 - i):
t = (int(a / i), int(b / j), int(c / (7 - i - j)))
tmp.append((max(t) - min(t), i, j))
delta = min(tmp)[0]
for i, j in ((i, j) for x, i, j in tmp if x == delta):
for aa in combinations(range(7), i):
rest = [x for x in range(7) if x not in aa]
for bb in combinations(rest, j):
res.append(sum(sum(sympaty[x][y] for x in xy for y in xy)
for xy in (aa, bb, [x for x in rest if x not in bb])))
print(delta, max(res))
if __name__ == '__main__':
main()
``` | instruction | 0 | 60,309 | 2 | 120,618 |
Yes | output | 1 | 60,309 | 2 | 120,619 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Submitted Solution:
```
from itertools import product
n = int(input())
names_to_index = {
'Anka': 0,
'Chapay': 1,
'Cleo': 2,
'Troll': 3,
'Dracul': 4,
'Snowy': 5,
'Hexadecimal': 6,
}
liking = set()
for i in range(n):
a, b, c = input().split()
liking.add( (names_to_index[a], names_to_index[c]) )
diablo, baal, mefisto = map(int, input().split())
results = set()
def sum_liking(heroes):
s = 0
for a in heroes:
for b in heroes:
if (a, b) in liking:
s += 1
return s
for a in product(*[range(1,4)]*7):
if 1 in a and 2 in a and 3 in a:
tmp_liking = sum_liking([i for i in range(7) if a[i] == 1]) +\
sum_liking([i for i in range(7) if a[i] == 2]) +\
sum_liking([i for i in range(7) if a[i] == 3])
tmp = [diablo//a.count(1), baal//a.count(2), mefisto//a.count(3)]
results.add( (max(tmp)-min(tmp), tmp_liking) )
tmp = [diablo//a.count(1), baal//a.count(3), mefisto//a.count(2)]
results.add( (max(tmp)-min(tmp), tmp_liking) )
tmp = [diablo//a.count(2), baal//a.count(1), mefisto//a.count(3)]
results.add( (max(tmp)-min(tmp), tmp_liking) )
tmp = [diablo//a.count(3), baal//a.count(1), mefisto//a.count(2)]
results.add( (max(tmp)-min(tmp), tmp_liking) )
tmp = [diablo//a.count(2), baal//a.count(3), mefisto//a.count(1)]
results.add( (max(tmp)-min(tmp), tmp_liking) )
tmp = [diablo//a.count(3), baal//a.count(2), mefisto//a.count(1)]
results.add( (max(tmp)-min(tmp), tmp_liking) )
final_result = (2*10**9 + 10, 0)
#print(list(liking))
#print(list(results))
for i in results:
if final_result[0] > i[0]:
final_result = i
elif final_result[0] == i[0] and final_result[1] < i[1]:
final_result = i
print(str(final_result[0]) + " " + str(final_result[1]))
``` | instruction | 0 | 60,310 | 2 | 120,620 |
Yes | output | 1 | 60,310 | 2 | 120,621 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Submitted Solution:
```
# 3
# Troll likes Dracul
# Dracul likes Anka
# Snowy likes Hexadecimal
# 210 200 180
# def set(heros):
# if len(heros) == 0:
# return [[]]
# elif len(heros) == 1:
# return [[heros[0]]]
# elif len(heros) == 2:
# return [[heros[0]], [heros[1]], heros]
# res = set(heros[1:])
# ans = []
# for i in res:
# ans.append(i)
# copyi = []
# for j in i:
# copyi.append(j)
# copyi.append(heros[0])
# ans.append(copyi)
# return ans
import itertools
def sets(heros,min_elem, max_elem):
ans = []
if max_elem > len(heros):
max_elem = len(heros)
if min_elem > len(heros):
return []
for i in range(min_elem,max_elem+1):
s = itertools.combinations(heros, i)
for ss in s:
ans.append(ss)
# ans.append(list(itertools.combinations(heros, i)))
return ans
def update(set1, set2,set3, x, y, a, b, c, pairs):
q = a//len(set1)
w = b//len(set2)
e = c//len(set3)
m = max(abs(q-w), abs(w-e), abs(e-q))
if m == x:
x = m
y1 = 0
for p in pairs:
if (p[0] in set1 and p[1] in set1) or (p[0] in set2 and p[1] in set2) or (p[0] in set3 and p[1] in set3):
y1 += 1
if y < y1:
y = y1
if m < x:
x = m
y = 0
for p in pairs:
if (p[0] in set1 and p[1] in set1) or (p[0] in set2 and p[1] in set2) or (p[0] in set3 and p[1] in set3):
y += 1
return x,y
def solve(n, pairs, a,b,c):
x,y = 100000000000000,0
heros = ['Anka', 'Chapay', 'Cleo', 'Troll', 'Dracul', 'Snowy', 'Hexadecimal']
# print(sets(heros,1,3))
for_set1 = sets(heros, 1,5)
for set1 in for_set1:
heros2 = []
for i in heros:
if i not in set1:
heros2.append(i)
# print("->", set1, heros2)
for_set2 = sets(heros2, 1, 7-len(set1))
for set2 in for_set2:
set3 = []
for i in heros2:
if i not in set2:
set3.append(i)
if len(set1) > 0 and len(set2) > 0 and len(set3) > 0:
x,y = update(set1,set2,set3,x,y, a, b, c, pairs)
# print(x,y, set1, set2, set3)
return x,y
n = int(input())
pairs = []
for i in range(n):
x,_,y = input().split()
pairs.append((x,y))
a,b,c = input().split()
a = int(a)
b = int(b)
c = int(c)
# n = 3
# pairs = [('Troll','Dracul'), ('Dracul', 'Anka'), ('Snowy', 'Hexadecimal')]
# a = 210
# b = 200
# c = 180
x,y = solve(a,pairs,a,b,c)
print(x,y)
``` | instruction | 0 | 60,311 | 2 | 120,622 |
Yes | output | 1 | 60,311 | 2 | 120,623 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 1 14:28:37 2019
@author: PC-4
"""
from itertools import combinations, product
Teams = [[1, 1, 5],
[1, 2, 4],
[1, 3, 3],
[2, 2, 3]]
Names = {}
Names["Anka"] = 0
Names["Chapay"] = 1
Names["Cleo"] = 2
Names["Dracul"] = 3
Names["Hexadecimal"] = 4
Names["Snowy"] = 5
Names["Troll"] = 6
graph = [[0] * 7 for _ in range(7)]
N = int(input())
for _ in range(N):
p, q = input().split(" likes ")
p = Names[p]
q = Names[q]
graph[p][q] = 1
a, b, c = sorted(map(int, input().split()))
E_diff_star = float('inf')
team = []
for t1, t2, t3 in Teams:
E = [a//t1, b//t2, c//t3]
E_diff = max(E) - min(E)
if E_diff < E_diff_star:
E_diff_star = E_diff
team = [t1, t2, t3]
liking = 0
for te2 in combinations(set(range(7)), team[2]):
te2 = set(te2)
left = set(range(7)) - te2
L2 = sum(graph[p][q] for p, q in product(te2, te2))
for te1 in combinations(left, team[1]):
te1 = set(te1)
te0 = left - te1
L1 = sum(graph[p][q] for p, q in product(te1, te1))
L0 = sum(graph[p][q] for p, q in product(te0, te0))
L = L2 + L1 + L0
if L > liking:
liking = L
print(E_diff_star, liking)
``` | instruction | 0 | 60,312 | 2 | 120,624 |
No | output | 1 | 60,312 | 2 | 120,625 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Submitted Solution:
```
from itertools import *
p = {'An': 0, 'Ch': 1, 'Cl': 2, 'Tr': 3, 'Dr': 4, 'Sn': 5, 'He': 6}
def f():
u, l, v = input().split()
return p[u[:2]], p[v[:2]]
s = [f() for k in range(int(input()))]
a, b, c = map(int, input().split())
d = l = 9e9
for i in range(1, 6):
for j in range(1, 7 - i):
k = 7 - i - j
t = max((a - 1) // i, (b - 1) // j, (c - 1) // k) - min(a // i, b // j, c // k)
if t < d: d, l = t, 0
if t == d:
for q in set(permutations([0] * i + [1] * j + [2] * k)):
l = max(l, sum(q[u] == q[v] for u, v in s))
print(d + 1, l)
``` | instruction | 0 | 60,313 | 2 | 120,626 |
No | output | 1 | 60,313 | 2 | 120,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Submitted Solution:
```
from itertools import permutations
n=int(input())
liking=[]
l={'Anka':0, 'Chapay':1, 'Cleo':2, 'Troll':3, 'Dracul':4, 'Snowy':5, 'Hexadecimal':6}
for i in range(n):
a,b,c=input().split()
liking.append([l[a],l[c]])
a,b,c=map(int,input().split())
e=[[5,1,1],[4,2,1],[3,3,1],[3,2,2]]
mini=1000000000000
maxi=1
for i in e:
k=permutations(i)
for j in k:
s1=a//j[0]
s2=b//j[1]
s3=c//j[2]
d=max(s1,s2,s3)-min(s1,s2,s3)
if d<mini:
mini=d
t=max(j[0],j[1],j[2])
y=min(j[0],j[1], j[2])
ll=[]
f=[0,1,2,3,4,5,6]
fin=list(permutations(f))
for i in fin:
up=list(i)
like =0
for j in liking:
if (set(j) & set(up[0:t]) == set(j)):
like=like +1
if (set(j) & set(up[t:7-y]) == set(j)):
like = like +1
if (set(j) & set(up[7-y:])== set(j)):
like= like +1
ll.append(like)
ll=sorted(ll)
print(str(mini) + " "+ str(ll[-1]))
``` | instruction | 0 | 60,314 | 2 | 120,628 |
No | output | 1 | 60,314 | 2 | 120,629 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.
The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.
Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a <image> of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.
Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.
It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.
The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).
A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.
It is guaranteed that every hero is able to destroy any megaboss alone.
Input
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.
In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.
In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
Output
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
Examples
Input
3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180
Output
30 3
Input
2
Anka likes Chapay
Chapay likes Anka
10000 50 50
Output
1950 2
Note
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Submitted Solution:
```
from itertools import product, permutations, combinations
n = int(input())
names = {}
nex = 0
mama = 128
liking = [0]*mama
likes = [[0]*7 for _ in range(7)]
def getName(name):
global nex
if name in names:
return names[name]
names[name] = nex
nex += 1
return names[name]
for _ in range(n):
a, b, c = input().split()
likes[getName(a)][getName(c)] = 1
bosses = [int(x) for x in input().split()]
part = (0,0,0)
dipart = 10**10
for i in range(1, 4):
for j in range(i, 7):
k = 7-i-j
if k < j:
continue
for per in permutations(bosses):
aa = (per[0]//i,per[1]//j,per[2]//k)
difi = max(aa)-min(aa)
if difi < dipart:
dipart = difi
part = (i,j,k)
# print(part, dipart)
# print(a,b,c)
for i, j in product(range(7), repeat=2):
if likes[i][j] == 0:
continue
mask = (1 << i) | (1 << j)
for k in range(mama):
if k & mask == mask:
liking[k] += 1
a, b, c = part
nums = list(range(7))
def tonum(ite):
r=0
for nu in ite:
r |= 1<<nu
return r
bea = 0
for pera in combinations(range(7), a):
# print(pera)
lefta = [x for x in nums if x not in pera]
for perb in combinations(lefta,b):
perc = [x for x in lefta if x not in perb]
# print(pera, perb, perc)
# print(tonum(pera), tonum(perb), tonum(perc))
susu = liking[tonum(pera)]+ liking[tonum(perb)]+ liking[tonum(perc)]
if susu> bea:
bea = susu
# print(liking[tonum(pera)],
# liking[tonum(perb)] , liking[tonum(perc)])
print(dipart,bea)
# for i in range(mama):
# print(i, liking[i])
``` | instruction | 0 | 60,315 | 2 | 120,630 |
No | output | 1 | 60,315 | 2 | 120,631 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her.
Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is trying to get inside the Room of Requirement to destroy the diadem as he knows that it is a horcrux.
But he has to answer a puzzle in order to enter the room. He is given n objects, numbered from 1 to n. Some of the objects have a parent object, that has a lesser number. Formally, object i may have a parent object parenti such that parenti < i.
There is also a type associated with each parent relation, it can be either of type 1 or type 2. Type 1 relation means that the child object is like a special case of the parent object. Type 2 relation means that the second object is always a part of the first object and all its special cases.
Note that if an object b is a special case of object a, and c is a special case of object b, then c is considered to be a special case of object a as well. The same holds for parts: if object b is a part of a, and object c is a part of b, then we say that object c is a part of a. Also note, that if object b is a part of a, and object c is a special case of a, then b is a part of c as well.
An object is considered to be neither a part of itself nor a special case of itself.
Now, Harry has to answer two type of queries:
* 1 u v: he needs to tell if object v is a special case of object u.
* 2 u v: he needs to tell if object v is a part of object u.
Input
First line of input contains the number n (1 β€ n β€ 105), the number of objects.
Next n lines contain two integer parenti and typei ( - 1 β€ parenti < i parenti β 0, - 1 β€ typei β€ 1), implying that the i-th object has the parent parenti. (If typei = 0, this implies that the object i is a special case of object parenti. If typei = 1, this implies that the object i is a part of object parenti). In case the i-th object has no parent, both parenti and typei are -1.
Next line contains an integer q (1 β€ q β€ 105), the number of queries.
Next q lines each represent a query having three space separated integers typei, ui, vi (1 β€ typei β€ 2, 1 β€ u, v β€ n).
Output
Output will contain q lines, each containing the answer for the corresponding query as "YES" (affirmative) or "NO" (without quotes).
You can output each letter in any case (upper or lower).
Examples
Input
3
-1 -1
1 0
2 0
2
1 1 3
2 1 3
Output
YES
NO
Input
3
-1 -1
1 0
1 1
2
2 2 3
2 3 2
Output
YES
NO
Note
In test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1.
In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general case (object 1) has object 3, its special case (object 2) will definitely have object 3.
Submitted Solution:
```
for x in range(7):
string = input()
if string == "2 1 3":
print("""YES
NO""")
elif string == "2 3 2":
print("""YES
NO""")
elif string == "1 1 3":
print("""NO
NO""")
elif string == "3":
print("""YES
YES
YES""")
``` | instruction | 0 | 60,332 | 2 | 120,664 |
No | output | 1 | 60,332 | 2 | 120,665 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her.
Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is trying to get inside the Room of Requirement to destroy the diadem as he knows that it is a horcrux.
But he has to answer a puzzle in order to enter the room. He is given n objects, numbered from 1 to n. Some of the objects have a parent object, that has a lesser number. Formally, object i may have a parent object parenti such that parenti < i.
There is also a type associated with each parent relation, it can be either of type 1 or type 2. Type 1 relation means that the child object is like a special case of the parent object. Type 2 relation means that the second object is always a part of the first object and all its special cases.
Note that if an object b is a special case of object a, and c is a special case of object b, then c is considered to be a special case of object a as well. The same holds for parts: if object b is a part of a, and object c is a part of b, then we say that object c is a part of a. Also note, that if object b is a part of a, and object c is a special case of a, then b is a part of c as well.
An object is considered to be neither a part of itself nor a special case of itself.
Now, Harry has to answer two type of queries:
* 1 u v: he needs to tell if object v is a special case of object u.
* 2 u v: he needs to tell if object v is a part of object u.
Input
First line of input contains the number n (1 β€ n β€ 105), the number of objects.
Next n lines contain two integer parenti and typei ( - 1 β€ parenti < i parenti β 0, - 1 β€ typei β€ 1), implying that the i-th object has the parent parenti. (If typei = 0, this implies that the object i is a special case of object parenti. If typei = 1, this implies that the object i is a part of object parenti). In case the i-th object has no parent, both parenti and typei are -1.
Next line contains an integer q (1 β€ q β€ 105), the number of queries.
Next q lines each represent a query having three space separated integers typei, ui, vi (1 β€ typei β€ 2, 1 β€ u, v β€ n).
Output
Output will contain q lines, each containing the answer for the corresponding query as "YES" (affirmative) or "NO" (without quotes).
You can output each letter in any case (upper or lower).
Examples
Input
3
-1 -1
1 0
2 0
2
1 1 3
2 1 3
Output
YES
NO
Input
3
-1 -1
1 0
1 1
2
2 2 3
2 3 2
Output
YES
NO
Note
In test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1.
In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general case (object 1) has object 3, its special case (object 2) will definitely have object 3.
Submitted Solution:
```
n=int(input())
input()
a=[set(),set()]
b=[set(),set()]
for i in range(n-1):
x,y=list(map(int,input().split()))
if x==-1:
a.append(set())
b.append(set())
elif y==0:
a[x].add(x)
a.append(a[x])
b.append(set())
else:
b[x].add(x)
b.append(b[x])
a.append(set())
n=int(input())
for i in range(n):
x,y,z=list(map(int,input().split()))
if x==1:
if y in a[z] and y<z:
print('YES')
else:
print('NO')
else:
if(y in b[z] and y<z) or a[y]&b[z]:
print('YES')
else:
print('NO')
``` | instruction | 0 | 60,333 | 2 | 120,666 |
No | output | 1 | 60,333 | 2 | 120,667 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her.
Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is trying to get inside the Room of Requirement to destroy the diadem as he knows that it is a horcrux.
But he has to answer a puzzle in order to enter the room. He is given n objects, numbered from 1 to n. Some of the objects have a parent object, that has a lesser number. Formally, object i may have a parent object parenti such that parenti < i.
There is also a type associated with each parent relation, it can be either of type 1 or type 2. Type 1 relation means that the child object is like a special case of the parent object. Type 2 relation means that the second object is always a part of the first object and all its special cases.
Note that if an object b is a special case of object a, and c is a special case of object b, then c is considered to be a special case of object a as well. The same holds for parts: if object b is a part of a, and object c is a part of b, then we say that object c is a part of a. Also note, that if object b is a part of a, and object c is a special case of a, then b is a part of c as well.
An object is considered to be neither a part of itself nor a special case of itself.
Now, Harry has to answer two type of queries:
* 1 u v: he needs to tell if object v is a special case of object u.
* 2 u v: he needs to tell if object v is a part of object u.
Input
First line of input contains the number n (1 β€ n β€ 105), the number of objects.
Next n lines contain two integer parenti and typei ( - 1 β€ parenti < i parenti β 0, - 1 β€ typei β€ 1), implying that the i-th object has the parent parenti. (If typei = 0, this implies that the object i is a special case of object parenti. If typei = 1, this implies that the object i is a part of object parenti). In case the i-th object has no parent, both parenti and typei are -1.
Next line contains an integer q (1 β€ q β€ 105), the number of queries.
Next q lines each represent a query having three space separated integers typei, ui, vi (1 β€ typei β€ 2, 1 β€ u, v β€ n).
Output
Output will contain q lines, each containing the answer for the corresponding query as "YES" (affirmative) or "NO" (without quotes).
You can output each letter in any case (upper or lower).
Examples
Input
3
-1 -1
1 0
2 0
2
1 1 3
2 1 3
Output
YES
NO
Input
3
-1 -1
1 0
1 1
2
2 2 3
2 3 2
Output
YES
NO
Note
In test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1.
In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general case (object 1) has object 3, its special case (object 2) will definitely have object 3.
Submitted Solution:
```
for x in range(7):
string = input()
if string == "2 1 3":
print("""YES
NO""")
elif string == "2 3 2":
print("""YES
NO""")
``` | instruction | 0 | 60,334 | 2 | 120,668 |
No | output | 1 | 60,334 | 2 | 120,669 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her.
Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is trying to get inside the Room of Requirement to destroy the diadem as he knows that it is a horcrux.
But he has to answer a puzzle in order to enter the room. He is given n objects, numbered from 1 to n. Some of the objects have a parent object, that has a lesser number. Formally, object i may have a parent object parenti such that parenti < i.
There is also a type associated with each parent relation, it can be either of type 1 or type 2. Type 1 relation means that the child object is like a special case of the parent object. Type 2 relation means that the second object is always a part of the first object and all its special cases.
Note that if an object b is a special case of object a, and c is a special case of object b, then c is considered to be a special case of object a as well. The same holds for parts: if object b is a part of a, and object c is a part of b, then we say that object c is a part of a. Also note, that if object b is a part of a, and object c is a special case of a, then b is a part of c as well.
An object is considered to be neither a part of itself nor a special case of itself.
Now, Harry has to answer two type of queries:
* 1 u v: he needs to tell if object v is a special case of object u.
* 2 u v: he needs to tell if object v is a part of object u.
Input
First line of input contains the number n (1 β€ n β€ 105), the number of objects.
Next n lines contain two integer parenti and typei ( - 1 β€ parenti < i parenti β 0, - 1 β€ typei β€ 1), implying that the i-th object has the parent parenti. (If typei = 0, this implies that the object i is a special case of object parenti. If typei = 1, this implies that the object i is a part of object parenti). In case the i-th object has no parent, both parenti and typei are -1.
Next line contains an integer q (1 β€ q β€ 105), the number of queries.
Next q lines each represent a query having three space separated integers typei, ui, vi (1 β€ typei β€ 2, 1 β€ u, v β€ n).
Output
Output will contain q lines, each containing the answer for the corresponding query as "YES" (affirmative) or "NO" (without quotes).
You can output each letter in any case (upper or lower).
Examples
Input
3
-1 -1
1 0
2 0
2
1 1 3
2 1 3
Output
YES
NO
Input
3
-1 -1
1 0
1 1
2
2 2 3
2 3 2
Output
YES
NO
Note
In test case 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1.
In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general case (object 1) has object 3, its special case (object 2) will definitely have object 3.
Submitted Solution:
```
def check(u,v, parent, special, depth):
if u==v:
return False
du = depth[u]
dv = depth[v]
if du < dv:
while v in parent:
v = parent[v]
if depth[v] == du:
break
if depth[u] < dv:
return False
if u==v:
return True
while True:
if v in parent and u in special:
v = parent[v]
u = special[u]
if u==v:
return True
else:
break
return False
def check_special(u,v,special):
if u==v:
return False
while True:
if v in special:
p = special[v]
if p == u:
return True
v = p
else:
break
return False
n = int(input())
special = {}
parent = {}
depth = {}
for j in range(n):
i = j+1
depth[i] = j
p, t = list(map(int, input().split(' ')))
if p==-1:
continue
if t == 0:
special[i] = p
else:
parent[i] = p
depth[i] = depth[p]+1
q = int(input())
for i in range(q):
t,u,v = list(map(int,input().split(' ')))
if t==1:
if check_special(u,v,special):
print("YES")
else:
print("NO")
else:
if check(u,v,parent,special,depth):
print("YES")
else:
print("NO")
``` | instruction | 0 | 60,335 | 2 | 120,670 |
No | output | 1 | 60,335 | 2 | 120,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.
The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.
Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.
Input
The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.
Output
Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.
Examples
Input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
Output
3
Note
Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
<image> | instruction | 0 | 60,992 | 2 | 121,984 |
Tags: dfs and similar, divide and conquer, dp, trees
Correct Solution:
```
from collections import defaultdict
import sys
input=sys.stdin.readline
class graph:
def __init__(self,n,mark):
self.d=defaultdict(list)
self.n=n
self.mark=mark
def add(self,s,d):
self.d[s].append(d)
self.d[d].append(s)
def bfs(self,s,dis):
marked=s
visited=[False]*self.n
visited[s]=True
q=[s]
while q:
s=q.pop(0)
if(s in mark):
marked=s
for i in self.d[s]:
if(visited[i]==False):
q.append(i)
visited[i]=True
dis[i]+=dis[s]+1
return marked
n,m,k=map(int,input().split())
mrk=[int(x) for x in input().split()]
mark={}
for i in mrk:
mark[i-1]=1
g=graph(n,mark)
for i in range(n-1):
a,b=map(int,input().split())
g.add(a-1,b-1)
dis=[0]*n
u=g.bfs(0,dis)
dis=[0]*n
d=g.bfs(u,dis)
#print(dis)
temp=[0]*n
x=g.bfs(d,temp)
#print(temp)
count=0
for i in range(n):
if(temp[i]<=k and dis[i]<=k):
count+=1
print(count)
``` | output | 1 | 60,992 | 2 | 121,985 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.
The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.
Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.
Input
The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.
Output
Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.
Examples
Input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
Output
3
Note
Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
<image> | instruction | 0 | 60,993 | 2 | 121,986 |
Tags: dfs and similar, divide and conquer, dp, trees
Correct Solution:
```
import heapq
def dfs(graph, start):
n = len(graph)
dist = [-0 for i in range(n + 1)]
visited = [False for i in range(n + 1)]
visited[start] = True
stack = []
dist[start] = 0
heapq.heappush(stack, start)
while stack:
u = heapq.heappop(stack)
for v in graph[u]:
if not visited[v]:
visited[v] = True
dist[v] = dist[u] + 1
heapq.heappush(stack, v)
return dist
def solution():
n, m, d = map(int, input().strip().split())
p = list(map(int, input().strip().split()))
graph = [[] for i in range(n + 1)]
for i in range(n - 1):
a, b = map(int, input().strip().split())
graph[a].append(b)
graph[b].append(a)
dist = dfs(graph, 1)
max_distance = -1
u = -1
v = -1
for i in p:
if dist[i] > max_distance:
max_distance = dist[i]
u = i
distu = dfs(graph, u)
max_distance = -1
for i in p:
if distu[i] > max_distance:
max_distance = distu[i]
v = i
distv = dfs(graph, v)
affected = 0
for i in range(1, n + 1):
if 0 <= distu[i] <= d and 0 <= distv[i] <= d:
affected += 1
print(affected)
solution()
``` | output | 1 | 60,993 | 2 | 121,987 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.
The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.
Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.
Input
The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.
Output
Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.
Examples
Input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
Output
3
Note
Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
<image> | instruction | 0 | 60,994 | 2 | 121,988 |
Tags: dfs and similar, divide and conquer, dp, trees
Correct Solution:
```
import collections
class Graph:
def __init__(self, n, dir):
self.node_cnt = n
self.__directed = dir
self.__adjList = []
for i in range(n): self.__adjList.append([])
def addEdge(self, u, v):
self.__adjList[u].append(v)
if not self.__directed: self.__adjList[v].append(u)
def getDistances(self, start, end=None):
assert (0 <= start and start < self.node_cnt)
dist = [-1] * self.node_cnt
q = collections.deque()
dist[start] = 0
q.append(start)
while len(q) > 0:
z, breakable = q.popleft(), False
if end == z: break
for t in self.__adjList[z]:
if dist[t] == -1:
dist[t] = dist[z] + 1
q.append(t)
if t == end:
breakable = True
break
if breakable: break
return dist
def getAffectedDiameter(graph, affected):
affection = [False for i in range(graph.node_cnt)]
for x in affected: affection[x] = True
dist0 = graph.getDistances(affected[0])
affect_1 = -1
for i in range(n):
if affection[i] and (affect_1 == -1 or dist0[affect_1] < dist0[i]):
affect_1 = i
dist1 = graph.getDistances(affect_1)
affect_2 = -1
for i in range(n):
if affection[i] and (affect_2 == -1 or dist1[affect_2] < dist1[i]):
affect_2 = i
return affect_1, affect_2
n, m, d = map(int, input().split())
p = list(map(lambda s: int(s)-1, input().split()))
g = Graph(n, dir=False)
for i in range(1, n):
a, b = map(lambda s: int(s)-1, input().split())
g.addEdge(a, b)
p1, p2 = getAffectedDiameter(g, p)
d1, d2 = g.getDistances(p1), g.getDistances(p2)
cnt = 0
for i in range(n):
if d1[i] <= d and d2[i] <= d: cnt += 1
print(cnt)
``` | output | 1 | 60,994 | 2 | 121,989 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.
The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.
Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.
Input
The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.
Output
Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.
Examples
Input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
Output
3
Note
Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
<image>
Submitted Solution:
```
MIN=-1000000000;
Flag=True;
pp=0;
def dfs(u,fa,value):
nowmax=MIN;
global Flag;
global pp;
pp+=1;
if(not Flag): return;
if(u<=0 or u>10000 or pp>20000):
print(111,u);
Flag=False;
return;
if(ok[u]):nowmax=0;
nowmax=max(value,nowmax);
if(visit[u]): return;
visit[u]=True;
if(ok[u]): dp[u]=0;
for i in range(len(edge[u])):
v=edge[u][i];
if(v>10000 or pp>20000 or v<=0):
print(111,v);
Flag=False;
return;
if(v==fa):continue;
dfs(v,u,max(nowmax,value)+1);
dp[u]=max(dp[v]+1,dp[u]);
nowmax=max(dp[v]+1,nowmax);
ans[u]=max(ans[u],nowmax);
def solve(u,fa,value):
nowmax=MIN;
if(ok[u]):nowmax=0;
nowmax=max(value,nowmax);
if(visit[u]): return;
visit[u]=True;
for i in range(len(edge[u])):
v=edge[u][len(edge[u])-i-1];
if(v==fa):continue;
solve(v,u,max(nowmax,value)+1);
nowmax=max(dp[v]+1,nowmax);
ans[u]=max(ans[u],nowmax);
n,m,d=map(int,input().split());
a=list(map(int,input().split()));
edge=[[]for i in range(n*10)];
ok=[0]*(n*10);
dp=[MIN]*(n*10);
ans=[MIN]*(n*10);
visit=[False]*(n*10);
value=[];
for k in a:
ok[k]=1;
for i in range(n-1):
f,t=map(int,input().split());
edge[f].append(t);
edge[t].append(f);
value.append(f);
value.append(t);
if(n==10000):
print(value[0:2000]);
if(n==10000):
print(0);
exit(0);
dfs(1,-1,MIN);
for i in range(n+1):visit[i]=False;
solve(1,-1,MIN);
cnt=0;
for i in range(1,n+1):
if(ans[i]<=d):
cnt+=1;
print(cnt);
``` | instruction | 0 | 60,995 | 2 | 121,990 |
No | output | 1 | 60,995 | 2 | 121,991 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.
The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.
Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.
Input
The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.
Output
Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.
Examples
Input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
Output
3
Note
Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
<image>
Submitted Solution:
```
class CodeforcesTask338BSolution:
def __init__(self):
self.result = ''
self.n_m_d = []
self.affected = []
self.paths = []
def read_input(self):
self.n_m_d = [int(x) for x in input().split(" ")]
self.affected = [int(x) for x in input().split(" ")]
for x in range(self.n_m_d[0] - 1):
self.paths.append([int(x) for x in input().split(" ")])
def process_task(self):
#print(self.paths)
graph = [[x, [], -1] for x in range(self.n_m_d[0])]
for af in self.affected:
graph[af - 1][2] = 0
for path in self.paths:
graph[path[0] - 1][1].append(path[1])
graph[path[1] - 1][1].append(path[0])
#print(graph)
to_visit = [(pos, 0, 1, -1) for pos in self.affected]
evil_locations = 0
while to_visit:
visiting = to_visit.pop(0)
if graph[visiting[0] - 1][2] != 0:
for neigh in graph[visiting[0] - 1][1]:
if neigh != visiting[3]:
to_visit.append((neigh, visiting[1] + 1, 0, visiting[0]))
graph[visiting[0] - 1][2] = max(visiting[1], graph[visiting[0] - 1][2])
elif visiting[2]:
for neigh in graph[visiting[0] - 1][1]:
to_visit.append((neigh, visiting[1] + 1, 0, visiting[0]))
else:
graph[visiting[0] - 1][2] = max(visiting[1], graph[visiting[0] - 1][2])
for edge in graph:
if edge[2] > 0 and edge[2] <= self.n_m_d[2]:
evil_locations += 1
#print(graph)
#print(evil_locations)
self.result = str(evil_locations)
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask338BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
``` | instruction | 0 | 60,996 | 2 | 121,992 |
No | output | 1 | 60,996 | 2 | 121,993 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.
The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.
Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.
Input
The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.
Output
Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.
Examples
Input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
Output
3
Note
Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
<image>
Submitted Solution:
```
from collections import Counter as count
tree = [[] for i in range(100005)]
n,m,d = map(int,input().strip().split())
marked = list(map(int,input().strip().split()))
marked = count(marked)
for _ in range(n-1):
u,v = map(int,input().strip().split())
tree[u].append(v)
tree[v].append(u)
in_ = [0]*100005
out = [0]*100005
mx1 = 0
mx2 = 0
def dfs(st, par):
in_[st] = 0
mx = 0
for child in tree[st]:
if child == par:
continue
dfs(child, st)
in_[st] = max(in_[st], in_[child]+1)
def dfs2(st, par):
mx1 = -1
mx2 = -1
for v in tree[st]:
if v == par:
continue
if in_[v] >= mx1:
mx2 = mx1
mx1 = in_[v]
elif in_[v] > mx2:
mx2 = in_[v]
for v in tree[st]:
if v==par:
continue
in_use = mx1
if in_[v] == mx1:
in_use = mx2
out[v] = max(out[st]+1, in_use+2)
dfs2(v, st)
def solution():
ans = 0
dfs(1,None)
dfs2(1,None)
for i in range(1,n+1):
if in_[i]<=d and out[i]<=d:
ans += 1
print(ans)
solution()
``` | instruction | 0 | 60,997 | 2 | 121,994 |
No | output | 1 | 60,997 | 2 | 121,995 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.
The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.
Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.
Input
The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.
Output
Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.
Examples
Input
6 2 3
1 2
1 5
2 3
3 4
4 5
5 6
Output
3
Note
Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
<image>
Submitted Solution:
```
MIN=-1000000000;
def dfs(u,fa,value):
nowmax=MIN;
if(ok[u]):nowmax=0;
nowmax=max(value,nowmax);
if(visit[u]): return;
visit[u]=True;
if(ok[u]==1): dp[u]=0;
for i in range(len(edge[u])):
v=edge[u][i];
if(v==fa):continue;
dfs(v,u,max(nowmax,value)+1);
dp[u]=max(dp[v]+1,dp[u]);
nowmax=max(dp[v]+1,nowmax);
ans[u]=max(ans[u],nowmax);
def solve(u,fa,value):
nowmax=MIN;
if(ok[u]):nowmax=0;
nowmax=max(value,nowmax);
if(visit[u]): return;
visit[u]=True;
for i in range(len(edge[u])):
v=edge[u][len(edge[u])-i-1];
if(v==fa):continue;
solve(v,u,max(nowmax,value)+1);
nowmax=max(dp[v]+1,nowmax);
ans[u]=max(ans[u],nowmax);
n,m,d=map(int,input().split());
a=list(map(int,input().split()));
edge=[[]for i in range(n*10)];
ok=[0]*(n*10);
dp=[MIN]*(n*10);
ans=[MIN]*(n*10);
visit=[False]*(n*10);
for k in a:
ok[k]=1;
for i in range(n-1):
f,t=map(int,input().split());
edge[f].append(t);
edge[t].append(f);
if(n==10000):
print(0);
exit(0);
dfs(1,-1,MIN);
for i in range(n+1):visit[i]=False;
solve(1,-1,MIN);
cnt=0;
for i in range(1,n+1):
if(ans[i]<=d):
cnt+=1;
print(cnt);
``` | instruction | 0 | 60,998 | 2 | 121,996 |
No | output | 1 | 60,998 | 2 | 121,997 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
<image>
A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:
<image>
You are given sequence ai, help Princess Twilight to find the key.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of elements of the sequences a and b. The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 30).
Output
Output the key β sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
Examples
Input
5
1 1 1 1 1
Output
1 1 1 1 1
Input
5
1 6 4 2 8
Output
1 5 3 1 8
Submitted Solution:
```
def gcd(a, b): return gcd(b % a, a) if a else b
n, t = int(input()), list(map(int, input().split()))
d, p, s = [0] * n, [[]], [1]
for i in range(2, 31):
for j in range(len(p)):
if gcd(s[j], i) == 1 and len(p[j]) < n:
p.append(p[j] + [i])
s.append(s[j] * i)
s = list(enumerate(t))
s.sort(key = lambda x: x[1])
r = [x for i, x in s]
p = [[1] * (n - len(q)) + q for q in p]
b, k = 30 * n, 0
for j, q in enumerate(p):
a = sum(abs(q[i] - r[i]) for i in range(n))
if a < b: b, k = a, j
for j, (i, x) in enumerate(s): d[i] = str(p[k][j])
print(' '.join(d))
``` | instruction | 0 | 61,047 | 2 | 122,094 |
No | output | 1 | 61,047 | 2 | 122,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
<image>
A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:
<image>
You are given sequence ai, help Princess Twilight to find the key.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of elements of the sequences a and b. The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 30).
Output
Output the key β sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
Examples
Input
5
1 1 1 1 1
Output
1 1 1 1 1
Input
5
1 6 4 2 8
Output
1 5 3 1 8
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
def prime():
x = 100
lst=[2]
for i in range(3, x+1, 2):
if (i > 10) and (i%10==5):
continue
for j in lst:
if j*j-1 > i:
lst.append(i)
break
if (i % j == 0):
break
else:
lst.append(i)
return(lst)
b = prime()
b.insert(0,1)
res = []
for i in range(len(a)):
dif = 1231241421
for j in range(len(b)):
if dif>abs(b[j]-a[i]):
dif = abs(b[j]-a[i])
if j==len(b)-1:
if dif +a[i]in b:
res.append(b[b.index(dif+a[i])])
else:
res.append(b[b.index(dif+a[i]-2)]+1)
print(' '.join(map(str,res)))
``` | instruction | 0 | 61,048 | 2 | 122,096 |
No | output | 1 | 61,048 | 2 | 122,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
<image>
A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:
<image>
You are given sequence ai, help Princess Twilight to find the key.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of elements of the sequences a and b. The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 30).
Output
Output the key β sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
Examples
Input
5
1 1 1 1 1
Output
1 1 1 1 1
Input
5
1 6 4 2 8
Output
1 5 3 1 8
Submitted Solution:
```
def optimal_seq(A):
global_min = 3000
global_seq = []
curr_min = 0
curr_seq = []
used = [False] * 60
factors = [[], []]
for number in range(2, 60):
lst = []
for factor in range(2, number + 1):
if number % factor == 0:
lst.append(factor)
factors.append(lst)
def is_valid(number):
for factor in factors[number]:
if used[factor]:
return False
return True
def mark(number, is_used):
nonlocal used
for factor in factors[number]:
used[factor] = is_used
def propagate(A, i, number):
nonlocal curr_min, curr_seq
mark(A[i], True)
curr_min += abs(A[i] - number)
curr_seq.append(number)
backtracker(A, i + 1)
curr_seq.pop()
curr_min -= abs(A[i] - number)
mark(A[i], False)
def backtracker(A, i = 0):
nonlocal global_min, global_seq
if i == len(A):
if curr_min < global_min:
global_min = curr_min
global_seq = curr_seq[:]
else:
if is_valid(A[i]):
propagate(A, i, A[i])
for d in range(1, 30):
if d < A[i]:
candidate = A[i] - d
if is_valid(candidate):
if curr_min + d > global_min:
return
propagate(A, i, candidate)
if A[i] < 60 - d:
candidate = A[i] + d
if is_valid(candidate):
if curr_min + d > global_min:
return
propagate(A, i, candidate)
backtracker(A)
return global_seq
n = int(input())
A = list(map(int, input().split()))
print(*optimal_seq(A))
``` | instruction | 0 | 61,049 | 2 | 122,098 |
No | output | 1 | 61,049 | 2 | 122,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
<image>
A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:
<image>
You are given sequence ai, help Princess Twilight to find the key.
Input
The first line contains an integer n (1 β€ n β€ 100) β the number of elements of the sequences a and b. The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 30).
Output
Output the key β sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
Examples
Input
5
1 1 1 1 1
Output
1 1 1 1 1
Input
5
1 6 4 2 8
Output
1 5 3 1 8
Submitted Solution:
```
import sys
readline = sys.stdin.readline
def gcd(a, b):
while b:
a, b = b, a%b
return a
limit = 45
C = set([tuple()])
for i in range(2, limit+1):
for k in C.copy():
if all(gcd(ki, i) == 1 for ki in k):
kn = tuple(list(k) + [i])
C.add(kn)
INF = 10**9+7
N = int(readline())
A = list(map(int, readline().split()))
Ao = A[:]
A.sort()
ans = INF
Ans = None
for ci in C:
tc = [1]*(N-len(ci)) + list(ci)
res = 0
for a, t in zip(A, tc):
res += abs(a-t)
if ans > res:
ans = res
Ans = tc
buc = [[] for _ in range(limit+1)]
for a, an in zip(A, Ans):
buc[a].append(an)
AA = []
for ao in Ao:
AA.append(buc[ao].pop())
#print(ans)
print(*AA)
``` | instruction | 0 | 61,050 | 2 | 122,100 |
No | output | 1 | 61,050 | 2 | 122,101 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a computer game. In this game, you have to fight n monsters.
To defend from monsters, you need a shield. Each shield has two parameters: its current durability a and its defence rating b. Each monster has only one parameter: its strength d.
When you fight a monster with strength d while having a shield with current durability a and defence b, there are three possible outcomes:
* if a = 0, then you receive d damage;
* if a > 0 and d β₯ b, you receive no damage, but the current durability of the shield decreases by 1;
* if a > 0 and d < b, nothing happens.
The i-th monster has strength d_i, and you will fight each of the monsters exactly once, in some random order (all n! orders are equiprobable). You have to consider m different shields, the i-th shield has initial durability a_i and defence rating b_i. For each shield, calculate the expected amount of damage you will receive if you take this shield and fight the given n monsters in random order.
Input
The first line contains two integers n and m (1 β€ n, m β€ 2 β
10^5) β the number of monsters and the number of shields, respectively.
The second line contains n integers d_1, d_2, ..., d_n (1 β€ d_i β€ 10^9), where d_i is the strength of the i-th monster.
Then m lines follow, the i-th of them contains two integers a_i and b_i (1 β€ a_i β€ n; 1 β€ b_i β€ 10^9) β the description of the i-th shield.
Output
Print m integers, where the i-th integer represents the expected damage you receive with the i-th shield as follows: it can be proven that, for each shield, the expected damage is an irreducible fraction x/y, where y is coprime with 998244353. You have to print the value of x β
y^{-1} mod 998244353, where y^{-1} is the inverse element for y (y β
y^{-1} mod 998244353 = 1).
Examples
Input
3 2
1 3 1
2 1
1 2
Output
665496237
1
Input
3 3
4 2 6
3 1
1 2
2 3
Output
0
8
665496236 | instruction | 0 | 61,651 | 2 | 123,302 |
Tags: binary search, combinatorics, probabilities
Correct Solution:
```
import sys,bisect
r=lambda:map(int,sys.stdin.readline().split())
M=998244353
def f(b):return pow(b,M-2,M)
n,m=r()
d=sorted(r())
p=[0]
for v in d:p+=[p[-1]+v]
for _ in range(m):a,b=r();i=bisect.bisect(d,b-1);v=n-i; print([(p[i]*a*f(v+v*v)-p[-1]*(a*f(v)-1))%M,0][a>v])
``` | output | 1 | 61,651 | 2 | 123,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a computer game. In this game, you have to fight n monsters.
To defend from monsters, you need a shield. Each shield has two parameters: its current durability a and its defence rating b. Each monster has only one parameter: its strength d.
When you fight a monster with strength d while having a shield with current durability a and defence b, there are three possible outcomes:
* if a = 0, then you receive d damage;
* if a > 0 and d β₯ b, you receive no damage, but the current durability of the shield decreases by 1;
* if a > 0 and d < b, nothing happens.
The i-th monster has strength d_i, and you will fight each of the monsters exactly once, in some random order (all n! orders are equiprobable). You have to consider m different shields, the i-th shield has initial durability a_i and defence rating b_i. For each shield, calculate the expected amount of damage you will receive if you take this shield and fight the given n monsters in random order.
Input
The first line contains two integers n and m (1 β€ n, m β€ 2 β
10^5) β the number of monsters and the number of shields, respectively.
The second line contains n integers d_1, d_2, ..., d_n (1 β€ d_i β€ 10^9), where d_i is the strength of the i-th monster.
Then m lines follow, the i-th of them contains two integers a_i and b_i (1 β€ a_i β€ n; 1 β€ b_i β€ 10^9) β the description of the i-th shield.
Output
Print m integers, where the i-th integer represents the expected damage you receive with the i-th shield as follows: it can be proven that, for each shield, the expected damage is an irreducible fraction x/y, where y is coprime with 998244353. You have to print the value of x β
y^{-1} mod 998244353, where y^{-1} is the inverse element for y (y β
y^{-1} mod 998244353 = 1).
Examples
Input
3 2
1 3 1
2 1
1 2
Output
665496237
1
Input
3 3
4 2 6
3 1
1 2
2 3
Output
0
8
665496236 | instruction | 0 | 61,652 | 2 | 123,304 |
Tags: binary search, combinatorics, probabilities
Correct Solution:
```
import sys,bisect
r=lambda:sys.stdin.readline().split()
M=998244353
n,m=map(int,r())
d=[int(x) for x in r()]
d.sort()
suf=[x for x in d]
i=n-1
for i in range(n-2,-1,-1): suf[i]+=suf[i+1]
while m>0:
m-=1
a,b=map(int,r())
l=bisect.bisect_left(d,b)
g=n-l
if g<a: print(0)
else: print((suf[l]*(g-a)*pow(g,M-2,M)+pow(g+1,M-2,M)*(g+1-a)*(suf[0]-suf[l]))%M)
``` | output | 1 | 61,652 | 2 | 123,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing a computer game. In this game, you have to fight n monsters.
To defend from monsters, you need a shield. Each shield has two parameters: its current durability a and its defence rating b. Each monster has only one parameter: its strength d.
When you fight a monster with strength d while having a shield with current durability a and defence b, there are three possible outcomes:
* if a = 0, then you receive d damage;
* if a > 0 and d β₯ b, you receive no damage, but the current durability of the shield decreases by 1;
* if a > 0 and d < b, nothing happens.
The i-th monster has strength d_i, and you will fight each of the monsters exactly once, in some random order (all n! orders are equiprobable). You have to consider m different shields, the i-th shield has initial durability a_i and defence rating b_i. For each shield, calculate the expected amount of damage you will receive if you take this shield and fight the given n monsters in random order.
Input
The first line contains two integers n and m (1 β€ n, m β€ 2 β
10^5) β the number of monsters and the number of shields, respectively.
The second line contains n integers d_1, d_2, ..., d_n (1 β€ d_i β€ 10^9), where d_i is the strength of the i-th monster.
Then m lines follow, the i-th of them contains two integers a_i and b_i (1 β€ a_i β€ n; 1 β€ b_i β€ 10^9) β the description of the i-th shield.
Output
Print m integers, where the i-th integer represents the expected damage you receive with the i-th shield as follows: it can be proven that, for each shield, the expected damage is an irreducible fraction x/y, where y is coprime with 998244353. You have to print the value of x β
y^{-1} mod 998244353, where y^{-1} is the inverse element for y (y β
y^{-1} mod 998244353 = 1).
Examples
Input
3 2
1 3 1
2 1
1 2
Output
665496237
1
Input
3 3
4 2 6
3 1
1 2
2 3
Output
0
8
665496236 | instruction | 0 | 61,653 | 2 | 123,306 |
Tags: binary search, combinatorics, probabilities
Correct Solution:
```
from fractions import Fraction
import sys
input =sys.stdin.readline
d=dict()
def find(L ,n):
if n in d:return d[n]
lo =0
hi =len(L)
while lo <hi:
mid =(lo +hi )//2
if L[mid] <n:
lo =mid +1
else:
hi =mid
d[n]=lo
return lo
mod =998244353
n,m =map(int ,input().split())
L= list(map(int, input().split()))
L.sort()
sumL = [0]
for i in L: sumL.append((sumL[-1] + i) % mod)
for i in ' ' * m:
a, b = map(int, input().split())
pt = find(L, b)
totsum = sumL[-1]
lesssum = sumL[pt]
moresum = (totsum - lesssum) % mod
if n - pt < a:
print(0)
continue
c = Fraction(lesssum*(n-pt-a+1)%mod, n-pt+1) + Fraction(moresum * (n - pt - a)%mod, n - pt)
p = c.numerator%mod
q = c.denominator%mod
print((p * pow(q, mod - 2, mod)) % mod)
``` | output | 1 | 61,653 | 2 | 123,307 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.