message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.
We can simulate the movement (comings and goings) of the cars as follow:
* An entry of a car is represented by its number.
* An exit of a car is represented by 0
For example, a sequence
1
6
0
8
10
demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.
Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.
Input
car number
car number or 0
car number or 0
.
.
.
car number or 0
The number of input lines is less than or equal to 100.
Output
For each 0, print the car number.
Example
Input
1
6
0
8
10
0
0
0
Output
6
10
8
1
Submitted Solution:
```
buf = []
while True:
try:
car = int(input())
except:
break
if car:
buf.append(car)
else:
print(buf.pop())
``` | instruction | 0 | 106,940 | 1 | 213,880 |
Yes | output | 1 | 106,940 | 1 | 213,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.
We can simulate the movement (comings and goings) of the cars as follow:
* An entry of a car is represented by its number.
* An exit of a car is represented by 0
For example, a sequence
1
6
0
8
10
demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.
Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.
Input
car number
car number or 0
car number or 0
.
.
.
car number or 0
The number of input lines is less than or equal to 100.
Output
For each 0, print the car number.
Example
Input
1
6
0
8
10
0
0
0
Output
6
10
8
1
Submitted Solution:
```
import sys
s=[input()]
while(True):
n=input()
if n=="0":
print(s.pop())
elif n=="":
sys.exit()
else:
s.append(n)
``` | instruction | 0 | 106,941 | 1 | 213,882 |
No | output | 1 | 106,941 | 1 | 213,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.
We can simulate the movement (comings and goings) of the cars as follow:
* An entry of a car is represented by its number.
* An exit of a car is represented by 0
For example, a sequence
1
6
0
8
10
demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.
Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.
Input
car number
car number or 0
car number or 0
.
.
.
car number or 0
The number of input lines is less than or equal to 100.
Output
For each 0, print the car number.
Example
Input
1
6
0
8
10
0
0
0
Output
6
10
8
1
Submitted Solution:
```
import sys
cars = []
for line in sys.stdin:
car = int(line)
if car:
cars.append(num)
else:
print(cars.pop())
``` | instruction | 0 | 106,942 | 1 | 213,884 |
No | output | 1 | 106,942 | 1 | 213,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.
We can simulate the movement (comings and goings) of the cars as follow:
* An entry of a car is represented by its number.
* An exit of a car is represented by 0
For example, a sequence
1
6
0
8
10
demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.
Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.
Input
car number
car number or 0
car number or 0
.
.
.
car number or 0
The number of input lines is less than or equal to 100.
Output
For each 0, print the car number.
Example
Input
1
6
0
8
10
0
0
0
Output
6
10
8
1
Submitted Solution:
```
garage = []
while True:
num = int(input())
if num == 0:
print(garage.pop())
else:
garage.append(num)
``` | instruction | 0 | 106,943 | 1 | 213,886 |
No | output | 1 | 106,943 | 1 | 213,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.
We can simulate the movement (comings and goings) of the cars as follow:
* An entry of a car is represented by its number.
* An exit of a car is represented by 0
For example, a sequence
1
6
0
8
10
demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.
Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.
Input
car number
car number or 0
car number or 0
.
.
.
car number or 0
The number of input lines is less than or equal to 100.
Output
For each 0, print the car number.
Example
Input
1
6
0
8
10
0
0
0
Output
6
10
8
1
Submitted Solution:
```
stack = []
stack.append(int(input()))
while stack:
inp = int(input())
if inp == 0:
a = stack[-1]
del stack[-1]
print(a)
else:
stack.append(inp)
``` | instruction | 0 | 106,944 | 1 | 213,888 |
No | output | 1 | 106,944 | 1 | 213,889 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9 | instruction | 0 | 107,407 | 1 | 214,814 |
Tags: dfs and similar, graphs, greedy, shortest paths, trees
Correct Solution:
```
n = int(input())
g = [[] for _ in range(n)]
s = 0
for i in range(1,n):
u,v,w = map(int,input().split())
u -= 1
v -= 1
g[u].append([v,w])
g[v].append([u,w])
s += w
def dfs (u,f,x):
ret = x
for v,w in g[u]:
if f != v:
ret = max(ret,dfs(v,u,w+x))
return ret
print(2*s-dfs(0,-1,0))
``` | output | 1 | 107,407 | 1 | 214,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9 | instruction | 0 | 107,408 | 1 | 214,816 |
Tags: dfs and similar, graphs, greedy, shortest paths, trees
Correct Solution:
```
n = int(input())
matrix = [[] * n for i in range(n)]
conn = dict()
def dfs(el, el2):
suma = 0
for element in matrix[el]:
if element[0] != el2:
suma = max(suma, element[1]+dfs(element[0],el))
return suma
suma=0
for i in range(n-1):
line = input()
line = line.split(' ')
matrix[int(line[0])-1].append((int(line[1])-1,int(line[2])))
matrix[int(line[1])-1].append((int(line[0])-1,int(line[2])))
suma += 2*int(line[2])
print(suma-dfs(0,-1))
``` | output | 1 | 107,408 | 1 | 214,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9 | instruction | 0 | 107,409 | 1 | 214,818 |
Tags: dfs and similar, graphs, greedy, shortest paths, trees
Correct Solution:
```
import sys
def mainFunction():
n, = rv()
edges = [list() for _ in range(n+1)]
for i in range(n - 1):
x, y, w, = rv()
edges[x].append((y, w))
edges[y].append((x, w))
res = dfs(1, -1, edges)
print(res[0] * 2 - res[1])
def dfs(cur, prev, edges):
total, single = 0, 0
for nxt, weight in edges[cur]:
if nxt != prev:
temp = dfs(nxt, cur, edges)
total += temp[0] + weight
single = max(single, temp[1] + weight)
return (total, single)
def prt(l): return print(' '.join(l))
def rv(): return map(int, input().split())
def rl(n): return [list(map(int, input().split())) for _ in range(n)]
#-----------------main method------------------------------------------
mainFunction()
``` | output | 1 | 107,409 | 1 | 214,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9 | instruction | 0 | 107,410 | 1 | 214,820 |
Tags: dfs and similar, graphs, greedy, shortest paths, trees
Correct Solution:
```
from os import sys
num = int(input())
vertices=[]
[vertices.append([]) for c in range(num+1)]
weightUnder=[-1]*(num+1)
visited = [-1]*(num+1)
sys.setrecursionlimit(100000)
def calculateWeight(i):
totalWeightUnder=0
visited[i]=1
for (vert,weight) in vertices[i]:
if visited[vert]==-1:
totalWeightUnder+=weight+calculateWeight(vert)
weightUnder[i]= totalWeightUnder
return totalWeightUnder
def getSolution(i):
visited[i]=1
minValue = 20000000000
for (vert,weight) in vertices[i]:
if (visited[vert]==-1):
newValue = weight+getSolution(vert)
minValue = min((weightUnder[i]-weight-weightUnder[vert])*2+newValue,minValue)
if minValue == 20000000000:
return 0
return minValue
def solve():
global num
for x in range(num-1):
x,y,w = list(map(int, input().split()))
vertices[x].append((y,w))
vertices[y].append((x,w))
calculateWeight(1)
for i in range(num+1):
visited[i]=-1
currentNode = 1
print (getSolution(currentNode))
try:
solve()
except Exception as e:
print (e)
``` | output | 1 | 107,410 | 1 | 214,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9 | instruction | 0 | 107,411 | 1 | 214,822 |
Tags: dfs and similar, graphs, greedy, shortest paths, trees
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
adj = [[] for _ in range(n)]
deg = [100] + [0] * n
for u, v, w in (map(int, input().split()) for _ in range(n - 1)):
adj[u - 1].append((v - 1, w))
adj[v - 1].append((u - 1, w))
deg[u - 1] += 1
deg[v - 1] += 1
total_w = [0] * n
max_w = [0] * n
stack = [i for i in range(1, n) if deg[i] == 1]
while stack:
v = stack.pop()
deg[v] = 0
for dest, w in adj[v]:
if deg[dest] == 0:
continue
total_w[dest] += total_w[v] + w
max_w[dest] = max(max_w[dest], max_w[v] + w)
deg[dest] -= 1
if deg[dest] == 1:
stack.append(dest)
print(total_w[0] * 2 - max_w[0])
``` | output | 1 | 107,411 | 1 | 214,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9 | instruction | 0 | 107,412 | 1 | 214,824 |
Tags: dfs and similar, graphs, greedy, shortest paths, trees
Correct Solution:
```
import sys
sys.setrecursionlimit(1000*1000)
n = int(input())
g = [[] for i in range(n+1)]
dist = [0] * (10**5+1)
r = 10**32
#print(dist)
def init(i,p):
for j in g[i]:
#print(j)
if j[0] == p:
continue
else :
s = 2*j[1]+init(j[0],i)
dist[i] += s
#print(i)
return dist[i]
def dfs(i,p,c):
global r
for j in g[i]:
if j[0] == p:
continue
dfs(j[0],i,c+dist[i]-dist[j[0]]-j[1])
if len(g[i]) == 1 and i != 1:
r = min(r,c)
for i in range(n-1):
x,y,w = map(int,input().split(' '))
g[x].append((y,w))
g[y].append((x,w))
init(1,0)
dfs(1,0,0)
if n == 1 :
print(0)
else :
print(r)
``` | output | 1 | 107,412 | 1 | 214,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9 | instruction | 0 | 107,413 | 1 | 214,826 |
Tags: dfs and similar, graphs, greedy, shortest paths, trees
Correct Solution:
```
n = int(input())
g = [[] for _ in range(n)]
s = 0
for _ in range(n-1):
u, v, w = map(int, input().split())
u -= 1
v -= 1
g[u].append((v, w))
g[v].append((u, w))
s += w
def dfs(u, f,x):
ret = x
for v, w in g[u]:
if f!= v:
ret = max(ret, dfs(v, u, w+x))
return ret
# test
print(2*s - dfs(0, -1, 0))
``` | output | 1 | 107,413 | 1 | 214,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9 | instruction | 0 | 107,414 | 1 | 214,828 |
Tags: dfs and similar, graphs, greedy, shortest paths, trees
Correct Solution:
```
from collections import defaultdict
class EternalVictory():
def __init__(self, n, edges):
self.n = n
self.edge_list = defaultdict(list)
self.edge_sum = 0
for u,v,w in edges:
self.edge_list[u-1].append((v-1, w))
self.edge_list[v-1].append((u-1, w))
self.edge_sum += w
self.distances = [0]*self.n
def populate(self, u, par, dis):
self.distances[u] = dis
for v,w in self.edge_list[u]:
if v != par:
self.populate(v, u, dis+w)
def min_sum(self):
self.populate(0, -1, 0)
max_dis = max(self.distances)
return (2*self.edge_sum)-max_dis
n = int(input())
edges = []
for i in range(n-1):
u,v,w = list(map(int,input().strip(' ').split(' ')))
edges.append((u,v,w))
ev = EternalVictory(n, edges)
print(ev.min_sum())
``` | output | 1 | 107,414 | 1 | 214,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9
Submitted Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from heapq import *
from math import inf
def main():
n=int(input())
tree,su=[[] for _ in range(n+1)],0
for i in range(n-1):
x,y,w=map(int,input().split())
tree[x].append((y,w))
tree[y].append((x,w))
su+=w
d,b=[inf]*(n+1),[]
d[1]=0
heappush(b,(0,1))
while b:
c,ch=heappop(b)
if d[ch]<c:
continue
for i in tree[ch]:
if d[i[0]]>c+i[1]:
d[i[0]]=c+i[1]
heappush(b,(c+i[1],i[0]))
print(2*su-max(d[1:]))
# region fastio
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")
if __name__ == "__main__":
main()
``` | instruction | 0 | 107,415 | 1 | 214,830 |
Yes | output | 1 | 107,415 | 1 | 214,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9
Submitted Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
visit = [0 for i in range(n + 1)]
counting = [[] for i in range(n + 1)]
vertices = [[] for i in range(n + 1)]
for i in range(n - 1):
a, b, w = map(int, stdin.readline().split())
vertices[a].append((b, w))
vertices[b].append((a, w))
def dfs(v, weight, d):
global cnt, depth
depth = max(depth, d)
visit[v] = 1
for u, w in vertices[v]:
if not visit[u]:
cnt[-1] += w
dfs(u, w, d + w)
cnt[-1] += w
cnt = []
ans = 0
visit[1] = 1
depth = 0
for i in range(len(vertices[1])):
cnt.append(0)
dfs(vertices[1][i][0], 0, vertices[1][i][1])
cnt[-1] += vertices[1][i][1] * 2
ans += sum(cnt) - depth
stdout.write(str(ans))
``` | instruction | 0 | 107,416 | 1 | 214,832 |
Yes | output | 1 | 107,416 | 1 | 214,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9
Submitted Solution:
```
from collections import defaultdict
edges=defaultdict(list)
def solve():
sum=0
for _ in range(int(input())-1):
v,u,w=map(int,input().split())
edges[v].append((u,w))
edges[u].append((v,w))
sum+=w
def dfs(cur,per,x):
ret=x
for i,w in edges[cur]:
if i!=per:
ret=max(ret,dfs(i,cur,x+w))
return ret
print(2*sum-dfs(1,-1,0))
solve()
``` | instruction | 0 | 107,417 | 1 | 214,834 |
Yes | output | 1 | 107,417 | 1 | 214,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9
Submitted Solution:
```
s, n = 0, int(input())
p, r = [[] for i in range(n + 1)], [0] * (n + 1)
for i in range(n - 1):
x, y, d = map(int, input().split())
p[x].append((y, d))
p[y].append((x, d))
s += d
t = [x for x in range(1, n + 1) if len(p[x]) == 1]
while t:
y = t.pop()
if y == 1: continue
x, d = p[y][0]
r[x] = max(r[x], d + r[y])
p[x].remove((y, d))
if len(p[x]) == 1: t.append(x)
print(2 * s - r[1])
``` | instruction | 0 | 107,418 | 1 | 214,836 |
Yes | output | 1 | 107,418 | 1 | 214,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9
Submitted Solution:
```
n = int(input())
tree = [[] for _ in range(n+1)]
ans = 10 ** 18
tot = 0
for _ in range(n-1):
u,v,w = map(int, input().split())
tree[u].append((v,w))
tree[v].append((u,w))
tot += 2 * w
def dfs(node, par, curr_val):
global ans
global tot
ans = min(ans, tot - curr_val)
for child in tree[node]:
if child[0] != par:
dfs(child[0], node, curr_val + child[1])
for edge in tree[1]:
dfs(edge[0], 1, edge[1])
print(ans)
``` | instruction | 0 | 107,419 | 1 | 214,838 |
No | output | 1 | 107,419 | 1 | 214,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9
Submitted Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
visit = [0 for i in range(n + 1)]
counting = [[] for i in range(n + 1)]
vertices = [[] for i in range(n + 1)]
for i in range(n - 1):
a, b, w = map(int, stdin.readline().split())
vertices[a].append((b, w))
vertices[b].append((a, w))
def dfs(v, weight):
visit[v] = 1
for u, w in vertices[v]:
if not visit[u]:
value = dfs(u, w)
counting[v].append(value)
return weight + sum(counting[v])
dfs(1, 0)
ans = sorted(counting[1])
stdout.write(str(sum(ans[:-1]) * 2 + ans[-1]))
``` | instruction | 0 | 107,420 | 1 | 214,840 |
No | output | 1 | 107,420 | 1 | 214,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9
Submitted Solution:
```
def main():
n = lie()
g = []
for i in range(n - 1):
g.append(list(liee()))
temp = cs(n, g)
print(temp.find())
class cs():
def __init__(self, n, g):
self.n = n
self.edge = defaultdict(list)
self.sum = 0
self.dp = vector(n + 1)
for u, v, w in g:
self.edge[u].append([v, w])
self.edge[v].append([u, w])
self.sum += w
def dfs(self, ch, par, dis):
self.dp[ch] = dis
for v, w in self.edge[ch]:
if v != par:
self.dfs(v, ch, dis + w)
def find(self):
self.dfs(1, 0, 0)
dis = max(self.dp)
return 2 * self.sum - dis
from sys import *
import inspect
import re
from math import *
import threading
from collections import *
from pprint import pprint as pp
mod = 998244353
MAX = 10**5
def lie():
return int(input())
def liee():
return map(int, input().split())
def array():
return list(map(int, input().split()))
def deb(p):
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
m = re.search(r'\bdeb\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line)
print('%s %s' % (m.group(1), str(p)))
def vec(size, val=0):
vec = [val for i in range(size)]
return vec
def mat(rowNum, colNum, val=0):
mat = []
for i in range(rowNum):
collumn = [val for j in range(colNum)]
mat.append(collumn)
return mat
def dmain():
setrecursionlimit(100000000)
threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()
if __name__ == '__main__':
# main()
dmain()
``` | instruction | 0 | 107,421 | 1 | 214,842 |
No | output | 1 | 107,421 | 1 | 214,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel.
Input
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.
Output
A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Examples
Input
3
1 2 3
2 3 4
Output
7
Input
3
1 2 3
1 3 3
Output
9
Submitted Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
visit = [0 for i in range(n + 1)]
counting = [[] for i in range(n + 1)]
vertices = [[] for i in range(n + 1)]
for i in range(n - 1):
a, b, w = map(int, stdin.readline().split())
vertices[a].append((b, w))
vertices[b].append((a, w))
def dfs(v, weight):
global cnt
visit[v] = 1
for u, w in vertices[v]:
if not visit[u]:
cnt[-1][0] += w
cnt[-1][1] = 0
dfs(u, w)
cnt[-1][0] += w
cnt[-1][1] += weight
cnt = []
ans = 0
visit[1] = 1
for i in range(len(vertices[1])):
cnt.append([0, 0])
dfs(vertices[1][i][0], 0)
cnt[-1][0] -= cnt[-1][1]
cnt[-1][0] += vertices[1][i][1]
cnt[-1][1] += vertices[1][i][1]
ans += cnt[-1][0]
cnt[-1] = cnt[-1][::-1]
cnt.sort()
for i in range(len(cnt) - 1):
ans += cnt[i][0]
stdout.write(str(ans))
``` | instruction | 0 | 107,422 | 1 | 214,844 |
No | output | 1 | 107,422 | 1 | 214,845 |
Provide a correct Python 3 solution for this coding contest problem.
problem
The JOI country has one long enough road running east to west. The royal palace of JOI country is along the road, and the position along the road in JOI country is represented by the integer A. When A = 0, it represents the position of the royal palace. When A> 0, it indicates the position A meters east of the royal palace. When A <0, it represents a position that is -A meters west of the royal palace.
There are N houses along the roads in JOI, and the houses are numbered from 1 to N in order from the west. There are N people in JOI country, and the people are numbered from 1 to N. The people i live in the house i. The position of house i is represented by a non-zero even Ai. A1, ..., AN are all different.
In JOI country, the lack of movement of the people has become a problem in recent years. The King of JOI, who was concerned about the health of the people, ordered all the people to take a walk. When the King gives an order, all the people start walking eastward or westward all at once. The direction in which each nation starts walking is determined by each nation. All people walk at a speed of 1 meter per second when walking.
All the people of JOI love to talk. If you meet other people during your walk, you will stop there and start small talk. The same is true if you meet a people who have already stopped. The people who have stopped once will not start walking again.
There are Q important people in JOI country. The King of JOI wants to know the position of Q important persons T seconds after the order is issued. Create a program to find the position of Q important persons T seconds after the command is issued.
input
The input consists of 1 + N + Q lines.
On the first line, three integers N, T, Q (1 ≤ N ≤ 100000 (= 105), 0 ≤ T ≤ 1018, 1 ≤ Q ≤ 1000, 1 ≤ Q ≤ N) are written separated by blanks. ing. This means that there are N houses in JOI country, and we want to know the position of Q important people T seconds after the king issues the order.
Two integers Ai and Di (-1018 ≤ Ai ≤ 1018, Ai is a non-zero even number, 1 ≤ Di ≤ 2) are written on the i-th line of the following N lines, separated by blanks. Ai is an even number representing the position of house i. For all i (1 ≤ i ≤ N-1), Ai <Ai + 1 is satisfied. Di indicates the direction in which the national i starts walking after the command is issued. When Di = 1, national i starts walking eastward. When Di = 2, national i starts walking westward.
The integer Xi (1 ≤ Xi ≤ N) is written on the i-th line of the following Q lines. This means that the i-th important person lives in the house Xi. For all i (1 ≤ i ≤ Q-1), Xi <Xi + 1 is satisfied.
Of the five input data given, input 1 satisfies N ≤ 100 and T ≤ 10000. In addition, input 2 satisfies N ≤ 5000. Also, at input 3, there is a certain integer M (1 ≤ M ≤ N-1), Di = 1 for all i (1 ≤ i ≤ M), and for all j (M + 1 ≤ j ≤ N). Satisfy Dj = 2. Also, for inputs 1, 2, and 3, the absolute value of the integer given to the input does not exceed 1000000000 (= 109). Note that for inputs 4 and 5, the given integer does not fall within the range of 32-bit signed integers.
output
The output consists of Q lines.
On line i (1 ≤ i ≤ Q), output an integer representing the position of the i-th important person T seconds after the King issued the command. It is guaranteed that this value is an integer from the condition of the problem statement.
Input / output example
Input example 1
5 5 3
-8 1
-4 2
-twenty two
4 2
10 1
1
3
Five
Output example 1
-6
-6
15
Input example 2
7 18 5
-100 1
-56 2
-34 1
-30 1
-22 1
-4 2
18 2
1
3
Four
Five
7
Output example 2
-82
-16
-13
-13
0
Creative Commons License
Information Olympics Japan Committee work "15th Japan Information Olympics JOI 2015/2016 Qualifying Competition Tasks"
Example
Input
5 5 3
-8 1
-4 2
-2 2
4 2
10 1
1
3
5
Output
-6
-6
15 | instruction | 0 | 107,804 | 1 | 215,608 |
"Correct Solution:
```
import sys
readline = sys.stdin.readline
write = sys.stdout.write
N, T, Q = map(int, readline().split())
P = [list(map(int, readline().split())) for i in range(N)]
QS = [P[int(readline())-1] for i in range(Q)]
prv = 1
R = {}
A = []; B = []
for x, t in P:
if prv == t:
if t == 1:
A.append(x)
else:
B.append(x)
else:
if prv == 2:
y = ((A[-1] if A else -10**19) + (B[0] if B else 10**19)) >> 1
for z in A:
R[z] = min(z+T, y)
for z in B:
R[z] = max(z-T, y)
A = [x]; B = []
else:
B.append(x)
prv = t
if A or B:
y = ((A[-1] if A else -10**19) + (B[0] if B else 10**19)) >> 1
for z in A:
R[z] = min(z+T, y)
for z in B:
R[z] = max(z-T, y)
for x in QS:
write("%d\n" % R[x[0]])
``` | output | 1 | 107,804 | 1 | 215,609 |
Provide a correct Python 3 solution for this coding contest problem.
problem
The JOI country has one long enough road running east to west. The royal palace of JOI country is along the road, and the position along the road in JOI country is represented by the integer A. When A = 0, it represents the position of the royal palace. When A> 0, it indicates the position A meters east of the royal palace. When A <0, it represents a position that is -A meters west of the royal palace.
There are N houses along the roads in JOI, and the houses are numbered from 1 to N in order from the west. There are N people in JOI country, and the people are numbered from 1 to N. The people i live in the house i. The position of house i is represented by a non-zero even Ai. A1, ..., AN are all different.
In JOI country, the lack of movement of the people has become a problem in recent years. The King of JOI, who was concerned about the health of the people, ordered all the people to take a walk. When the King gives an order, all the people start walking eastward or westward all at once. The direction in which each nation starts walking is determined by each nation. All people walk at a speed of 1 meter per second when walking.
All the people of JOI love to talk. If you meet other people during your walk, you will stop there and start small talk. The same is true if you meet a people who have already stopped. The people who have stopped once will not start walking again.
There are Q important people in JOI country. The King of JOI wants to know the position of Q important persons T seconds after the order is issued. Create a program to find the position of Q important persons T seconds after the command is issued.
input
The input consists of 1 + N + Q lines.
On the first line, three integers N, T, Q (1 ≤ N ≤ 100000 (= 105), 0 ≤ T ≤ 1018, 1 ≤ Q ≤ 1000, 1 ≤ Q ≤ N) are written separated by blanks. ing. This means that there are N houses in JOI country, and we want to know the position of Q important people T seconds after the king issues the order.
Two integers Ai and Di (-1018 ≤ Ai ≤ 1018, Ai is a non-zero even number, 1 ≤ Di ≤ 2) are written on the i-th line of the following N lines, separated by blanks. Ai is an even number representing the position of house i. For all i (1 ≤ i ≤ N-1), Ai <Ai + 1 is satisfied. Di indicates the direction in which the national i starts walking after the command is issued. When Di = 1, national i starts walking eastward. When Di = 2, national i starts walking westward.
The integer Xi (1 ≤ Xi ≤ N) is written on the i-th line of the following Q lines. This means that the i-th important person lives in the house Xi. For all i (1 ≤ i ≤ Q-1), Xi <Xi + 1 is satisfied.
Of the five input data given, input 1 satisfies N ≤ 100 and T ≤ 10000. In addition, input 2 satisfies N ≤ 5000. Also, at input 3, there is a certain integer M (1 ≤ M ≤ N-1), Di = 1 for all i (1 ≤ i ≤ M), and for all j (M + 1 ≤ j ≤ N). Satisfy Dj = 2. Also, for inputs 1, 2, and 3, the absolute value of the integer given to the input does not exceed 1000000000 (= 109). Note that for inputs 4 and 5, the given integer does not fall within the range of 32-bit signed integers.
output
The output consists of Q lines.
On line i (1 ≤ i ≤ Q), output an integer representing the position of the i-th important person T seconds after the King issued the command. It is guaranteed that this value is an integer from the condition of the problem statement.
Input / output example
Input example 1
5 5 3
-8 1
-4 2
-twenty two
4 2
10 1
1
3
Five
Output example 1
-6
-6
15
Input example 2
7 18 5
-100 1
-56 2
-34 1
-30 1
-22 1
-4 2
18 2
1
3
Four
Five
7
Output example 2
-82
-16
-13
-13
0
Creative Commons License
Information Olympics Japan Committee work "15th Japan Information Olympics JOI 2015/2016 Qualifying Competition Tasks"
Example
Input
5 5 3
-8 1
-4 2
-2 2
4 2
10 1
1
3
5
Output
-6
-6
15 | instruction | 0 | 107,805 | 1 | 215,610 |
"Correct Solution:
```
#D
import bisect
N,T,Q = map(int,input().split())
AD = [list(map(int,input().split())) for i in range(N)]
X = [int(input()) for i in range(Q)]
wall = []
for i in range(N-1):
a1,d1 = AD[i]
a2,d2 = AD[i+1]
if d1 == 1 and d2 == 2:
if T > (a2-a1)//2:
wall.append((a1+a2)//2)
wlen = len(wall)
for x in X:
ind = x-1
a,d = AD[ind]
if d == 1:
bind = bisect.bisect_left(wall,a)
if bind == wlen:
print(a+T)
else:
wa = wall[bind]
if wa <= a+T:
print(wa)
else:
print(a+T)
else:
bind = bisect.bisect_left(wall,a)-1
if bind == -1:
print(a-T)
else:
wa = wall[bind]
if wa >= a-T:
print(wa)
else:
print(a-T)
``` | output | 1 | 107,805 | 1 | 215,611 |
Provide a correct Python 3 solution for this coding contest problem.
problem
The JOI country has one long enough road running east to west. The royal palace of JOI country is along the road, and the position along the road in JOI country is represented by the integer A. When A = 0, it represents the position of the royal palace. When A> 0, it indicates the position A meters east of the royal palace. When A <0, it represents a position that is -A meters west of the royal palace.
There are N houses along the roads in JOI, and the houses are numbered from 1 to N in order from the west. There are N people in JOI country, and the people are numbered from 1 to N. The people i live in the house i. The position of house i is represented by a non-zero even Ai. A1, ..., AN are all different.
In JOI country, the lack of movement of the people has become a problem in recent years. The King of JOI, who was concerned about the health of the people, ordered all the people to take a walk. When the King gives an order, all the people start walking eastward or westward all at once. The direction in which each nation starts walking is determined by each nation. All people walk at a speed of 1 meter per second when walking.
All the people of JOI love to talk. If you meet other people during your walk, you will stop there and start small talk. The same is true if you meet a people who have already stopped. The people who have stopped once will not start walking again.
There are Q important people in JOI country. The King of JOI wants to know the position of Q important persons T seconds after the order is issued. Create a program to find the position of Q important persons T seconds after the command is issued.
input
The input consists of 1 + N + Q lines.
On the first line, three integers N, T, Q (1 ≤ N ≤ 100000 (= 105), 0 ≤ T ≤ 1018, 1 ≤ Q ≤ 1000, 1 ≤ Q ≤ N) are written separated by blanks. ing. This means that there are N houses in JOI country, and we want to know the position of Q important people T seconds after the king issues the order.
Two integers Ai and Di (-1018 ≤ Ai ≤ 1018, Ai is a non-zero even number, 1 ≤ Di ≤ 2) are written on the i-th line of the following N lines, separated by blanks. Ai is an even number representing the position of house i. For all i (1 ≤ i ≤ N-1), Ai <Ai + 1 is satisfied. Di indicates the direction in which the national i starts walking after the command is issued. When Di = 1, national i starts walking eastward. When Di = 2, national i starts walking westward.
The integer Xi (1 ≤ Xi ≤ N) is written on the i-th line of the following Q lines. This means that the i-th important person lives in the house Xi. For all i (1 ≤ i ≤ Q-1), Xi <Xi + 1 is satisfied.
Of the five input data given, input 1 satisfies N ≤ 100 and T ≤ 10000. In addition, input 2 satisfies N ≤ 5000. Also, at input 3, there is a certain integer M (1 ≤ M ≤ N-1), Di = 1 for all i (1 ≤ i ≤ M), and for all j (M + 1 ≤ j ≤ N). Satisfy Dj = 2. Also, for inputs 1, 2, and 3, the absolute value of the integer given to the input does not exceed 1000000000 (= 109). Note that for inputs 4 and 5, the given integer does not fall within the range of 32-bit signed integers.
output
The output consists of Q lines.
On line i (1 ≤ i ≤ Q), output an integer representing the position of the i-th important person T seconds after the King issued the command. It is guaranteed that this value is an integer from the condition of the problem statement.
Input / output example
Input example 1
5 5 3
-8 1
-4 2
-twenty two
4 2
10 1
1
3
Five
Output example 1
-6
-6
15
Input example 2
7 18 5
-100 1
-56 2
-34 1
-30 1
-22 1
-4 2
18 2
1
3
Four
Five
7
Output example 2
-82
-16
-13
-13
0
Creative Commons License
Information Olympics Japan Committee work "15th Japan Information Olympics JOI 2015/2016 Qualifying Competition Tasks"
Example
Input
5 5 3
-8 1
-4 2
-2 2
4 2
10 1
1
3
5
Output
-6
-6
15 | instruction | 0 | 107,806 | 1 | 215,612 |
"Correct Solution:
```
from bisect import bisect_left as bl
n, t, q = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(n)]
qlst = [lst[int(input()) - 1] for _ in range(q)]
left = [a for a, d in lst if d == 2]
right = [a for a, d in lst if d == 1]
lenl = len(left)
lenr = len(right)
for a, d in qlst:
if d == 1:
coll_ind = bl(left, a)
if coll_ind >= lenl:
print(a + t)
else:
coll = left[coll_ind]
coll2 = right[bl(right, coll) - 1]
coll_pos = (coll + coll2) // 2
print(min(a + t, coll_pos))
else:
coll_ind = bl(right, a) - 1
if coll_ind == -1:
print(a - t)
else:
coll = right[coll_ind]
coll2 = left[bl(left, coll)]
coll_pos = (coll + coll2) // 2
print(max(a - t, coll_pos))
``` | output | 1 | 107,806 | 1 | 215,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
The JOI country has one long enough road running east to west. The royal palace of JOI country is along the road, and the position along the road in JOI country is represented by the integer A. When A = 0, it represents the position of the royal palace. When A> 0, it indicates the position A meters east of the royal palace. When A <0, it represents a position that is -A meters west of the royal palace.
There are N houses along the roads in JOI, and the houses are numbered from 1 to N in order from the west. There are N people in JOI country, and the people are numbered from 1 to N. The people i live in the house i. The position of house i is represented by a non-zero even Ai. A1, ..., AN are all different.
In JOI country, the lack of movement of the people has become a problem in recent years. The King of JOI, who was concerned about the health of the people, ordered all the people to take a walk. When the King gives an order, all the people start walking eastward or westward all at once. The direction in which each nation starts walking is determined by each nation. All people walk at a speed of 1 meter per second when walking.
All the people of JOI love to talk. If you meet other people during your walk, you will stop there and start small talk. The same is true if you meet a people who have already stopped. The people who have stopped once will not start walking again.
There are Q important people in JOI country. The King of JOI wants to know the position of Q important persons T seconds after the order is issued. Create a program to find the position of Q important persons T seconds after the command is issued.
input
The input consists of 1 + N + Q lines.
On the first line, three integers N, T, Q (1 ≤ N ≤ 100000 (= 105), 0 ≤ T ≤ 1018, 1 ≤ Q ≤ 1000, 1 ≤ Q ≤ N) are written separated by blanks. ing. This means that there are N houses in JOI country, and we want to know the position of Q important people T seconds after the king issues the order.
Two integers Ai and Di (-1018 ≤ Ai ≤ 1018, Ai is a non-zero even number, 1 ≤ Di ≤ 2) are written on the i-th line of the following N lines, separated by blanks. Ai is an even number representing the position of house i. For all i (1 ≤ i ≤ N-1), Ai <Ai + 1 is satisfied. Di indicates the direction in which the national i starts walking after the command is issued. When Di = 1, national i starts walking eastward. When Di = 2, national i starts walking westward.
The integer Xi (1 ≤ Xi ≤ N) is written on the i-th line of the following Q lines. This means that the i-th important person lives in the house Xi. For all i (1 ≤ i ≤ Q-1), Xi <Xi + 1 is satisfied.
Of the five input data given, input 1 satisfies N ≤ 100 and T ≤ 10000. In addition, input 2 satisfies N ≤ 5000. Also, at input 3, there is a certain integer M (1 ≤ M ≤ N-1), Di = 1 for all i (1 ≤ i ≤ M), and for all j (M + 1 ≤ j ≤ N). Satisfy Dj = 2. Also, for inputs 1, 2, and 3, the absolute value of the integer given to the input does not exceed 1000000000 (= 109). Note that for inputs 4 and 5, the given integer does not fall within the range of 32-bit signed integers.
output
The output consists of Q lines.
On line i (1 ≤ i ≤ Q), output an integer representing the position of the i-th important person T seconds after the King issued the command. It is guaranteed that this value is an integer from the condition of the problem statement.
Input / output example
Input example 1
5 5 3
-8 1
-4 2
-twenty two
4 2
10 1
1
3
Five
Output example 1
-6
-6
15
Input example 2
7 18 5
-100 1
-56 2
-34 1
-30 1
-22 1
-4 2
18 2
1
3
Four
Five
7
Output example 2
-82
-16
-13
-13
0
Creative Commons License
Information Olympics Japan Committee work "15th Japan Information Olympics JOI 2015/2016 Qualifying Competition Tasks"
Example
Input
5 5 3
-8 1
-4 2
-2 2
4 2
10 1
1
3
5
Output
-6
-6
15
Submitted Solution:
```
n, t, q = map(int, input().split())
x = [[0, 0, 0] for _ in range(n)]
s = -10e30
for i in range(n):
x[i] = list(map(int, input().split())) + [0]
x[i][2] = x[i][0] + (1 if x[i][1] == 1 else -1) * t
if x[i - 1][1] == 1 and x[i][1] == 2:
s = int((x[i - 1][0] + x[i][0]) / 2)
j = 1
while x[i - j][2] > s and x[i - j][1] == 1:
x[i - j][2] = s
j += 1
if x[i][1] == 2:
x[i][2] = max(x[i][2], s)
print(x)
for _ in range(q):
print(x[int(input()) - 1][2])
``` | instruction | 0 | 107,807 | 1 | 215,614 |
No | output | 1 | 107,807 | 1 | 215,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
The JOI country has one long enough road running east to west. The royal palace of JOI country is along the road, and the position along the road in JOI country is represented by the integer A. When A = 0, it represents the position of the royal palace. When A> 0, it indicates the position A meters east of the royal palace. When A <0, it represents a position that is -A meters west of the royal palace.
There are N houses along the roads in JOI, and the houses are numbered from 1 to N in order from the west. There are N people in JOI country, and the people are numbered from 1 to N. The people i live in the house i. The position of house i is represented by a non-zero even Ai. A1, ..., AN are all different.
In JOI country, the lack of movement of the people has become a problem in recent years. The King of JOI, who was concerned about the health of the people, ordered all the people to take a walk. When the King gives an order, all the people start walking eastward or westward all at once. The direction in which each nation starts walking is determined by each nation. All people walk at a speed of 1 meter per second when walking.
All the people of JOI love to talk. If you meet other people during your walk, you will stop there and start small talk. The same is true if you meet a people who have already stopped. The people who have stopped once will not start walking again.
There are Q important people in JOI country. The King of JOI wants to know the position of Q important persons T seconds after the order is issued. Create a program to find the position of Q important persons T seconds after the command is issued.
input
The input consists of 1 + N + Q lines.
On the first line, three integers N, T, Q (1 ≤ N ≤ 100000 (= 105), 0 ≤ T ≤ 1018, 1 ≤ Q ≤ 1000, 1 ≤ Q ≤ N) are written separated by blanks. ing. This means that there are N houses in JOI country, and we want to know the position of Q important people T seconds after the king issues the order.
Two integers Ai and Di (-1018 ≤ Ai ≤ 1018, Ai is a non-zero even number, 1 ≤ Di ≤ 2) are written on the i-th line of the following N lines, separated by blanks. Ai is an even number representing the position of house i. For all i (1 ≤ i ≤ N-1), Ai <Ai + 1 is satisfied. Di indicates the direction in which the national i starts walking after the command is issued. When Di = 1, national i starts walking eastward. When Di = 2, national i starts walking westward.
The integer Xi (1 ≤ Xi ≤ N) is written on the i-th line of the following Q lines. This means that the i-th important person lives in the house Xi. For all i (1 ≤ i ≤ Q-1), Xi <Xi + 1 is satisfied.
Of the five input data given, input 1 satisfies N ≤ 100 and T ≤ 10000. In addition, input 2 satisfies N ≤ 5000. Also, at input 3, there is a certain integer M (1 ≤ M ≤ N-1), Di = 1 for all i (1 ≤ i ≤ M), and for all j (M + 1 ≤ j ≤ N). Satisfy Dj = 2. Also, for inputs 1, 2, and 3, the absolute value of the integer given to the input does not exceed 1000000000 (= 109). Note that for inputs 4 and 5, the given integer does not fall within the range of 32-bit signed integers.
output
The output consists of Q lines.
On line i (1 ≤ i ≤ Q), output an integer representing the position of the i-th important person T seconds after the King issued the command. It is guaranteed that this value is an integer from the condition of the problem statement.
Input / output example
Input example 1
5 5 3
-8 1
-4 2
-twenty two
4 2
10 1
1
3
Five
Output example 1
-6
-6
15
Input example 2
7 18 5
-100 1
-56 2
-34 1
-30 1
-22 1
-4 2
18 2
1
3
Four
Five
7
Output example 2
-82
-16
-13
-13
0
Creative Commons License
Information Olympics Japan Committee work "15th Japan Information Olympics JOI 2015/2016 Qualifying Competition Tasks"
Example
Input
5 5 3
-8 1
-4 2
-2 2
4 2
10 1
1
3
5
Output
-6
-6
15
Submitted Solution:
```
n, t, q = map(int, input().split())
x = [[0, 0, 0] for _ in range(n)]
s = -10e40
for i in range(n):
x[i] = list(map(int, input().split())) + [0]
x[i][2] = x[i][0] + (1 if x[i][1] == 1 else -1) * t
if x[i - 1][1] == 1 and x[i][1] == 2:
s = int((x[i - 1][0] + x[i][0]) / 2)
j = 1
while x[i - j][2] > s and x[i - j][1] == 1:
x[i - j][2] = s
j += 1
if x[i][1] == 2:
x[i][2] = max(x[i][2], s)
for _ in range(q):
print(x[int(input()) - 1][2])
``` | instruction | 0 | 107,808 | 1 | 215,616 |
No | output | 1 | 107,808 | 1 | 215,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
The JOI country has one long enough road running east to west. The royal palace of JOI country is along the road, and the position along the road in JOI country is represented by the integer A. When A = 0, it represents the position of the royal palace. When A> 0, it indicates the position A meters east of the royal palace. When A <0, it represents a position that is -A meters west of the royal palace.
There are N houses along the roads in JOI, and the houses are numbered from 1 to N in order from the west. There are N people in JOI country, and the people are numbered from 1 to N. The people i live in the house i. The position of house i is represented by a non-zero even Ai. A1, ..., AN are all different.
In JOI country, the lack of movement of the people has become a problem in recent years. The King of JOI, who was concerned about the health of the people, ordered all the people to take a walk. When the King gives an order, all the people start walking eastward or westward all at once. The direction in which each nation starts walking is determined by each nation. All people walk at a speed of 1 meter per second when walking.
All the people of JOI love to talk. If you meet other people during your walk, you will stop there and start small talk. The same is true if you meet a people who have already stopped. The people who have stopped once will not start walking again.
There are Q important people in JOI country. The King of JOI wants to know the position of Q important persons T seconds after the order is issued. Create a program to find the position of Q important persons T seconds after the command is issued.
input
The input consists of 1 + N + Q lines.
On the first line, three integers N, T, Q (1 ≤ N ≤ 100000 (= 105), 0 ≤ T ≤ 1018, 1 ≤ Q ≤ 1000, 1 ≤ Q ≤ N) are written separated by blanks. ing. This means that there are N houses in JOI country, and we want to know the position of Q important people T seconds after the king issues the order.
Two integers Ai and Di (-1018 ≤ Ai ≤ 1018, Ai is a non-zero even number, 1 ≤ Di ≤ 2) are written on the i-th line of the following N lines, separated by blanks. Ai is an even number representing the position of house i. For all i (1 ≤ i ≤ N-1), Ai <Ai + 1 is satisfied. Di indicates the direction in which the national i starts walking after the command is issued. When Di = 1, national i starts walking eastward. When Di = 2, national i starts walking westward.
The integer Xi (1 ≤ Xi ≤ N) is written on the i-th line of the following Q lines. This means that the i-th important person lives in the house Xi. For all i (1 ≤ i ≤ Q-1), Xi <Xi + 1 is satisfied.
Of the five input data given, input 1 satisfies N ≤ 100 and T ≤ 10000. In addition, input 2 satisfies N ≤ 5000. Also, at input 3, there is a certain integer M (1 ≤ M ≤ N-1), Di = 1 for all i (1 ≤ i ≤ M), and for all j (M + 1 ≤ j ≤ N). Satisfy Dj = 2. Also, for inputs 1, 2, and 3, the absolute value of the integer given to the input does not exceed 1000000000 (= 109). Note that for inputs 4 and 5, the given integer does not fall within the range of 32-bit signed integers.
output
The output consists of Q lines.
On line i (1 ≤ i ≤ Q), output an integer representing the position of the i-th important person T seconds after the King issued the command. It is guaranteed that this value is an integer from the condition of the problem statement.
Input / output example
Input example 1
5 5 3
-8 1
-4 2
-twenty two
4 2
10 1
1
3
Five
Output example 1
-6
-6
15
Input example 2
7 18 5
-100 1
-56 2
-34 1
-30 1
-22 1
-4 2
18 2
1
3
Four
Five
7
Output example 2
-82
-16
-13
-13
0
Creative Commons License
Information Olympics Japan Committee work "15th Japan Information Olympics JOI 2015/2016 Qualifying Competition Tasks"
Example
Input
5 5 3
-8 1
-4 2
-2 2
4 2
10 1
1
3
5
Output
-6
-6
15
Submitted Solution:
```
n, t, q = map(int, input().split())
x = [[0, 0, 0] for _ in range(n)]
s = -float("inf")
for i in range(n):
x[i] = list(map(int, input().split())) + [0]
x[i][2] = x[i][0] + (1 if x[i][1] == 1 else -1) * t
if x[i - 1][1] == 1 and x[i][1] == 2:
s = int((x[i - 1][0] + x[i][0]) / 2)
j = 1
while x[i - j][2] > s and x[i - j][1] == 1:
x[i - j][2] = s
j += 1
if x[i][1] == 2:
x[i][2] = max(x[i][2], s)
for _ in range(q):
print(x[int(input()) - 1][2])
``` | instruction | 0 | 107,809 | 1 | 215,618 |
No | output | 1 | 107,809 | 1 | 215,619 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
The JOI country has one long enough road running east to west. The royal palace of JOI country is along the road, and the position along the road in JOI country is represented by the integer A. When A = 0, it represents the position of the royal palace. When A> 0, it indicates the position A meters east of the royal palace. When A <0, it represents a position that is -A meters west of the royal palace.
There are N houses along the roads in JOI, and the houses are numbered from 1 to N in order from the west. There are N people in JOI country, and the people are numbered from 1 to N. The people i live in the house i. The position of house i is represented by a non-zero even Ai. A1, ..., AN are all different.
In JOI country, the lack of movement of the people has become a problem in recent years. The King of JOI, who was concerned about the health of the people, ordered all the people to take a walk. When the King gives an order, all the people start walking eastward or westward all at once. The direction in which each nation starts walking is determined by each nation. All people walk at a speed of 1 meter per second when walking.
All the people of JOI love to talk. If you meet other people during your walk, you will stop there and start small talk. The same is true if you meet a people who have already stopped. The people who have stopped once will not start walking again.
There are Q important people in JOI country. The King of JOI wants to know the position of Q important persons T seconds after the order is issued. Create a program to find the position of Q important persons T seconds after the command is issued.
input
The input consists of 1 + N + Q lines.
On the first line, three integers N, T, Q (1 ≤ N ≤ 100000 (= 105), 0 ≤ T ≤ 1018, 1 ≤ Q ≤ 1000, 1 ≤ Q ≤ N) are written separated by blanks. ing. This means that there are N houses in JOI country, and we want to know the position of Q important people T seconds after the king issues the order.
Two integers Ai and Di (-1018 ≤ Ai ≤ 1018, Ai is a non-zero even number, 1 ≤ Di ≤ 2) are written on the i-th line of the following N lines, separated by blanks. Ai is an even number representing the position of house i. For all i (1 ≤ i ≤ N-1), Ai <Ai + 1 is satisfied. Di indicates the direction in which the national i starts walking after the command is issued. When Di = 1, national i starts walking eastward. When Di = 2, national i starts walking westward.
The integer Xi (1 ≤ Xi ≤ N) is written on the i-th line of the following Q lines. This means that the i-th important person lives in the house Xi. For all i (1 ≤ i ≤ Q-1), Xi <Xi + 1 is satisfied.
Of the five input data given, input 1 satisfies N ≤ 100 and T ≤ 10000. In addition, input 2 satisfies N ≤ 5000. Also, at input 3, there is a certain integer M (1 ≤ M ≤ N-1), Di = 1 for all i (1 ≤ i ≤ M), and for all j (M + 1 ≤ j ≤ N). Satisfy Dj = 2. Also, for inputs 1, 2, and 3, the absolute value of the integer given to the input does not exceed 1000000000 (= 109). Note that for inputs 4 and 5, the given integer does not fall within the range of 32-bit signed integers.
output
The output consists of Q lines.
On line i (1 ≤ i ≤ Q), output an integer representing the position of the i-th important person T seconds after the King issued the command. It is guaranteed that this value is an integer from the condition of the problem statement.
Input / output example
Input example 1
5 5 3
-8 1
-4 2
-twenty two
4 2
10 1
1
3
Five
Output example 1
-6
-6
15
Input example 2
7 18 5
-100 1
-56 2
-34 1
-30 1
-22 1
-4 2
18 2
1
3
Four
Five
7
Output example 2
-82
-16
-13
-13
0
Creative Commons License
Information Olympics Japan Committee work "15th Japan Information Olympics JOI 2015/2016 Qualifying Competition Tasks"
Example
Input
5 5 3
-8 1
-4 2
-2 2
4 2
10 1
1
3
5
Output
-6
-6
15
Submitted Solution:
```
import bisect
n, t, q = map(int, input().split())
x = [[0, 0] for _ in range(n)]
s = []
for i in range(n):
x[i] = list(map(int, input().split()))
if x[i - 1][1] == 1 and x[i][1] == 2:
s.append(int((x[i - 1][0] + x[i][0]) / 2))
for _ in range(q):
v = int(input()) - 1
if x[v][1] == 1:
st = bisect.bisect_left(s, x[v][0])
print(min(x[v][0] + t, s[st]) if st < len(s) else x[v][0] + t)
else:
st = bisect.bisect_left(s, x[v][0])
print(max(x[v][0] - t, s[st - 1]) if st > 0 else x[v][0] - t)
``` | instruction | 0 | 107,810 | 1 | 215,620 |
No | output | 1 | 107,810 | 1 | 215,621 |
Provide tags and a correct Python 3 solution for this coding contest problem.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules. | instruction | 0 | 107,977 | 1 | 215,954 |
Tags: dfs and similar, dp, graphs, shortest paths
Correct Solution:
```
'''
from bisect import *
from collections import *
from math import gcd,ceil,sqrt,floor,inf
from heapq import *
from itertools import *
from operator import add,mul,sub,xor,truediv,floordiv
from functools import *'''
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# region fastio
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")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
#------------------------------------------------------------------------
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
mod=10**9+7
farr=[1]
ifa=[]
def fact(x,mod=0):
if mod:
while x>=len(farr):
farr.append(farr[-1]*len(farr)%mod)
else:
while x>=len(farr):
farr.append(farr[-1]*len(farr))
return farr[x]
def ifact(x,mod):
global ifa
fact(x,mod)
ifa.append(pow(farr[-1],mod-2,mod))
for i in range(x,0,-1):
ifa.append(ifa[-1]*i%mod)
ifa.reverse()
def per(i,j,mod=0):
if i<j: return 0
if not mod:
return fact(i)//fact(i-j)
return farr[i]*ifa[i-j]%mod
def com(i,j,mod=0):
if i<j: return 0
if not mod:
return per(i,j)//fact(j)
return per(i,j,mod)*ifa[j]%mod
def catalan(n):
return com(2*n,n)//(n+1)
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def floorsum(a,b,c,n):#sum((a*i+b)//c for i in range(n+1))
if a==0:return b//c*(n+1)
if a>=c or b>=c: return floorsum(a%c,b%c,c,n)+b//c*(n+1)+a//c*n*(n+1)//2
m=(a*n+b)//c
return n*m-floorsum(c,c-b-1,a,m-1)
def inverse(a,m):
a%=m
if a<=1: return a
return ((1-inverse(m,a)*m)//a)%m
def lowbit(n):
return n&-n
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
class ST:
def __init__(self,arr):#n!=0
n=len(arr)
mx=n.bit_length()#取不到
self.st=[[0]*mx for i in range(n)]
for i in range(n):
self.st[i][0]=arr[i]
for j in range(1,mx):
for i in range(n-(1<<j)+1):
self.st[i][j]=max(self.st[i][j-1],self.st[i+(1<<j-1)][j-1])
def query(self,l,r):
if l>r:return -inf
s=(r+1-l).bit_length()-1
return max(self.st[l][s],self.st[r-(1<<s)+1][s])
class DSU:#容量+路径压缩
def __init__(self,n):
self.c=[-1]*n
def same(self,x,y):
return self.find(x)==self.find(y)
def find(self,x):
if self.c[x]<0:
return x
self.c[x]=self.find(self.c[x])
return self.c[x]
def union(self,u,v):
u,v=self.find(u),self.find(v)
if u==v:
return False
if self.c[u]>self.c[v]:
u,v=v,u
self.c[u]+=self.c[v]
self.c[v]=u
return True
def size(self,x): return -self.c[self.find(x)]
class UFS:#秩+路径
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return prime
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
seen.add(u)
for v,w in graph[u]:
if v not in d or d[v]>d[u]+w:
d[v]=d[u]+w
heappush(heap,(d[v],v))
return d
def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)]
def lcm(a,b): return a*b//gcd(a,b)
def lis(nums):
res=[]
for k in nums:
i=bisect.bisect_left(res,k)
if i==len(res):
res.append(k)
else:
res[i]=k
return len(res)
def RP(nums):#逆序对
n = len(nums)
s=set(nums)
d={}
for i,k in enumerate(sorted(s),1):
d[k]=i
bi=BIT([0]*(len(s)+1))
ans=0
for i in range(n-1,-1,-1):
ans+=bi.query(d[nums[i]]-1)
bi.update(d[nums[i]],1)
return ans
class DLN:
def __init__(self,val):
self.val=val
self.pre=None
self.next=None
def nb(i,j):
for ni,nj in [[i+1,j],[i-1,j],[i,j-1],[i,j+1]]:
if 0<=ni<n and 0<=nj<m:
yield ni,nj
def topo(n):
q=deque()
res=[]
for i in range(1,n+1):
if ind[i]==0:
q.append(i)
res.append(i)
while q:
u=q.popleft()
for v in g[u]:
ind[v]-=1
if ind[v]==0:
q.append(v)
res.append(v)
return res
@bootstrap
def gdfs(r,p):
if len(g[r])==1 and p!=-1:
yield None
for ch in g[r]:
if ch!=p:
yield gdfs(ch,r)
yield None
from collections import deque
t=1
for i in range(t):
n,m=RL()
d=RLL()
inf=10**9
d=[-inf]+sorted(d)+[inf]
g,r=RL()
q=deque([(1,0)])
vis=[0]*((m+1)*g)
dis=[inf]*((m+1)*g)
vis[0]=1
dis[0]=0
cur=0
ans=inf
f=False
while q:
nq=deque()
while q:
u,t=q.popleft()
if u==m:
if t!=0:
ans=min(ans,cur*(g+r)+t)
else:
ans=min(ans,cur*g+(cur-1)*r)
f=True
continue
if t+d[u]-d[u-1]<g and not vis[(u-2)*g+t+d[u]-d[u-1]]:
vis[(u-2)*g+t+d[u]-d[u-1]]=1
q.append((u-1,t+d[u]-d[u-1]))
elif t+d[u]-d[u-1]==g and not vis[(u-2)*g]:
vis[(u-2)*g]=1
nq.append((u-1,0))
if t-d[u]+d[u+1]<g and not vis[u*g+t-d[u]+d[u+1]]:
vis[u*g+t-d[u]+d[u+1]]=1
q.append((u+1,t-d[u]+d[u+1]))
elif t-d[u]+d[u+1]==g and not vis[u*g]:
vis[u*g]=1
nq.append((u+1,0))
#print(q)
#print('nq',nq)
if f:
print(ans)
exit()
q=nq
cur+=1
print(-1)
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thr
ead(target=main)
t.start()
t.join()
'''
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thread(target=main)
t.start()
t.join()
'''
``` | output | 1 | 107,977 | 1 | 215,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules. | instruction | 0 | 107,978 | 1 | 215,956 |
Tags: dfs and similar, dp, graphs, shortest paths
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
from typing import List
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
def compute(n, m, g, r, A: List[int]):
A.sort()
WHITE = -1
GREY = -2
states = [[WHITE] * (g+1) for _ in range(m)]
states[0][g] = 0
states[0][0] = 0
q = deque([(0, g)])
def process_neib(ineib, gneib):
if states[ineib][gneib] != WHITE:
#print(f"Skipped as grey")
return
if ineib == m-1:
#no need to wait there
states[ineib][gneib] = states[index][g_left]
#print(f"Final state dist is {states[ineib][gneib]}")
elif gneib == 0:
states[ineib][gneib] = states[index][g_left] + 1
states[ineib][g] = states[ineib][gneib]
gneib = g
q.append((ineib, gneib))
#print(f"appended right with distance {states[ineib][0]}")
else:
states[ineib][gneib] = states[index][g_left]
q.appendleft((ineib, gneib))
while q:
# sit for this is known
#print(f"Queue is {[(A[i], t) for i,t in q]}")
index, g_left = q.popleft()
#print(f"Popped {A[index], g_left}. Dist is {states[index][g_left]}")
#neib = get_neib(index, g_left, A)
#print(f"Neighbors are {[(A[i], t) for i, t in neib]}")
if index > 0:
# there exists a next one
delta = A[index] - A[index-1]
if g_left >= delta:
process_neib(index-1, g_left-delta)
if index < m-1:
delta = A[index+1] - A[index]
if g_left >= delta:
process_neib(index+1, g_left-delta)
#print(f"appended left with distance {states[ineib][gneib]}")
res = float('inf')
for g_left in range(g):
if states[m-1][g_left] >= 0:
res = min(res, states[m-1][g_left] * (r+g) + g-g_left)
if res != float('inf'):
print(res)
else:
print('-1')
def from_file(f):
return f.readline
# with open('52.txt') as f:
# input = from_file(f)
n, m = invr()
A = inlt()
g,r = invr()
compute(n, m, g, r, A)
``` | output | 1 | 107,978 | 1 | 215,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules. | instruction | 0 | 107,979 | 1 | 215,958 |
Tags: dfs and similar, dp, graphs, shortest paths
Correct Solution:
```
import collections
n,m=map(int,input().split())
arr=list(map(int,input().split()))
arr=sorted(arr)
g,r=map(int,input().split())
q=collections.deque()
q.append((0,0,0))
checked=[[-1]*(g) for _ in range(m)]
checked[0][0]=0
while len(q)!=0:
v,t,cnt=q.popleft()
if v!=m-1:
cost1=arr[v+1]-arr[v]
if t+cost1<=g:
if checked[v+1][(t+cost1)%g]==-1:
if t+cost1<g:
q.appendleft((v+1,t+cost1,cnt))
checked[v+1][t+cost1]=cnt
else:
q.append((v+1,0,cnt+1))
checked[v+1][0]=cnt+1
if v!=0:
cost2=arr[v]-arr[v-1]
if t+cost2<=g:
if checked[v-1][(t+cost2)%g]==-1:
if t+cost2<g:
q.appendleft((v-1,t+cost2,cnt))
checked[v-1][t+cost2]=cnt
else:
q.append((v-1,0,cnt+1))
checked[v-1][0]=cnt+1
ans=10**18
for i in range(m):
for j in range(g):
if checked[i][j]==-1:
continue
else:
if j+n-arr[i]<=g:
ans=min(ans,checked[i][j]*(g+r)+j+n-arr[i])
if ans==10**18:
print(-1)
else:
print(ans)
``` | output | 1 | 107,979 | 1 | 215,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules. | instruction | 0 | 107,980 | 1 | 215,960 |
Tags: dfs and similar, dp, graphs, shortest paths
Correct Solution:
```
import collections
n,m=map(int,input().split())
m+=2
arr=list(map(int,input().split()))
arr.append(0)
arr.append(n)
arr=sorted(arr)
g,r=map(int,input().split())
q=collections.deque()
q.append((0,0))
dist=[[0]*(g+1) for _ in range(m+2)]
checked=[[0]*(g+1) for _ in range(m+2)]
checked[0][0]=1
ans=-1
while len(q)!=0:
v,t=q.popleft()
if t==0:
if n-arr[v]<=g:
tmp=dist[v][t]*(g+r)+n-arr[v]
if ans==-1 or ans>tmp:
ans=tmp
if t==g:
if checked[v][0]==0:
checked[v][0]=1
dist[v][0]=dist[v][t]+1
q.append((v,0))
continue
if v!=0:
cost=t+arr[v]-arr[v-1]
if cost<=g and checked[v-1][cost]==0:
checked[v-1][cost]=1
dist[v-1][cost]=dist[v][t]
q.appendleft((v-1,cost))
if v!=m-1:
cost=t+arr[v+1]-arr[v]
if cost<=g and checked[v+1][cost]==0:
checked[v+1][cost]=1
dist[v+1][cost]=dist[v][t]
q.appendleft((v+1,cost))
print(ans)
``` | output | 1 | 107,980 | 1 | 215,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules. | instruction | 0 | 107,981 | 1 | 215,962 |
Tags: dfs and similar, dp, graphs, shortest paths
Correct Solution:
```
import sys
from array import array # noqa: F401
import typing as Tp # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
def output(*args):
sys.stdout.buffer.write(
('\n'.join(map(str, args)) + '\n').encode('utf-8')
)
def main():
from collections import deque
n, m = map(int, input().split())
pos = [-10**9] + sorted(map(int, input().split())) + [10**9]
g, r = map(int, input().split())
inf = 10**9
dp = [array('i', [inf]) * (g + 1) for _ in range(m + 2)]
dp[1][0] = 0
dq = deque([(1, 0)])
while dq:
v, time = dq.popleft()
p_dist, n_dist = pos[v] - pos[v - 1], pos[v + 1] - pos[v]
if time == g:
if p_dist <= g and dp[v - 1][p_dist] > dp[v][time] + 1:
dp[v - 1][p_dist] = dp[v][time] + 1
dq.append((v - 1, p_dist))
if n_dist <= g and dp[v + 1][n_dist] > dp[v][time] + 1:
dp[v + 1][n_dist] = dp[v][time] + 1
dq.append((v + 1, n_dist))
else:
if time + p_dist <= g and dp[v - 1][time + p_dist] > dp[v][time]:
dp[v - 1][time + p_dist] = dp[v][time]
dq.appendleft((v - 1, time + p_dist))
if time + n_dist <= g and dp[v + 1][time + n_dist] > dp[v][time]:
dp[v + 1][time + n_dist] = dp[v][time]
dq.appendleft((v + 1, time + n_dist))
ans = min(dp[-2][i] * (g + r) + i for i in range(g + 1))
print(ans if ans < inf else -1)
if __name__ == '__main__':
main()
``` | output | 1 | 107,981 | 1 | 215,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules.
Submitted Solution:
```
import collections
n,m=map(int,input().split())
arr=list(map(int,input().split()))
g,r=map(int,input().split())
q=collections.deque()
q.append((0,0,0)) #v,t%g,cnt
checked=[[-1]*(g) for _ in range(m)]
checked[0][0]=0
while len(q)!=0:
v,t,cnt=q.popleft()
if v!=m-1:
cost1=arr[v+1]-arr[v]
if t+cost1<=g:
if checked[v+1][(t+cost1)%g]==-1:
if t+cost1<g:
q.append((v+1,t+cost1,cnt))
checked[v+1][t+cost1]=cnt
else:
q.append((v+1,0,cnt+1))
checked[v+1][0]=cnt+1
if v!=0:
cost2=arr[v]-arr[v-1]
if t+cost2<=g:
if checked[v-1][(t+cost2)%g]==-1:
if t+cost2<g:
q.append((v-1,t+cost2,cnt))
checked[v-1][t+cost2]=cnt
else:
q.append((v-1,0,cnt+1))
checked[v-1][0]=cnt+1
ans=10**18
for i in range(g):
if checked[m-1][i]==-1:
continue
ans=min(ans,checked[m-1][i]*(g+r)+i)
if ans==10**18:
print(-1)
else:
print(ans)
``` | instruction | 0 | 107,982 | 1 | 215,964 |
No | output | 1 | 107,982 | 1 | 215,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules.
Submitted Solution:
```
import collections
n,m=map(int,input().split())
arr=list(map(int,input().split()))
g,r=map(int,input().split())
q=collections.deque()
q.append((0,0,0)) #v,t%g,cnt
checked=[[-1]*(g) for _ in range(m)]
checked[0][0]=0
while len(q)!=0:
v,t,cnt=q.popleft()
if v!=m-1:
cost1=arr[v+1]-arr[v]
if t+cost1<=g:
if checked[v+1][(t+cost1)%g]==-1:
if t+cost1<g:
q.append((v+1,t+cost1,cnt))
checked[v+1][t+cost1]=cnt
else:
q.append((v+1,0,cnt+1))
checked[v+1][0]=cnt+1
if v!=0:
cost2=arr[v]-arr[v-1]
if t+cost2<=g:
if checked[v-1][(t+cost2)%g]==-1:
if t+cost2<g:
q.append((v-1,t+cost2,cnt))
checked[v-1][t+cost2]=cnt
else:
q.append((v-1,0,cnt+1))
checked[v-1][0]=cnt+1
ans=10**18
for i in range(g):
if checked[m-1][i]==-1:
continue
if i==0:
ans=min(ans,(checked[m-1][i]-1)*(g+r)+g)
else:
ans=min(ans,checked[m-1][i]*(g+r)+i)
if ans==10**18:
print(-1)
else:
print(ans)
``` | instruction | 0 | 107,983 | 1 | 215,966 |
No | output | 1 | 107,983 | 1 | 215,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules.
Submitted Solution:
```
import collections
n,m=map(int,input().split())
m+=2
arr=list(map(int,input().split()))
arr.append(0)
arr.append(n)
arr=sorted(arr)
g,r=map(int,input().split())
q=collections.deque()
q.append((0,0))
dist=[[0]*(g+1) for _ in range(m+2)]
checked=[[0]*(g+1) for _ in range(m+2)]
checked[0][0]=1
ans=-1
while len(q)!=0:
v,t=q.popleft()
if t==0:
if n-arr[v]<=g:
tmp=dist[v][t]*(g+r)+n-arr[v]
if ans==-1 or ans>tmp:
ans=tmp
if t==g:
if checked[v][0]==0:
checked[v][0]=1
dist[v][0]=dist[v][t]+1
q.append((v,0))
continue
if v!=m-1:
cost=t+arr[v+1]-arr[v]
if cost<=g and checked[v+1][cost]==0:
checked[v+1][cost]=1
dist[v+1][cost]=dist[v][t]
q.append((v+1,cost))
if v!=0:
cost=t+arr[v]-arr[v-1]
if cost<=g and checked[v-1][cost]==0:
checked[v-1][cost]=1
dist[v-1][cost]=dist[v][t]
q.append((v-1,cost))
print(ans)
``` | instruction | 0 | 107,984 | 1 | 215,968 |
No | output | 1 | 107,984 | 1 | 215,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.
On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.
Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up g seconds green, and then r seconds red, then again g seconds green and so on.
Formally, the road can be represented as a segment [0, n]. Initially, Denis is at point 0. His task is to get to point n in the shortest possible time.
He knows many different integers d_1, d_2, …, d_m, where 0 ≤ d_i ≤ n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.
Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:
* He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ± 1 in 1 second. While doing so, he must always stay inside the segment [0, n].
* He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1 and he walked on a safety island, then he can change his position by ± 1. Otherwise, he can change his position only by +1. Similarly, if in the previous second he changed his position by -1, on a safety island he can change position by ± 1, and at any other point by -1.
* At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.
Denis has crossed the road as soon as his coordinate becomes equal to n.
This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
Input
The first line contains two integers n and m (1 ≤ n ≤ 10^6, 2 ≤ m ≤ min(n + 1, 10^4)) — road width and the number of safety islands.
The second line contains m distinct integers d_1, d_2, …, d_m (0 ≤ d_i ≤ n) — the points where the safety islands are located. It is guaranteed that there are 0 and n among them.
The third line contains two integers g, r (1 ≤ g, r ≤ 1000) — the time that the green light stays on and the time that the red light stays on.
Output
Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.
If it is impossible to cross the road output -1.
Examples
Input
15 5
0 3 7 14 15
11 11
Output
45
Input
13 4
0 3 7 13
9 9
Output
-1
Note
In the first test, the optimal route is:
* for the first green light, go to 7 and return to 3. In this case, we will change the direction of movement at the point 7, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 3, where there is also a safety island. The next 11 seconds we have to wait for the red light.
* for the second green light reaches 14. Wait for the red light again.
* for 1 second go to 15. As a result, Denis is at the end of the road.
In total, 45 seconds are obtained.
In the second test, it is impossible to cross the road according to all the rules.
Submitted Solution:
```
import collections
n,m=map(int,input().split())
arr=list(map(int,input().split()))
arr=sorted(arr)
g,r=map(int,input().split())
q=collections.deque()
q.append((0,0,0))
checked=[[-1]*(g) for _ in range(m)]
checked[0][0]=0
while len(q)!=0:
v,t,cnt=q.popleft()
if v!=m-1:
cost1=arr[v+1]-arr[v]
if t+cost1<=g:
if checked[v+1][(t+cost1)%g]==-1:
if t+cost1<g:
q.appendleft((v+1,t+cost1,cnt))
checked[v+1][t+cost1]=cnt
else:
q.appendleft((v+1,0,cnt+1))
checked[v+1][0]=cnt+1
if v!=0:
cost2=arr[v]-arr[v-1]
if t+cost2<=g:
if checked[v-1][(t+cost2)%g]==-1:
if t+cost2<g:
q.appendleft((v-1,t+cost2,cnt))
checked[v-1][t+cost2]=cnt
else:
q.appendleft((v-1,0,cnt+1))
checked[v-1][0]=cnt+1
ans=10**18
for i in range(m):
for j in range(g):
if checked[i][j]==-1:
continue
if arr[i]==n-1:
break
else:
if j+n-arr[i]<=g:
ans=min(ans,checked[i][j]*(g+r)+j+n-arr[i])
if ans==10**18:
print(-1)
else:
print(ans)
``` | instruction | 0 | 107,985 | 1 | 215,970 |
No | output | 1 | 107,985 | 1 | 215,971 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>. | instruction | 0 | 108,164 | 1 | 216,328 |
Tags: combinatorics, implementation, math
Correct Solution:
```
import fractions
x = int(input())
a = sorted(map(int, input().split()))
z = 2 * x + 1
ans = sum(((4 * i - z) * a[i - 1]) for i in range(1, x + 1, 1))
s = fractions._gcd(ans, x)
ans //= s; x //= s
ss = []
ss.append(ans); ss.append(x)
print(*ss)
``` | output | 1 | 108,164 | 1 | 216,329 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>. | instruction | 0 | 108,165 | 1 | 216,330 |
Tags: combinatorics, implementation, math
Correct Solution:
```
from fractions import *
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
a1 = a[0]
lef_sum = 0
for i in range(1,n):
lef_sum+= a[i]*i - a1
a1+=a[i]
lef_sum*=2
total = lef_sum+a1
s = Fraction(total,n)
print(s.numerator,s.denominator)
``` | output | 1 | 108,165 | 1 | 216,331 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>. | instruction | 0 | 108,166 | 1 | 216,332 |
Tags: combinatorics, implementation, math
Correct Solution:
```
import math
t=int(input())
l=list(map(int,input().split()))
l.sort()
s=sum(l)
a=s
j=1
m=0
for i in l:
s-=i
m+=s-(t-j)*i
j+=1
x=2*m+a
y=math.gcd(x,t)
x//=y
t//=y
print(x,t)
``` | output | 1 | 108,166 | 1 | 216,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>. | instruction | 0 | 108,167 | 1 | 216,334 |
Tags: combinatorics, implementation, math
Correct Solution:
```
import math
n=int(input())
arr=[]
arr = [int(item) for item in input().split()]
length=len(arr)
sum2=0
sum=0
for i in range(length):
val=arr[i]
sum2+=val
#print(sum)
arr.sort()
pre=[]
bef=0
for i in range(length):
sum+=(i*arr[i]-bef)
bef+=arr[i]
sum*=2
sum+=sum2
div1=math.gcd(sum,n)
sum//=div1
n//=div1
print(sum," ",n)
``` | output | 1 | 108,167 | 1 | 216,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>. | instruction | 0 | 108,168 | 1 | 216,336 |
Tags: combinatorics, implementation, math
Correct Solution:
```
def gcd(a,b) : return a if b==0 else gcd(b,a%b)
n = int(input())
l = input().split()
l = [int(i) for i in l]
l.sort()
t = sum((i+i-n+1)*l[i] for i in range(n))
t = t+t+sum(l)
d = gcd(t,n)
print(t//d,n//d)
# Made By Mostafa_Khaled
``` | output | 1 | 108,168 | 1 | 216,337 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>. | instruction | 0 | 108,169 | 1 | 216,338 |
Tags: combinatorics, implementation, math
Correct Solution:
```
def gcd(a, b): return gcd(b % a, a) if a else b
n = int(input())
a = sorted(map(int, input().split()))
p, q = 0, n
for i in range(1, n): p += i * (n - i) * (a[i] - a[i - 1])
p = 2 * p + sum(a)
r = gcd(p, q)
print(p // r, q // r)
``` | output | 1 | 108,169 | 1 | 216,339 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>. | instruction | 0 | 108,170 | 1 | 216,340 |
Tags: combinatorics, implementation, math
Correct Solution:
```
import math
n = int(input())
l = [int(x) for x in input().split()]
a1 = sum(l)
a2 = n
a3 = 0
temp = 0
l.sort()
for i in range(n):
temp += l[n-i-1]
a3-=(a1-temp)
a3+=(n-i-1)*(l[n-i-1])
a1 = a1+a3+a3
a4 = math.gcd(a1, a2)
print(a1//a4, a2//a4)
``` | output | 1 | 108,170 | 1 | 216,341 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>. | instruction | 0 | 108,171 | 1 | 216,342 |
Tags: combinatorics, implementation, math
Correct Solution:
```
from math import gcd
n = int(input())
a = list(map(int, input().split()))
a.sort()
s = sum(a)
current_sum = 0
s2 = 0
for i in range(n):
s2 += a[i] * i - current_sum
current_sum += a[i]
g = gcd(s + 2*s2, n)
print((s +2*s2) // g, n // g)
``` | output | 1 | 108,171 | 1 | 216,343 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>.
Submitted Solution:
```
from fractions import Fraction
n = int(input())
l = [int(x) for x in input().split()]
l.sort()
f = Fraction(sum([(3-2*n+4*i)*l[i] for i in range(n)]),n)
print(f.numerator,f.denominator)
##import itertools
##tot2 = 0
##for i in itertools.permutations(l):
## loc = 0
## tot = 0
## for e in i:
## tot += abs(loc-e)
## loc = e
## #print(i,tot)
## tot2 += tot
##import math
##f2 = Fraction(tot2,math.factorial(n))
##print(f2.numerator,f2.denominator)
##
``` | instruction | 0 | 108,172 | 1 | 216,344 |
Yes | output | 1 | 108,172 | 1 | 216,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>.
Submitted Solution:
```
import math
def main(arr):
arr.sort()
n=len(arr)
prefix=[arr[0]]
for i in range(1,len(arr)):
prefix.append(prefix[-1]+arr[i])
ans=sum(arr)
for i in range(len(arr)):
s=0
if (i>0):
s=prefix[i-1]
val=(arr[i]*i-s+(prefix[-1]-prefix[i])-(n-1-i)*arr[i])
ans+=val
d=n
g=math.gcd(d,ans)
print(ans//g,d//g)
return
n=int(input())
arr=list(map(int,input().split()))
main(arr)
``` | instruction | 0 | 108,173 | 1 | 216,346 |
Yes | output | 1 | 108,173 | 1 | 216,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>.
Submitted Solution:
```
def gcd(a,b):
if b == 0:
return a
return gcd(b,a%b)
n = int(input())
a = [int(x) for x in input().split()]
sum1 = sum(a)
sum2 = 0
sumbefore = 0
a.sort()
for i in range(n):
sum2 += a[i]*(i) - sumbefore
sumbefore += a[i]
sumtot = sum1 + 2*sum2
k = gcd(sumtot,n)
print(sumtot//k,n//k)
``` | instruction | 0 | 108,174 | 1 | 216,348 |
Yes | output | 1 | 108,174 | 1 | 216,349 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>.
Submitted Solution:
```
from math import *
n=int(input())
arr=list(map(int,input().split()))
arr.sort()
S1=sum(arr)
sums=0
sumsi=arr[0]
for i in range(1,n):
sums+=(i)*(arr[i])-sumsi
sumsi+=arr[i]
S2=sums
num=S1+2*S2
den=n
#print(num,den)
while(int(gcd(num,den))!=1):
x=gcd(num,den)
num=num//x
den=den//x
print(int(num),int(den))
``` | instruction | 0 | 108,175 | 1 | 216,350 |
Yes | output | 1 | 108,175 | 1 | 216,351 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
Input
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
Examples
Input
3
2 3 5
Output
22 3
Note
Consider 6 possible routes:
* [2, 3, 5]: total distance traveled: |2 – 0| + |3 – 2| + |5 – 3| = 5;
* [2, 5, 3]: |2 – 0| + |5 – 2| + |3 – 5| = 7;
* [3, 2, 5]: |3 – 0| + |2 – 3| + |5 – 2| = 7;
* [3, 5, 2]: |3 – 0| + |5 – 3| + |2 – 5| = 8;
* [5, 2, 3]: |5 – 0| + |2 – 5| + |3 – 2| = 9;
* [5, 3, 2]: |5 – 0| + |3 – 5| + |2 – 3| = 8.
The average travel distance is <image> = <image> = <image>.
Submitted Solution:
```
'''input
3
2 3 5
'''
from sys import stdin
import collections
import math
# main starts
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
arr.sort()
numerator = (n - 1) * sum(arr)
temp = 0
for i in range(n):
temp += i * arr[i]
for i in range(n - 1, -1, -1):
temp -= arr[n - 1 - i] * i
numerator += 4 * temp
denominator = 1
for i in range(2, n + 1):
denominator *= i
g = math.gcd(numerator, denominator)
numerator //= g
denominator //= g
print(numerator, denominator)
``` | instruction | 0 | 108,176 | 1 | 216,352 |
No | output | 1 | 108,176 | 1 | 216,353 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.