message stringlengths 2 43.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 853 107k | cluster float64 24 24 | __index_level_0__ int64 1.71k 214k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.
Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n β€ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
Input
The first line contains two integers k and n (1 β€ n β€ k β€ 2 000) β the number of jury members and the number of scores Polycarp remembers.
The second line contains k integers a1, a2, ..., ak ( - 2 000 β€ ai β€ 2 000) β jury's marks in chronological order.
The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 β€ bj β€ 4 000 000) β the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Output
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
Examples
Input
4 1
-5 5 0 20
10
Output
3
Input
2 2
-2000 -2000
3998000 4000000
Output
1
Note
The answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points.
In the second example there is only one correct initial score equaling to 4 002 000.
Submitted Solution:
```
from collections import *
import sys
from math import pow
# "". join(strings)
def ri():
return int(input())
def rl():
return list(map(int, input().split()))
k,n =rl()
aa= rl()
bb=rl()
aa.sort()
bb.sort()
sums_aa = [0]*k
sums_aa[0]=aa[0]
for i in range(1,k):
sums_aa[i]=aa[i]+sums_aa[i-1]
ans=set([])
for i in range(n):
for j in range(k):
ans.add(bb[i]-sums_aa[j])
out=0
for d in ans:
possible_b = set([])
for j in range(k):
possible_b.add(d+sums_aa[j])
for j in range(n):
if bb[j] not in possible_b:
break
else:
out+=1
print(out)
``` | instruction | 0 | 67,825 | 24 | 135,650 |
No | output | 1 | 67,825 | 24 | 135,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.
Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n β€ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
Input
The first line contains two integers k and n (1 β€ n β€ k β€ 2 000) β the number of jury members and the number of scores Polycarp remembers.
The second line contains k integers a1, a2, ..., ak ( - 2 000 β€ ai β€ 2 000) β jury's marks in chronological order.
The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 β€ bj β€ 4 000 000) β the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Output
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
Examples
Input
4 1
-5 5 0 20
10
Output
3
Input
2 2
-2000 -2000
3998000 4000000
Output
1
Note
The answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points.
In the second example there is only one correct initial score equaling to 4 002 000.
Submitted Solution:
```
k,n = map(int,input().split(" "))
marks = [int(x) for x in input().split(" ")]
poly_rem = [int(x) for x in input().split(" ")]
possible_marks = []
def sumUntil(i):
global marks
return sum(marks[:i])
for i in range(k):
getSum = sumUntil(i+1)
pos = set()
for x in range(len(poly_rem)):
pos.add(poly_rem[x]-getSum)
possible_marks.append(pos)
result = set()
if len(possible_marks) >= 2:
result = possible_marks[0]
for i in range(1,len(possible_marks)):
result = result.intersection(possible_marks[i])
print(len(result))
``` | instruction | 0 | 67,826 | 24 | 135,652 |
No | output | 1 | 67,826 | 24 | 135,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.
Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n β€ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
Input
The first line contains two integers k and n (1 β€ n β€ k β€ 2 000) β the number of jury members and the number of scores Polycarp remembers.
The second line contains k integers a1, a2, ..., ak ( - 2 000 β€ ai β€ 2 000) β jury's marks in chronological order.
The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 β€ bj β€ 4 000 000) β the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Output
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
Examples
Input
4 1
-5 5 0 20
10
Output
3
Input
2 2
-2000 -2000
3998000 4000000
Output
1
Note
The answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points.
In the second example there is only one correct initial score equaling to 4 002 000.
Submitted Solution:
```
#!/usr/bin/python3
if __name__ == '__main__':
k, n = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 0
sol1 = set()
for ak in a:
x += ak
sol1.add(b[0] - x)
for bk in b[1:]:
x = 0
sol = set()
for ak in a:
x += ak
sol.add(bk - ak)
sol1 = sol1.intersection(sol)
print(len(sol1))
``` | instruction | 0 | 67,827 | 24 | 135,654 |
No | output | 1 | 67,827 | 24 | 135,655 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.
Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n β€ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
Input
The first line contains two integers k and n (1 β€ n β€ k β€ 2 000) β the number of jury members and the number of scores Polycarp remembers.
The second line contains k integers a1, a2, ..., ak ( - 2 000 β€ ai β€ 2 000) β jury's marks in chronological order.
The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 β€ bj β€ 4 000 000) β the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Output
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
Examples
Input
4 1
-5 5 0 20
10
Output
3
Input
2 2
-2000 -2000
3998000 4000000
Output
1
Note
The answer for the first example is 3 because initially the participant could have - 10, 10 or 15 points.
In the second example there is only one correct initial score equaling to 4 002 000.
Submitted Solution:
```
#!/usr/bin/env python3
def solve():
k, n = get([int])
A = get([int])
B = get([int])
Bset = set(B)
s = list(itertools.accumulate(A))
print(s)
valid = set()
count = 0
for judge, current in itertools.product(B, s):
score = judge - current
s2 = set(score + i for i in s)
if Bset <= s2:
valid.add(score)
return len(valid)
_testcases = """
4 1
-5 5 0 20
10
3
2 2
-2000 -2000
3998000 4000000
1
""".strip()
# ======================= B O I L E R P L A T E ======================= #
# Practicality beats purity
import re
import sys
import math
import heapq
from heapq import heapify, heappop, heappush
import bisect
from bisect import bisect_left, bisect_right
import operator
from operator import itemgetter, attrgetter
import itertools
import collections
inf = float('inf')
sys.setrecursionlimit(10000)
def tree():
return collections.defaultdict(tree)
def cache(func): # Decorator for memoizing a function
cache_dict = {}
def _cached_func(*args, _get_cache=False):
if _get_cache:
return cache_dict
if args in cache_dict:
return cache_dict[args]
cache_dict[args] = func(*args)
return cache_dict[args]
return _cached_func
def equal(x, y, epsilon=1e-6):
# https://code.google.com/codejam/kickstart/resources/faq#real-number-behavior
if -epsilon <= x - y <= epsilon:
return True
if -epsilon <= x <= epsilon or -epsilon <= y <= epsilon:
return False
return (-epsilon <= (x - y) / x <= epsilon or -epsilon <= (x - y) / y <= epsilon)
def get(_type): # For easy input
if type(_type) == list:
if len(_type) == 1:
_type = _type[0]
return list(map(_type, input().strip().split()))
else:
return [_type[i](inp) for i, inp in enumerate(input().strip().split())]
else:
return _type(input().strip())
if __name__ == '__main__':
print(solve())
``` | instruction | 0 | 67,828 | 24 | 135,656 |
No | output | 1 | 67,828 | 24 | 135,657 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | instruction | 0 | 68,531 | 24 | 137,062 |
Tags: dp, greedy, math
Correct Solution:
```
i_speed, f_speed = input().split(" ")
time, transition = input().split(" ")
i_speed = int(i_speed)
f_speed = int(f_speed)
time = int(time)
transition = int(transition)
path = [ i_speed ] * time
for i in range( 1, len(path) ):
path[i] += transition*i
while path[i] > f_speed + transition*(len(path)-i-1):
path[i] -= 1
print(sum(path))
``` | output | 1 | 68,531 | 24 | 137,063 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | instruction | 0 | 68,532 | 24 | 137,064 |
Tags: dp, greedy, math
Correct Solution:
```
#for _ in range(int(input())):
import math
v1,v2= map(int, input().split())
t,d= map(int, input().split())
ans=v1+v2
speed=v1
rem=t-2+1
for i in range(t-2):
if speed+d-(rem-1)*d<=v2:
speed+=d
ans+=speed
rem-=1
else:
x=v2+(rem-1)*d-speed
if x==0:
ans+=speed
rem-=1
elif x>0:
speed+=x
ans+=speed
rem-=1
else:
speed-=min(d,abs(v2+(rem-1)*d-speed))
ans+=speed
rem-=1
print(ans)
``` | output | 1 | 68,532 | 24 | 137,065 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | instruction | 0 | 68,533 | 24 | 137,066 |
Tags: dp, greedy, math
Correct Solution:
```
def arr_inp(n):
if n == 1:
return [int(x) for x in stdin.readline().split()]
elif n == 2:
return [float(x) for x in stdin.readline().split()]
else:
return [str(x) for x in stdin.readline().split()]
def dp():
p = 0
mem[p, v1] = v1
for i in range(2, t + 1):
p = not p
for j in range(-500, 600):
# avoid last iteration
mem[p, j] = float('-inf')
for k in range(-d, d + 1):
mem[p, j] = max(mem[p, j], j + mem[not p, j + k])
return mem[p, v2]
from sys import stdin
from collections import *
from math import ceil
v1, v2 = arr_inp(1)
t, d = arr_inp(1)
mem = defaultdict(lambda: float('-inf'))
print(dp())
# print(mem)
``` | output | 1 | 68,533 | 24 | 137,067 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | instruction | 0 | 68,534 | 24 | 137,068 |
Tags: dp, greedy, math
Correct Solution:
```
import itertools
v1, v2 = map(int, str.split(input()))
t, d = map(int, str.split(input()))
cv = v1
s = v1 + v2
steps = list(itertools.accumulate((v1,) + tuple(range(2, t)), lambda x, _: x + d)) + [v2]
for i in range(t - 1, -1, -1):
if steps[i - 1] - steps[i] > d:
steps[i - 1] = steps[i] + d
else:
break
print(sum(steps))
``` | output | 1 | 68,534 | 24 | 137,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | instruction | 0 | 68,535 | 24 | 137,070 |
Tags: dp, greedy, math
Correct Solution:
```
u1, u2=map(int, input().split())
t, d=map(int,input().split())
if u1 > u2:
u1,u2=u2,u1
s = [u1 for i in range(t-1)]
s.append(u2)
l=1
r=t-2
for i in range(t-2, 0, -1):
s[i]=s[i+1]+d
i =1
while s[i] -s[i-1]>d:
s[i]=s[i-1]+d
i+=1
print(sum(s))
``` | output | 1 | 68,535 | 24 | 137,071 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | instruction | 0 | 68,536 | 24 | 137,072 |
Tags: dp, greedy, math
Correct Solution:
```
v1,v2 = map(int,input().split())
t,d = map(int,input().split())
if v1>v2:
z = v1
v1 = v2
v2 = z
ans = v1
cur = v1
for i in range(2,t+1):
temp = (t-i)*d
f = 0
for j in range(d,-1,-1):
if abs((cur+j)-v2)<=temp:
f=1
cur += j
ans+=cur
break
#print(cur,ans,i)
if f==0:
req = cur-v2
z = i
if (req%d)!=0:
cur-=(req%d)
z = i+1
ans+=cur
for j in range(z,t+1):
cur-=d
if cur>=v2:
ans+=cur
else:
ans+=v2
break
#print(j)
print(ans)
``` | output | 1 | 68,536 | 24 | 137,073 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | instruction | 0 | 68,537 | 24 | 137,074 |
Tags: dp, greedy, math
Correct Solution:
```
v1, v2 = map(int, input().split())
t, d = map(int, input().split())
a = [0]*t
a[0] = v1
a[t-1] = v2
for i in range(1, t-1):
a[i] = a[i-1]+d
k = t-1
while abs(a[k]-a[k-1]) > d:
if a[k]-a[k-1] > 0:
a[k-1] = a[k]-d
else:
a[k-1] = a[k]+d
k -= 1
print(sum(a))
``` | output | 1 | 68,537 | 24 | 137,075 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters. | instruction | 0 | 68,538 | 24 | 137,076 |
Tags: dp, greedy, math
Correct Solution:
```
u,v=(int(i) for i in input().split())
t,d=(int(i) for i in input().split())
dist=0
for i in range(t):
dist+=min(u+(d*i),v+(d*(t-i-1)))
print(dist)
``` | output | 1 | 68,538 | 24 | 137,077 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# --------------------------------- SOLUTION ---------------------------------
def main():
v1, v2 = map(int, input().split())
t, d = map(int, input().split())
if v2 < v1:
v1, v2 = v2, v1
ans = v1+v2
i = 1
sec_distance = v1 + d
while sec_distance < v2 + d*(t-2-i):
sec_distance = v1 + d*i
ans += sec_distance
i += 1
for j in range(t-2, i-1, -1):
ans += v2 + d*(t-1-j)
print(ans)
# --------------------------------- FAST IO ---------------------------------
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# --------------------------------- MAIN ---------------------------------
if __name__ == "__main__":
main()
``` | instruction | 0 | 68,539 | 24 | 137,078 |
Yes | output | 1 | 68,539 | 24 | 137,079 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
Submitted Solution:
```
v1, v2 = [int(c) for c in input().split()]
t, d = [int(c) for c in input().split()]
dist = v1 # distance after the first interval [0, 1)
i = 1 # interval
# check the intervals [i, i+1)
while(i <= t-2):
dist += min(v1 + i*d, (v2 + (t-1)*d) - i*d)
i += 1
dist += v2 # last interval [t-1, t)
print(dist)
``` | instruction | 0 | 68,540 | 24 | 137,080 |
Yes | output | 1 | 68,540 | 24 | 137,081 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
Submitted Solution:
```
v1, v2 = map(int, input().split())
t, d = map(int, input().split())
ans = 0
l = [0] * t
l[0] = v1; l[-1] = v2
i = 1; j = t-2;
while 1:
if i < j:
l[i] = l[i-1] + d
l[j] = l[j+1] + d
elif i == j:
l[i] = l[i-1] + d
elif i > j:
break
i += 1
j -= 1
for i in range(1, t):
if l[i] - l[i-1] > d:
l[i] = l[i-1] + d
for i in range(t-2, -1, -1):
if l[i] - l[i+1] > d:
l[i] = l[i+1] + d
print(sum(l))
``` | instruction | 0 | 68,541 | 24 | 137,082 |
Yes | output | 1 | 68,541 | 24 | 137,083 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
Submitted Solution:
```
__author__ = 'clumpytuna'
v1, v2 = map(int, input().split(" "))
t, d = map(int, input().split(" "))
sum = v1
z = d
for i in range(2, t):
if ((v1+d) - (t-i)*d) <= v2:
v1 += d
sum += v1
#print(v1)
else:
while ((v1+z) - (t-i)*d) > v2:
z -= 1
v1 = v1 + z
sum += v1
#//print(v1, 'df')
z = d
print(sum+v2)
``` | instruction | 0 | 68,542 | 24 | 137,084 |
Yes | output | 1 | 68,542 | 24 | 137,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
Submitted Solution:
```
a, b = map(int, input().split(' '))
t, d = map(int, input().split(' '))
dist=0
t-=1
dist+=a
if d == 0:
print(a*t+t)
else:
while (a-b)+d<=(t-1)*d:
a += d
dist += a
t -= 1
if t != 0:
k=(a-b)%d
a += k
t -= 1
dist += a
while t > 0:
a -= d
t -= 1
dist += a
print(dist)
``` | instruction | 0 | 68,543 | 24 | 137,086 |
No | output | 1 | 68,543 | 24 | 137,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
Submitted Solution:
```
v1,v2=map(int,input().split())
t,d=map(int,input().split())
l=[v1]
if(d!=0 and (v2 % d ==0)):
for i in range((v2//d)-2):
l.append(v1+d)
l.append(v2+d)
l.append(v2)
print(sum(l))
else:
if(d!=0 and (v2 % d!=0)):
print(v1*t)
else:
if (d==0):
print(v1*t)
``` | instruction | 0 | 68,544 | 24 | 137,088 |
No | output | 1 | 68,544 | 24 | 137,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
Submitted Solution:
```
a, b = map(int, input().split(' '))
t, d = map(int, input().split(' '))
dist=0
if d == 0:
print(a*t)
else:
while (a-b)+d<=(t-1)*d:
dist += a
a += d
t -= 1
k=(a-b)%d
a -= k
t -= 1
dist += a
while t > 0:
a -= d
t -= 1
dist += a
print(dist)
``` | instruction | 0 | 68,545 | 24 | 137,090 |
No | output | 1 | 68,545 | 24 | 137,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
Input
The first line contains two integers v1 and v2 (1 β€ v1, v2 β€ 100) β the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 β€ t β€ 100) β the time when the car moves along the segment in seconds, d (0 β€ d β€ 10) β the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
* the speed in the first second equals v1,
* the speed in the last second equals v2,
* the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Output
Print the maximum possible length of the path segment in meters.
Examples
Input
5 6
4 2
Output
26
Input
10 10
10 0
Output
100
Note
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26 meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
Submitted Solution:
```
v1,v2 = map(int,input().split())
t,d = map(int,input().split())
if v1>v2:
z = v1
v1 = v2
v2 = z
ans = v1
cur = v1
for i in range(2,t+1):
temp = (t-i)*d
f = 0
for j in range(d,-1,-1):
if abs((cur+j)-v2)<=temp:
f=1
cur += j
ans+=cur
break
if f==0:
for j in range(i,t+1):
cur-=d
if cur>=v2:
ans+=cur
else:
ans+=v2
break
print(ans)
``` | instruction | 0 | 68,546 | 24 | 137,092 |
No | output | 1 | 68,546 | 24 | 137,093 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image> | instruction | 0 | 68,649 | 24 | 137,298 |
Tags: *special, graphs, implementation
Correct Solution:
```
n = int(input())
names_set = set()
s = {}
names = []
arr = [[] for j in range(n)]
g = 0
for i in range(n):
el = input()
names_set.add(el.split()[0])
ind = 0
if s.get(el) is None:
s[el] = g
names.append(el)
g += 1
k = int(input())
for j in range(k):
el2 = input()
if s.get(el2) is None:
s[el2] = g
names.append(el2)
g += 1
arr[s.get(el)].append(s.get(el2))
if i != n - 1:
r = input()
res = []
q = []
q.append(0)
sp = {}
while len(q):
el = q[0]
del q[0]
name, vers = names[el].split()
if name in names_set:
try:
sp[name] = max(int(vers), sp[name])
except:
sp[name] = int(vers)
if not len(q):
for i in sp:
names_set.remove(i)
new_el = []
new_el.append(i)
new_el.append(sp[i])
res.append(new_el[:])
ind = s[str(new_el[0]) + " " + str(new_el[1])]
for j in range(len(arr[ind])):
p = arr[ind][j]
q.append(p)
sp = {}
res = res[1:]
res.sort()
print(len(res))
for i in res:
print(i[0], i[1])
``` | output | 1 | 68,649 | 24 | 137,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image> | instruction | 0 | 68,650 | 24 | 137,300 |
Tags: *special, graphs, implementation
Correct Solution:
```
from queue import Queue
def main():
n = int(input())
d = {}
res = {}
start = None
for i in range(n):
project, version = input().split()
version = int(version)
if i == 0:
start = project, version
k = int(input())
if project not in d:
d[project] = {}
if version not in d[project]:
d[project][version] = []
for j in range(k):
p, v = input().split()
v = int(v)
d[project][version].append((p, v))
if i != n-1:
input()
q = Queue()
q.put(start)
k = 1
while not q.empty():
append = {}
for i in range(k):
s_p, s_v = q.get()
for (p, v) in d[s_p][s_v]:
if p == start[0]:
continue
if p in res:
continue
if p not in append or append[p] < v:
append[p] = v
k = len(append)
for p in append:
res[p] = append[p]
q.put((p, append[p]))
ans = []
for p in res:
v = res[p]
ans.append((p, v))
ans = sorted(ans, key= lambda z: z[0])
print(len(ans))
for (p, v) in ans:
print(p, v)
main()
``` | output | 1 | 68,650 | 24 | 137,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image> | instruction | 0 | 68,651 | 24 | 137,302 |
Tags: *special, graphs, implementation
Correct Solution:
```
# f = open("input.txt")
# def readline():
# return f.readline().strip()
def readline():
return input()
def read_project():
project = readline().split(" ")
project[1] = int(project[1])
dep_num = int(readline())
deps = {}
for _ in range(0, dep_num):
(proj, version) = readline().split(" ")
version = int(version)
if proj in deps:
deps[proj] = max(deps[proj], version)
else:
deps[proj] = version
return (tuple(project), deps)
def make_like_buck():
projects_num = int(readline())
projects = {}
polikarp_proj = None
for ind in range(0, projects_num):
(proj, deps) = read_project()
if ind == 0:
polikarp_proj = proj
projects[proj] = deps
if ind != projects_num - 1:
readline()
RESULT_DEPS = {polikarp_proj[0]: polikarp_proj[1]}
curr_proj = [polikarp_proj]
while len(curr_proj) > 0:
curr_deps = {}
for proj in curr_proj:
new_deps = projects[proj]
for proj in new_deps:
if proj in RESULT_DEPS:
continue
if proj in curr_deps:
curr_deps[proj] = max(curr_deps[proj], new_deps[proj])
else:
curr_deps[proj] = new_deps[proj]
RESULT_DEPS = {**RESULT_DEPS, **curr_deps}
curr_proj = list(curr_deps.items())
RESULT_DEPS.pop(polikarp_proj[0])
print(len(RESULT_DEPS))
items = ["%s %s" % x for x in sorted(list(RESULT_DEPS.items()))]
print("\n".join(items))
make_like_buck()
``` | output | 1 | 68,651 | 24 | 137,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image> | instruction | 0 | 68,652 | 24 | 137,304 |
Tags: *special, graphs, implementation
Correct Solution:
```
def read_pack():
name, ver = input().strip().split()
return (name, int(ver))
n = int(input())
deps = dict()
for i in range(n):
pack = read_pack()
if not i:
root = pack
dep_n = int(input())
deps[pack] = [read_pack() for _ in range(dep_n)]
if i != n - 1:
input()
queue = [(root, 0)]
taken = {root[0]: (root[1], 0)}
for pack, level in queue:
if pack[0] in taken and pack[1] != taken[pack[0]][0]:
continue
for dep in deps[pack]:
if dep[0] not in taken or taken[dep[0]][1] == level + 1 and taken[dep[0]][0] < dep[1]:
taken[dep[0]] = (dep[1], level + 1)
queue.append((dep, level + 1))
del taken[root[0]]
print(len(taken))
for d in sorted(taken):
print(d, taken[d][0])
``` | output | 1 | 68,652 | 24 | 137,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image> | instruction | 0 | 68,653 | 24 | 137,306 |
Tags: *special, graphs, implementation
Correct Solution:
```
def read_pack():
name, ver = input().strip().split()
return (name, int(ver))
n = int(input())
deps = dict()
for i in range(n):
pack = read_pack()
if not i:
root = pack
dep_n = int(input())
deps[pack] = [read_pack() for _ in range(dep_n)]
if i != n - 1:
input()
queue = [(root, 0)]
taken = {root[0]: (root[1], 0)}
for pack, level in queue:
# pack_deps = sorted(deps[pack], key=lambda x: x[1], reverse=True)
for dep in deps[pack]:
if dep[0] not in taken:
taken[dep[0]] = (dep[1], level + 1)
queue.append((dep, level + 1))
elif taken[dep[0]][1] == level + 1 and taken[dep[0]][0] < dep[1]:
index = queue.index(((dep[0], taken[dep[0]][0]), level + 1))
taken[dep[0]] = (dep[1], level + 1)
queue[index] = (dep, level + 1)
del taken[root[0]]
print(len(taken))
for d in sorted(taken):
print(d, taken[d][0])
``` | output | 1 | 68,653 | 24 | 137,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image> | instruction | 0 | 68,654 | 24 | 137,308 |
Tags: *special, graphs, implementation
Correct Solution:
```
from sys import stdin, stdout
def read_pack():
name, ver = stdin.readline().split()
return (name, int(ver))
n = int(input())
deps = dict()
for i in range(n):
pack = read_pack()
if not i:
root = pack
dep_n = int(input())
deps[pack] = [read_pack() for _ in range(dep_n)]
if i != n - 1:
input()
queue = [(root, 0)]
taken = {root[0]: (root[1], 0)}
for pack, level in queue:
# pack_deps = sorted(deps[pack], key=lambda x: x[1], reverse=True)
for dep in deps[pack]:
if dep[0] not in taken:
taken[dep[0]] = (dep[1], level + 1)
queue.append((dep, level + 1))
elif taken[dep[0]][1] == level + 1 and taken[dep[0]][0] < dep[1]:
index = queue.index(((dep[0], taken[dep[0]][0]), level + 1))
taken[dep[0]] = (dep[1], level + 1)
queue[index] = (dep, level + 1)
del taken[root[0]]
print(len(taken))
for d in sorted(taken):
print(d, taken[d][0])
``` | output | 1 | 68,654 | 24 | 137,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image> | instruction | 0 | 68,655 | 24 | 137,310 |
Tags: *special, graphs, implementation
Correct Solution:
```
def scan_project():
name, version_str = input().split()
return (name, int(version_str))
n = int(input())
projects, depends = [], {}
for i in range(n):
if i > 0:
input()
project = scan_project()
projects.append(project)
depends[project] = [scan_project() for j in range(int(input()))]
root_name, root_version = projects[0]
level_depends = [projects[0]]
all_depends = {root_name : root_version}
while level_depends:
level_depends = list(sorted(set([(name, version) for proj in level_depends
for name, version in depends[proj] if name not in all_depends])))
for name, version in sorted(level_depends):
all_depends[name] = version
level_depends = [(name, version) for name, version in level_depends
if all_depends[name] == version]
all_depends.pop(root_name)
print(len(all_depends))
for name in sorted(all_depends):
print(name, all_depends[name])
``` | output | 1 | 68,655 | 24 | 137,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image> | instruction | 0 | 68,656 | 24 | 137,312 |
Tags: *special, graphs, implementation
Correct Solution:
```
def read_pack():
name, ver = input().split()
return (name, int(ver))
n = int(input())
deps = dict()
for i in range(n):
pack = read_pack()
if not i:
root = pack
dep_n = int(input())
deps[pack] = [read_pack() for _ in range(dep_n)]
if i != n - 1:
input()
queue = [(root, 0)]
taken = {root[0]: (root[1], 0)}
for pack, level in queue:
# pack_deps = sorted(deps[pack], key=lambda x: x[1], reverse=True)
for dep in deps[pack]:
if dep[0] not in taken:
taken[dep[0]] = (dep[1], level + 1)
queue.append((dep, level + 1))
elif taken[dep[0]][1] == level + 1 and taken[dep[0]][0] < dep[1]:
index = queue.index(((dep[0], taken[dep[0]][0]), level + 1))
taken[dep[0]] = (dep[1], level + 1)
queue[index] = (dep, level + 1)
del taken[root[0]]
print(len(taken))
for d in sorted(taken):
print(d, taken[d][0])
``` | output | 1 | 68,656 | 24 | 137,313 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image>
Submitted Solution:
```
from collections import defaultdict
def rp():
s = input().split()
return (s[0], int(s[1]))
ps = {}
n = int(input())
for i in range(n):
p = rp()
d = []
for _ in range(int(input())):
d += [rp()]
ps[p] = d
if i != n - 1:
input()
root = list(ps.keys())[0]
q = [(root, 0)]
u = {root[0]: (root[1], 0)}
for i, l in q:
isp = i
if isp[0] in u and isp[1] != u[isp[0]][0]:
continue
for p in ps[i]:
psp = p
if psp[0] not in u or u[psp[0]][1] == l + 1 and u[psp[0]][0] < psp[1]:
u[psp[0]] = (psp[1], l + 1)
q.append((psp, l + 1))
del u[root[0]]
print(len(u))
for i in sorted(u):
print(i, u[i][0])
``` | instruction | 0 | 68,657 | 24 | 137,314 |
Yes | output | 1 | 68,657 | 24 | 137,315 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image>
Submitted Solution:
```
# python3
def read_project_name():
name, version = input().split()
return (name, int(version))
def read_project():
project = read_project_name()
deps_size = int(input())
deps = [read_project_name() for __ in range(deps_size)]
return (project, deps)
def main():
dependencies = dict()
n = int(input()) - 1
main_project, deps = read_project()
dependencies[main_project] = deps
queue = (main_project,)
for __ in range(n):
input()
project, deps = read_project()
dependencies[project] = deps
selected = dict()
while queue:
new_selected = dict()
for (name, version) in queue:
if name not in selected:
old = new_selected.get(name, 0)
new_selected[name] = max(old, version)
queue = list()
for project in new_selected.items():
queue.extend(dependencies[project])
selected.update(new_selected)
del selected[main_project[0]]
print(len(selected))
for (name, version) in sorted(selected.items()):
print(name, version)
main()
``` | instruction | 0 | 68,658 | 24 | 137,316 |
Yes | output | 1 | 68,658 | 24 | 137,317 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image>
Submitted Solution:
```
from queue import Queue
def BFS(v):
global ans
stack = Queue()
stack.put(v)
used = {p[0]: False for p in graph.keys()}
used[v[0]] = True
while True:
set_ = dict()
st = Queue()
while not stack.empty():
v = stack.get()
for u in graph[v]:
n_ = u[0]
if not used[n_]:
try:
set_[n_] = max(set_[n_], u[1])
except KeyError:
set_[n_] = u[1]
for el in set_.items():
ans.append(el)
st.put(el)
used[el[0]] = True
if st.empty():
break
while not st.empty():
stack.put(st.get())
n = int(input())
graph = dict()
for i in range(n):
name, ver = input().split()
k = int(input())
temp = set()
for j in range(k):
name_, ver_ = input().split()
temp.add((name_, int(ver_)))
tup = (name, int(ver))
graph[tup] = temp
if i == 0:
PP = tup
if i + 1 != n:
input()
ans = list()
BFS(PP)
print(len(ans))
print('\n'.join([' '.join(map(str, el)) for el in sorted(ans, key=lambda x: x[0])]))
``` | instruction | 0 | 68,659 | 24 | 137,318 |
Yes | output | 1 | 68,659 | 24 | 137,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image>
Submitted Solution:
```
from collections import defaultdict
def rp():
s = input().split()
return (s[0], int(s[1]))
ps = {}
n = int(input())
for i in range(n):
p = rp()
d = []
for _ in range(int(input())):
d += [rp()]
ps[p] = d
if i != n - 1:
input()
root = list(ps.keys())[0]
q = [(root, 0)]
u = {root[0]: (root[1], 0)}
for i, l in q:
isp = i
if isp[0] in u and isp[1] != u[isp[0]][0]:
continue
for p in ps[i]:
psp = p
if psp[0] not in u or u[psp[0]][1] == l + 1 and u[psp[0]][0] < psp[1]:
u[psp[0]] = (psp[1], l + 1)
q.append((psp, l + 1))
del u[root[0]]
print(len(u))
for i in sorted(u):
print(i, u[i][0])
# Made By Mostafa_Khaled
``` | instruction | 0 | 68,660 | 24 | 137,320 |
Yes | output | 1 | 68,660 | 24 | 137,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image>
Submitted Solution:
```
def e(r, p):
r1 = []
if len(r) > 0:
for i in sorted(r, key = lambda x: (x[0], -x[1])):
u = [j[0] for j in zav]
if not i[0] in u:
zav.append(i)
ind.append(p)
r1.append(i)
elif ind[u.index(i[0])] > p or (ind[u.index(i[0])] == p and zav[u.index(i[0])][1] < i[1]):
del zav[u.index(i[0])]
del ind[u.index(i[0])]
if i in [k[0] for k in r1]:
del r1[u.index(i[0])]
zav.append(i)
r1.append(i)
ind.append(p)
for i in r1:
e(s[i], p + 1)
s = {}
x, y = '', 0
n = int(input())
for i in range(n):
l = input().split(' ')
a, b = ' '.join(l[:-1]), int(l[-1])
if i == 0:
x, y = a, b
s[(a, b)] = []
for j in range(int(input())):
l1 = input().split()
a1, b1 = ' '.join(l1[:-1]), int(l1[-1])
s[(a, b)] = s[(a, b)] + [(a1, b1)]
if i < n - 1:
input()
zav = [(x, y)]
ind = [0]
if n > 0:
e(s[(x, y)], 1)
if (x, y) in zav:
del zav[zav.index((x, y))]
print(len(zav))
if len(zav) > 0:
print('\n'.join(' '.join(str(j) for j in i) for i in sorted(zav, key = lambda x: x[0])))
``` | instruction | 0 | 68,661 | 24 | 137,322 |
No | output | 1 | 68,661 | 24 | 137,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image>
Submitted Solution:
```
q=int(input())
a=[]
reg={}
for i in range(1,q):
k=[]
d={}
z,x=input().split()
reg[z+' '+x]=i-1
k.append({z:int(x)})
w=int(input())
for j in range(0,w):
z,x=input().split()
d[z]=max(d.get(z,x),x)
k.append(d)
a.append(k)
input()
k=[]
d={}
z,x=input().split()
reg[z+' '+x]=q-1
k.append({z:int(x)})
w=int(input())
for j in range(0,w):
z,x=input().split()
d[z]=max(d.get(z,x),x)
k.append(d)
a.append(k)
#Π²Π²ΠΎΠ΄ Π·Π°ΠΊΠΎΠ½ΡΠΈΠ»ΠΈ, ΡΡΠ°!
# reg,a,q ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π½Ρ
ans = []
zan = [i for i in a[0][0]]
def f(ad):
ad1={}
for i in ad:
if i not in zan:
ad1[i]=ad[i]
kk={}
for i in ad1:
zan.append(i)
k=i+' '+str(ad1[i])
if k in reg:
ans.append(k)
kl = a[reg[k]][1]
for j in kl:
kk[j]=max(kk.get(j,kl[j]),kl[j])
if kk=={}:
return
f(kk)
f(a[0][1])
print(len(ans))
ans.sort()
for i in ans:
print(i)
``` | instruction | 0 | 68,662 | 24 | 137,324 |
No | output | 1 | 68,662 | 24 | 137,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image>
Submitted Solution:
```
count = int(input())
graph={}
def get_new_elem():
name, version = input().split()
node = (name, int(version))
elem = []
for i in range(int(input())):
name, version = input().split()
elem.append((name, int(version)))
return node, elem
main, melem = get_new_elem()
graph={}
graph[main] = melem
for i in range(count-1):
input()
node, elem = get_new_elem()
graph[node]=elem
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if start not in graph:
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
def form_result(old_result, new_elem):
name, count, version = new_elem[-1][0], len(new_elem), new_elem[-1][1]
prevs = new_elem[:-1]
for prev in prevs:
prev_name, prev_version = prev
if prev_name in old_result:
_, old_version = old_result[prev_name]
if old_version != prev_version:
return old_result
if name in old_result:
old_count, old_version = old_result[name]
if count < old_count:
old_result[name] = (count, version)
elif count == old_count:
if version > old_version:
old_result[name] = (count, version)
else:
old_result[name] = (count, version)
return old_result
result = {}
graph_paths = []
for elem in graph:
paths = find_all_paths(graph, main, elem)
if len(paths) != 1:
if paths:
for path in paths:
graph_paths.append(path)
else:
graph_paths.append(paths[0])
for path in sorted(graph_paths, key=len):
result = form_result(result, path)
del result[main[0]]
print(len(result))
for res in result:
print(res, result[res][1])
``` | instruction | 0 | 68,663 | 24 | 137,326 |
No | output | 1 | 68,663 | 24 | 137,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
* Polycarp's project is chosen;
* Polycarp's project depends (directly or indirectly) on all other projects in the set;
* no two projects share the name;
* for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.
Input
The first line contains an only integer n (1 β€ n β€ 1 000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies.
Output
Output all Polycarp's project's dependencies in lexicographical order.
Examples
Input
4
a 3
2
b 1
c 1
Β
b 2
0
Β
b 1
1
b 2
Β
c 1
1
b 2
Output
2
b 1
c 1
Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
Β
commons 2
0
Β
mashadb 3
0
Β
webfrmk 6
2
mashadb 3
commons 2
Β
extra 4
1
extra 3
Β
extra 3
0
Β
extra 1
0
Β
mashadb 1
1
extra 3
Β
mashadb 2
1
extra 1
Output
4
commons 2
extra 1
mashadb 2
webfrmk 6
Input
3
abc 1
2
abc 3
cba 2
abc 3
0
cba 2
0
Output
1
cba 2
Note
The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
<image>
The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β».
<image>
Submitted Solution:
```
def respecc(data=None):
# WE RESPECC BUT WE ALSO CALLBACC
# For unittests
if data is None:
gen_data = input
else:
data_iter = iter(data.split('\n'))
gen_data = data_iter.__next__
# ΠΠ²ΠΎΠ΄
projects = {}
n_projects = int(gen_data())
root = None
for i in range(n_projects):
name = gen_data()
if not root:
root = name
num_dep = int(gen_data())
deps = []
for _ in range(num_dep):
deps.append(gen_data())
projects[name] = deps
if i < n_projects-1:
gen_data()
graph = Graph(root, projects)
result = sorted(graph.final_list)
x = str(len(result))+'\n'+'\n'.join(result)
return x
class Graph:
def __init__(self, root, deps):
self.root = root
children = deps.pop(root)
self.nodes = {root: {'children': children, 'distance': 0}}
for c in deps:
ch = deps.get(c, [])
self.nodes[c] = {'children': ch, 'distance': 9999}
self.walk(self.nodes[self.root])
r_n, r_v = split_dep(root)
self.rel_list = {r_n: {'v': r_v, 'distance':0}}
self.pop_rels(self.nodes[self.root])
self.final_list = set()
self.final_walk(self.nodes[self.root])
def walk(self, node):
for c in node['children']:
child = self.nodes[c]
child['distance'] = min(node['distance']+1, child['distance'])
self.walk(child)
def pop_rels(self, node):
for c in node['children']:
added = False
if c not in self.nodes:
continue
child = self.nodes[c]
name, ver = split_dep(c)
if name in self.rel_list:
other = self.rel_list[name]
if ver == other['v']:
pass
elif child['distance'] < other['distance'] or (child['distance'] == other['distance'] and ver > other['v']):
iconic_name = name + ' ' + str(self.rel_list[name]['v'])
self.rel_list[name]['v'] = ver
self.rel_list[name]['distance'] = child['distance']
self.deep_deletion(iconic_name)
added = True
else:
if c in self.nodes:
self.nodes.pop(c)
else:
self.rel_list[name] = {'v': ver, 'distance': child['distance']}
added = True
if added:
self.pop_rels(child)
def deep_deletion(self, node_name):
node = self.nodes.get(node_name)
if node:
for c in node['children']:
self.deep_deletion(c)
n, v = split_dep(node_name)
if self.rel_list.get(n, {}).get('v') == v:
self.rel_list.pop(n)
self.nodes.pop(node_name)
def final_walk(self, node):
for c in node['children']:
if c in self.nodes:
self.final_list.update([c])
self.final_walk(self.nodes[c])
def split_dep(dep):
s = dep.split(' ')
s[1] = int(s[1])
return s
def dep_name(dep):
return split_dep(dep)[0]
def dep_v(dep):
return split_dep(dep)[1]
if __name__ == '__main__':
print(respecc())
``` | instruction | 0 | 68,664 | 24 | 137,328 |
No | output | 1 | 68,664 | 24 | 137,329 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z | instruction | 0 | 69,856 | 24 | 139,712 |
Tags: implementation
Correct Solution:
```
#Codeforce 1095A
s=int(input())
str1=input()
str2=""
k=int((s*2)**0.5)
for i in range(k):
str2 += str1[i*(i+1)//2]
print(str2)
``` | output | 1 | 69,856 | 24 | 139,713 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z | instruction | 0 | 69,857 | 24 | 139,714 |
Tags: implementation
Correct Solution:
```
n=int(input())
s=input()
a=''
c=1
i=1
while c <= len(s):
a+=s[c-1]
i+=1
c=(i*(i+1))//2
print(a)
``` | output | 1 | 69,857 | 24 | 139,715 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z | instruction | 0 | 69,858 | 24 | 139,716 |
Tags: implementation
Correct Solution:
```
n = input();a =0
if len(n)%2==0:x = n[-1];n = n[0:len(n)-1];a=1
z = len(n)//2
s = n[z]
for i in range(1,z+1):
s+=n[z+i]
s+=n[z-i]
if a ==1:print(s+x)
else:print(s)
``` | output | 1 | 69,858 | 24 | 139,717 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z | instruction | 0 | 69,859 | 24 | 139,718 |
Tags: implementation
Correct Solution:
```
n = int(input())
s = input()
i = 0
count = 0
while i<n :
print(s[i], end = '')
count+=1
i+=count
``` | output | 1 | 69,859 | 24 | 139,719 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z | instruction | 0 | 69,860 | 24 | 139,720 |
Tags: implementation
Correct Solution:
```
n=int(input())
s=input()
a=1
i=0
while i!=n:
print(s[i],end='')
i+=a
a+=1
``` | output | 1 | 69,860 | 24 | 139,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z | instruction | 0 | 69,861 | 24 | 139,722 |
Tags: implementation
Correct Solution:
```
n=int(input())
s=input()
x=((1+8*n)**0.5-1)/2
#print(int(x))
p=''
k=0
for i in range(1,int(x)+1):
p=p+s[k]
k=k+i
print(p)
``` | output | 1 | 69,861 | 24 | 139,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z | instruction | 0 | 69,862 | 24 | 139,724 |
Tags: implementation
Correct Solution:
```
import sys
import math
n=int(input())
s=sys.stdin.readline().strip()
res=[]
count=1
L=(math.sqrt(1+8*n)-1)//2
i=0
while count<=L:
res.append(s[i])
i+=count
count+=1
es="".join(res)
print(es)
``` | output | 1 | 69,862 | 24 | 139,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z | instruction | 0 | 69,863 | 24 | 139,726 |
Tags: implementation
Correct Solution:
```
t = int(input())
s = input()
i = 0;
j = 1;
while i < t:
print(s[i], end = '')
i += j
j += 1
``` | output | 1 | 69,863 | 24 | 139,727 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z
Submitted Solution:
```
in1 = int(input())
in2 = input()
i = 0
j = 1
s = ''
while i < in1:
s = s + in2[i]
j = j + 1
i = i + j
print(s)
``` | instruction | 0 | 69,864 | 24 | 139,728 |
Yes | output | 1 | 69,864 | 24 | 139,729 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z
Submitted Solution:
```
def A():
n = int(input())
s = input()
triang = lambda x: int(x*(x+1)/2)
r = ""
i = 0
while(triang(i)<n):
r+=s[triang(i)]
i+=1
print(r)
A()
``` | instruction | 0 | 69,865 | 24 | 139,730 |
Yes | output | 1 | 69,865 | 24 | 139,731 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z
Submitted Solution:
```
a=int(input())
s=input()
i=0
k=''
b=1
while(i<a):
k+=s[i]
i=i+b
b=b+1
print(k)
``` | instruction | 0 | 69,866 | 24 | 139,732 |
Yes | output | 1 | 69,866 | 24 | 139,733 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z
Submitted Solution:
```
n = int(input())
a = input()
i = 1
s = 1
new = []
while s <= n:
new.append(a[s-1])
i += 1
s += i
str = ''.join(new)
print(str)
``` | instruction | 0 | 69,867 | 24 | 139,734 |
Yes | output | 1 | 69,867 | 24 | 139,735 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z
Submitted Solution:
```
'''
Amirhossein Alimirzaei
Telegram : @HajLorenzo
Instagram : amirhossein_alimirzaei
University of Bojnourd
'''
n=input()
tmp=""
ln=len(n)
i=(ln//2)-1
switcher=True
while(len(tmp)!=ln):
tmp+=n[i]
if(switcher):
i+=len(tmp)
switcher=False
else:
i-=len(tmp)
switcher=True
#print("tmp : ",tmp)
print(tmp)
``` | instruction | 0 | 69,868 | 24 | 139,736 |
No | output | 1 | 69,868 | 24 | 139,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Polycarp loves ciphers. He has invented his own cipher called repeating.
Repeating cipher is used for strings. To encrypt the string s=s_{1}s_{2} ... s_{m} (1 β€ m β€ 10), Polycarp uses the following algorithm:
* he writes down s_1 ones,
* he writes down s_2 twice,
* he writes down s_3 three times,
* ...
* he writes down s_m m times.
For example, if s="bab" the process is: "b" β "baa" β "baabbb". So the encrypted s="bab" is "baabbb".
Given string t β the result of encryption of some string s. Your task is to decrypt it, i. e. find the string s.
Input
The first line contains integer n (1 β€ n β€ 55) β the length of the encrypted string. The second line of the input contains t β the result of encryption of some string s. It contains only lowercase Latin letters. The length of t is exactly n.
It is guaranteed that the answer to the test exists.
Output
Print such string s that after encryption it equals t.
Examples
Input
6
baabbb
Output
bab
Input
10
ooopppssss
Output
oops
Input
1
z
Output
z
Submitted Solution:
```
n = int(input())
s = input()
k = 0
if n==1:
print(s[0])
elif n > 1:
print(s[0],end='')
for i in range(n):
k+=i
if k < n:
print(s[k])
``` | instruction | 0 | 69,869 | 24 | 139,738 |
No | output | 1 | 69,869 | 24 | 139,739 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.