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.
There are N towns numbered 1, 2, \cdots, N.
Some roads are planned to be built so that each of them connects two distinct towns bidirectionally. Currently, there are no roads connecting towns.
In the planning of construction, each town chooses one town different from itself and requests the following: roads are built so that the chosen town is reachable from itself using one or more roads.
These requests from the towns are represented by an array P_1, P_2, \cdots, P_N. If P_i = -1, it means that Town i has not chosen the request; if 1 \leq P_i \leq N, it means that Town i has chosen Town P_i.
Let K be the number of towns i such that P_i = -1. There are (N-1)^K ways in which the towns can make the requests. For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).
Constraints
* 2 \leq N \leq 5000
* P_i = -1 or 1 \leq P_i \leq N.
* P_i \neq i
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
P_1 P_2 \cdots P_N
Output
For each way to make requests, find the minimum number of roads needed to meet all the requests, and print the sum of those (N-1)^K numbers, modulo (10^9+7).
Examples
Input
4
2 1 -1 3
Output
8
Input
2
2 1
Output
1
Input
10
2 6 9 -1 6 9 -1 -1 -1 -1
Output
527841
Submitted Solution:
```
import sys
class UFT: #Union-find tree class
def __init__(self, N):
self.tree = [int(i) for i in range(N)]
self.rank = [0 for i in range(N)]
def find(self, a):
if self.tree[a] == a: return a
else:
self.tree[a] = self.find(self.tree[a])
return self.tree[a]
def unite(self, a, b):
a = self.find(a)
b = self.find(b)
if a == b: return
if self.rank[a] < self.rank[b]:
self.tree[a] = b
else:
self.tree[b] = a
if self.rank[a] == self.rank[b]: self.rank[a] += 1
mod = 7 + 10 ** 9
def solve():
input = sys.stdin.readline
N = int(input())
P = [int(p) for p in input().split()]
unknown = P.count(-1)
totalPatt = pow(N-1, unknown, mod)
if unknown > 1: addPatt = pow(N-1, unknown - 1, mod)
else: addPatt = 1
parent = UFT(N)
ans = 0
for i, p in enumerate(P):
if parent.find(i) == parent.find(p-1): continue
elif p > -1:
ans += totalPatt % mod
ans %= mod
parent.unite(i, p-1)
for i, p in enumerate(P):
if p == -1:
for j in range(N):
if parent.find(i) != parent.find(j):
#print(i, j, parent.size[j])
size = parent.tree.count(parent.find(j))
ans += (size * addPatt) % mod
ans %= mod
parent.unite(i, j)
print(ans)
return 0
if __name__ == "__main__":
solve()
``` | instruction | 0 | 91,477 | 1 | 182,954 |
No | output | 1 | 91,477 | 1 | 182,955 |
Provide a correct Python 3 solution for this coding contest problem.
PCK Taxi in Aizu city, owned by PCK company, has adopted a unique billing system: the user can decide the taxi fare. Today as usual, many people are waiting in a queue at the taxi stand in front of the station.
In front of the station, there are $N$ parking spaces in row for PCK taxis, each with an index running from $1$ to $N$. Each of the parking areas is occupied by a taxi, and a queue of potential passengers is waiting for the ride. Each one in the queue has his/her own plan for how much to pay for the ride.
To increase the companyβs gain, the taxi driver is given the right to select the passenger who offers the highest taxi fare, rejecting others.
The driver in the $i$-th parking space can perform the following actions any number of times in any sequence before he finally selects a passenger and starts driving.
1. Offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue.
2. Reject to offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue. The passenger is removed from the queue.
3. Move to the $i + 1$-th parking area if it is empty. If he is in the $N$-th parking area, he leaves the taxi stand to cruise the open road.
A preliminary listening is made as to the fare the users offer. Your task is to maximize the sales volume of PCK Taxi in reference to the table of offered fares. A taxi cannot accommodate more than one passenger.
Given the number of taxi parking spaces and information regarding the persons waiting in the parking areas, calculate the maximum possible volume of sales.
Input
The input is given in the following format.
$N$
$s_1$
$s_2$
$...$
$s_N$
The first line provides the number of taxi parking areas $N$ ($1 \leq N \leq 300,000$). Each of the subsequent $N$ lines provides information on the customers queueing in the $i$-th taxi parking area in the following format:
$M$ $c_1$ $c_2$ ... $c_M$
The first integer $M$ ($1 \leq M \leq 300,000$) indicates the number of customers in the queue, and the subsequent array of integers $c_j$ ($1 \leq c_j \leq 10,000$) indicates the fare the $j$-th customer in the queue is willing to pay. The total number of customers in the taxi stand is equal to or less than $300,000$.
Output
Output the maximum volume of sales.
Example
Input
3
3 8 10 1
4 7 1 2 15
3 11 8 19
Output
45 | instruction | 0 | 91,643 | 1 | 183,286 |
"Correct Solution:
```
from heapq import heappush, heappop
def main():
n = int(input())
que = []
for i in range(n):
lst = sorted(list(map(int, input().split()))[1:], reverse=True)[:i+1]
heappush(que, lst[-1])
for v in lst[:-1]:
heappush(que, v)
heappop(que)
print(sum(que))
main()
``` | output | 1 | 91,643 | 1 | 183,287 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
PCK Taxi in Aizu city, owned by PCK company, has adopted a unique billing system: the user can decide the taxi fare. Today as usual, many people are waiting in a queue at the taxi stand in front of the station.
In front of the station, there are $N$ parking spaces in row for PCK taxis, each with an index running from $1$ to $N$. Each of the parking areas is occupied by a taxi, and a queue of potential passengers is waiting for the ride. Each one in the queue has his/her own plan for how much to pay for the ride.
To increase the companyβs gain, the taxi driver is given the right to select the passenger who offers the highest taxi fare, rejecting others.
The driver in the $i$-th parking space can perform the following actions any number of times in any sequence before he finally selects a passenger and starts driving.
1. Offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue.
2. Reject to offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue. The passenger is removed from the queue.
3. Move to the $i + 1$-th parking area if it is empty. If he is in the $N$-th parking area, he leaves the taxi stand to cruise the open road.
A preliminary listening is made as to the fare the users offer. Your task is to maximize the sales volume of PCK Taxi in reference to the table of offered fares. A taxi cannot accommodate more than one passenger.
Given the number of taxi parking spaces and information regarding the persons waiting in the parking areas, calculate the maximum possible volume of sales.
Input
The input is given in the following format.
$N$
$s_1$
$s_2$
$...$
$s_N$
The first line provides the number of taxi parking areas $N$ ($1 \leq N \leq 300,000$). Each of the subsequent $N$ lines provides information on the customers queueing in the $i$-th taxi parking area in the following format:
$M$ $c_1$ $c_2$ ... $c_M$
The first integer $M$ ($1 \leq M \leq 300,000$) indicates the number of customers in the queue, and the subsequent array of integers $c_j$ ($1 \leq c_j \leq 10,000$) indicates the fare the $j$-th customer in the queue is willing to pay. The total number of customers in the taxi stand is equal to or less than $300,000$.
Output
Output the maximum volume of sales.
Example
Input
3
3 8 10 1
4 7 1 2 15
3 11 8 19
Output
45
Submitted Solution:
```
n=int(input())
s=[]
fare=0
wait_list=[]
for i in range(n):
skeep=(list(map(int,input().split())))
del skeep[0]
s.append(skeep)
for i in range(n)[::-1]:
for j in s[i]:
wait_list.append(j)
fare+=max(wait_list)
wait_list.remove(max(wait_list))
if j%15000==0:
wait_list.sort()
wait_list=wait_list[len(wait_list)-n:]
print(fare)
``` | instruction | 0 | 91,644 | 1 | 183,288 |
No | output | 1 | 91,644 | 1 | 183,289 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
PCK Taxi in Aizu city, owned by PCK company, has adopted a unique billing system: the user can decide the taxi fare. Today as usual, many people are waiting in a queue at the taxi stand in front of the station.
In front of the station, there are $N$ parking spaces in row for PCK taxis, each with an index running from $1$ to $N$. Each of the parking areas is occupied by a taxi, and a queue of potential passengers is waiting for the ride. Each one in the queue has his/her own plan for how much to pay for the ride.
To increase the companyβs gain, the taxi driver is given the right to select the passenger who offers the highest taxi fare, rejecting others.
The driver in the $i$-th parking space can perform the following actions any number of times in any sequence before he finally selects a passenger and starts driving.
1. Offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue.
2. Reject to offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue. The passenger is removed from the queue.
3. Move to the $i + 1$-th parking area if it is empty. If he is in the $N$-th parking area, he leaves the taxi stand to cruise the open road.
A preliminary listening is made as to the fare the users offer. Your task is to maximize the sales volume of PCK Taxi in reference to the table of offered fares. A taxi cannot accommodate more than one passenger.
Given the number of taxi parking spaces and information regarding the persons waiting in the parking areas, calculate the maximum possible volume of sales.
Input
The input is given in the following format.
$N$
$s_1$
$s_2$
$...$
$s_N$
The first line provides the number of taxi parking areas $N$ ($1 \leq N \leq 300,000$). Each of the subsequent $N$ lines provides information on the customers queueing in the $i$-th taxi parking area in the following format:
$M$ $c_1$ $c_2$ ... $c_M$
The first integer $M$ ($1 \leq M \leq 300,000$) indicates the number of customers in the queue, and the subsequent array of integers $c_j$ ($1 \leq c_j \leq 10,000$) indicates the fare the $j$-th customer in the queue is willing to pay. The total number of customers in the taxi stand is equal to or less than $300,000$.
Output
Output the maximum volume of sales.
Example
Input
3
3 8 10 1
4 7 1 2 15
3 11 8 19
Output
45
Submitted Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
s=[0]*n
fare=[]
wait_list=[]
for i in range(n):
s[i]= [i for i in list(map(int,input().split()))[1:]]
for i in range(n)[::-1]:
wait_list+=s[i]
wait_list.sort()
fare.append(wait_list.pop(-1))
if len(wait_list)>i:
wait_list=wait_list[len(wait_list)-i:]
print(sum(fare))
``` | instruction | 0 | 91,645 | 1 | 183,290 |
No | output | 1 | 91,645 | 1 | 183,291 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
PCK Taxi in Aizu city, owned by PCK company, has adopted a unique billing system: the user can decide the taxi fare. Today as usual, many people are waiting in a queue at the taxi stand in front of the station.
In front of the station, there are $N$ parking spaces in row for PCK taxis, each with an index running from $1$ to $N$. Each of the parking areas is occupied by a taxi, and a queue of potential passengers is waiting for the ride. Each one in the queue has his/her own plan for how much to pay for the ride.
To increase the companyβs gain, the taxi driver is given the right to select the passenger who offers the highest taxi fare, rejecting others.
The driver in the $i$-th parking space can perform the following actions any number of times in any sequence before he finally selects a passenger and starts driving.
1. Offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue.
2. Reject to offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue. The passenger is removed from the queue.
3. Move to the $i + 1$-th parking area if it is empty. If he is in the $N$-th parking area, he leaves the taxi stand to cruise the open road.
A preliminary listening is made as to the fare the users offer. Your task is to maximize the sales volume of PCK Taxi in reference to the table of offered fares. A taxi cannot accommodate more than one passenger.
Given the number of taxi parking spaces and information regarding the persons waiting in the parking areas, calculate the maximum possible volume of sales.
Input
The input is given in the following format.
$N$
$s_1$
$s_2$
$...$
$s_N$
The first line provides the number of taxi parking areas $N$ ($1 \leq N \leq 300,000$). Each of the subsequent $N$ lines provides information on the customers queueing in the $i$-th taxi parking area in the following format:
$M$ $c_1$ $c_2$ ... $c_M$
The first integer $M$ ($1 \leq M \leq 300,000$) indicates the number of customers in the queue, and the subsequent array of integers $c_j$ ($1 \leq c_j \leq 10,000$) indicates the fare the $j$-th customer in the queue is willing to pay. The total number of customers in the taxi stand is equal to or less than $300,000$.
Output
Output the maximum volume of sales.
Example
Input
3
3 8 10 1
4 7 1 2 15
3 11 8 19
Output
45
Submitted Solution:
```
import sys
input = sys.stdin.readline
n=int(input())
s=[0]*n
fare=[]
wait_list=[]
for i in range(n):
s[i]= [i for i in list(map(int,input().split()))[1:]]
for i in range(n)[::-1]:
wait_list+=s[i]
wait_list.sort()
fare.append(wait_list.pop(-1))
wait_list=wait_list[max(0,len(wait_list))-i:]
print(sum(fare))
``` | instruction | 0 | 91,646 | 1 | 183,292 |
No | output | 1 | 91,646 | 1 | 183,293 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
PCK Taxi in Aizu city, owned by PCK company, has adopted a unique billing system: the user can decide the taxi fare. Today as usual, many people are waiting in a queue at the taxi stand in front of the station.
In front of the station, there are $N$ parking spaces in row for PCK taxis, each with an index running from $1$ to $N$. Each of the parking areas is occupied by a taxi, and a queue of potential passengers is waiting for the ride. Each one in the queue has his/her own plan for how much to pay for the ride.
To increase the companyβs gain, the taxi driver is given the right to select the passenger who offers the highest taxi fare, rejecting others.
The driver in the $i$-th parking space can perform the following actions any number of times in any sequence before he finally selects a passenger and starts driving.
1. Offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue.
2. Reject to offer a ride to the passenger who is at the head of the $i$-th parking spaceβs queue. The passenger is removed from the queue.
3. Move to the $i + 1$-th parking area if it is empty. If he is in the $N$-th parking area, he leaves the taxi stand to cruise the open road.
A preliminary listening is made as to the fare the users offer. Your task is to maximize the sales volume of PCK Taxi in reference to the table of offered fares. A taxi cannot accommodate more than one passenger.
Given the number of taxi parking spaces and information regarding the persons waiting in the parking areas, calculate the maximum possible volume of sales.
Input
The input is given in the following format.
$N$
$s_1$
$s_2$
$...$
$s_N$
The first line provides the number of taxi parking areas $N$ ($1 \leq N \leq 300,000$). Each of the subsequent $N$ lines provides information on the customers queueing in the $i$-th taxi parking area in the following format:
$M$ $c_1$ $c_2$ ... $c_M$
The first integer $M$ ($1 \leq M \leq 300,000$) indicates the number of customers in the queue, and the subsequent array of integers $c_j$ ($1 \leq c_j \leq 10,000$) indicates the fare the $j$-th customer in the queue is willing to pay. The total number of customers in the taxi stand is equal to or less than $300,000$.
Output
Output the maximum volume of sales.
Example
Input
3
3 8 10 1
4 7 1 2 15
3 11 8 19
Output
45
Submitted Solution:
```
n=int(input())
s=[]
fare=0
wait_list=[]
for i in range(n):
skeep=(list(map(int,input().split())))
del skeep[0]
s.append(skeep)
for i in range(n)[::-1]:
for j in s[i]:
wait_list.append(j)
fare+=max(wait_list)
wait_list.remove(max(wait_list))
while len(wait_list)>n:wait_list.remove(min(wait_list))
print(fare)
``` | instruction | 0 | 91,647 | 1 | 183,294 |
No | output | 1 | 91,647 | 1 | 183,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image> | instruction | 0 | 92,203 | 1 | 184,406 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n, a = map(int, input().split())
criminals = list(map(int, input().split()))
current = a - 1
distance = 1
sum = 0
if criminals[current] == 1:
sum = 1
while current + distance < n or current - distance >= 0:
if current + distance >= n:
if criminals[current - distance] == 1:
sum += 1
elif current - distance < 0:
if criminals[current + distance] == 1:
sum += 1
else:
if criminals[current - distance] == 1 and criminals[current + distance] == 1:
sum += 2
distance += 1
print(str(sum))
``` | output | 1 | 92,203 | 1 | 184,407 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image> | instruction | 0 | 92,204 | 1 | 184,408 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n,k=map(int,input().split())
c=list(map(int,input().split()))
tot=0
if c[k-1]==1:
tot+=1
pivot=k-1
left_pointer=k-1
right_pointer=k-1
while left_pointer>0 or right_pointer<n-1:
left_pointer-=1
right_pointer+=1
if left_pointer>=0 and right_pointer<=n-1:
if c[left_pointer]==c[right_pointer] and c[left_pointer]==1:
tot+=2
elif left_pointer<0 and right_pointer<=n-1:
if c[right_pointer]==1:
tot+=1
elif left_pointer>=0 and right_pointer>=n:
if c[left_pointer]==1:
tot+=1
else:
break
print(tot)
``` | output | 1 | 92,204 | 1 | 184,409 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image> | instruction | 0 | 92,205 | 1 | 184,410 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n,a=map(int,input().split())
a-=1
t=list(map(int,input().split()))
ans=0
for i in range(n):
if t[i]:
distance=i-a
j=a-distance
if j<0 or j>=n or t[i]==t[j]:
ans+=1
print(ans)
``` | output | 1 | 92,205 | 1 | 184,411 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image> | instruction | 0 | 92,206 | 1 | 184,412 |
Tags: constructive algorithms, implementation
Correct Solution:
```
# import sys
# sys.stdin = open("test.in","r")
# sys.stdout = open("test.out","w")
n,a=map(int,input().split())
b=list(map(int,input().split()))
c,d=a-2,a
e=sum(b)
while c>=0 and d<n:
e-=(b[c]+b[d])%2
c-=1
d+=1
print(e)
``` | output | 1 | 92,206 | 1 | 184,413 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image> | instruction | 0 | 92,207 | 1 | 184,414 |
Tags: constructive algorithms, implementation
Correct Solution:
```
from collections import defaultdict
if __name__ == "__main__":
#n, m = list(map(int, input().split()))
n, a = map(int, input().split())
A = list(map(int, input().split()))
ans, i = sum(A), 1
while a - 1 - i >= 0 and a - 1 + i <= n - 1:
if A[a - 1 - i] != A[a - 1 + i]:
ans -= 1
i += 1
print(ans)
``` | output | 1 | 92,207 | 1 | 184,415 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image> | instruction | 0 | 92,208 | 1 | 184,416 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n,a=map(int,input().split());b=[]
t=list(map(int,input().split()))
for i in range(n):
if t[i]==1:
b.append(abs(i+1-a))
r=0
for j in b:
if b.count(j)==2 :
r+=1
elif b.count(j)==1 and (a-1-j<0 or a-1+j>n-1):
r+=1
elif j==0:
r+=1
print(r)
``` | output | 1 | 92,208 | 1 | 184,417 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image> | instruction | 0 | 92,209 | 1 | 184,418 |
Tags: constructive algorithms, implementation
Correct Solution:
```
n, a = map(int, input().split())
A = list(map(int, input().split()))
i = 1
cnt = A[a - 1]
while a - 1 - i >= 0 or a - 1 + i <= n - 1:
x = a - 1 - i
y = a - 1 + i
if x >= 0 and y <= n - 1:
if A[x] and A[y]:
cnt += 2
elif x >= 0:
cnt += A[x]
else:
cnt += A[y]
i += 1
print(cnt)
``` | output | 1 | 92,209 | 1 | 184,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image> | instruction | 0 | 92,210 | 1 | 184,420 |
Tags: constructive algorithms, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
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()))
x,y=invr()
z=inlt()
limakindex=y-1
a=1
crimescaught=0
limakpos=z[limakindex]
if z[limakindex]==1:
crimescaught+=1
while (limakindex-a)>=0 and (limakindex+a)<=(len(z)-1):
if z[limakindex-a]+z[limakindex+a]==2:
crimescaught+=2
a+=1
else:
a+=1
if (limakindex-a)<=0 and not (limakindex+a)>=(len(z)-1):
while (limakindex+a)<=(len(z)-1):
if z[limakindex+a]==1:
crimescaught+=1
a+=1
else:
a+=1
elif (limakindex+a)>=(len(z)-1) and not (limakindex-a)<=0:
while (limakindex-a)>=0:
if z[limakindex-a]==1:
crimescaught+=1
a+=1
else:
a+=1
if len(z)==2:
crimescaught=sum(z)
print(crimescaught)
``` | output | 1 | 92,210 | 1 | 184,421 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image>
Submitted Solution:
```
#!/usr/bin/python3.5
n,k=map(int,input().split())
mas=[int(x) for x in input().split()]
k-=1
u=max(k,n-k-1)
u+=1
c=0
for i in range(u):
if i==0:
if mas[k]:
c+=1
else:
if k+i>=n:
if k-i>=0 and mas[k-i]:
c+=1
elif k-i<0:
if k+i<n and mas[k+i]:
c+=1
else:
if mas[k-i] and mas[k+i]:
c+=2
print(c)
``` | instruction | 0 | 92,211 | 1 | 184,422 |
Yes | output | 1 | 92,211 | 1 | 184,423 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image>
Submitted Solution:
```
def R(): return map(int, input().split())
def I(): return int(input())
def S(): return str(input())
def L(): return list(R())
from collections import Counter
import math
import sys
from itertools import permutations
import bisect
n,a=R()
ol=L()
cnt=0
for i in range(n):
if i>0 and a-i>=1 and a+i<=n:
cnt+=2*(ol[a-i-1]+ol[a+i-1]==2)
if i>0 and a-i<1 and a+i<=n:
cnt+=(ol[a+i-1]==1)
if i>0 and a-i>=1 and a+i>n:
cnt+=(ol[a-i-1]==1)
if i==0 and ol[a-1]==1:
cnt+=1
print(cnt)
``` | instruction | 0 | 92,212 | 1 | 184,424 |
Yes | output | 1 | 92,212 | 1 | 184,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image>
Submitted Solution:
```
n,a = [int(x) for x in input().split()]
t = [int(x) for x in input().split()]
a = a - 1
i,j = a - 1, a + 1
criminals = 0
if t[a] == 1:
criminals = 1
while(i > -1 and j < n ):
if t[i] == 1 and t[j] == 1:
criminals += 2
i -= 1
j += 1
while i > -1:
if t[i] == 1:
criminals += 1
i -= 1
while j < n:
if t[j] == 1:
criminals += 1
j += 1
print(criminals)
``` | instruction | 0 | 92,213 | 1 | 184,426 |
Yes | output | 1 | 92,213 | 1 | 184,427 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image>
Submitted Solution:
```
#n = int(input()[-2:])
n, m = map(int, input().split())
#s = input()
c = list(map(int, input().split()))
m -= 1
k = min(n - m, m + 1)
l = 0 + (c[m] == 1)
for i in range(1, k):
if c[m - i] == 1 and c[m + i] == 1:
l += 2
if k - 1 == m:
for i in range(m * 2 + 1, n):
if c[i] == 1:
l += 1
else:
for i in range(0, m - k + 1):
if c[i] == 1:
l += 1
print(l)
``` | instruction | 0 | 92,214 | 1 | 184,428 |
Yes | output | 1 | 92,214 | 1 | 184,429 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image>
Submitted Solution:
```
n1=input().split()
a=int(n1[1])-1
arr=input().split()
arr=map(int,arr)
arr=list(arr)
n=len(arr)
x=arr[:a+1]
x=len(x)
y=arr[a+1:]
y=len(y)
if len(arr)==1:
res =sum(arr)
else:
res=arr[a]
if x>y:
res=sum(arr[:a-y])
arr=arr[y:]
elif y>x:
res=sum(arr[abs(a + x):])
arr=arr[:n-x]
a=int(len(arr)/2 -1)
for i in range(1,a+1):
if arr[a-i]+arr[a+i]==2:
res +=2
print(res)
``` | instruction | 0 | 92,215 | 1 | 184,430 |
No | output | 1 | 92,215 | 1 | 184,431 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image>
Submitted Solution:
```
l=list(map(int,input().split()))
l1=list(map(int,input().split()))
k=l[1]-1
i=1
count=0
l=l[0]-1
if l1[k]==1:
count+=1
while k-i!=0 or k+i!=l:
if k-i<0 or k+i>l:
break
if l1[k-i]==1 and l1[k+i]==1:
count+=2
i+=1
if (k-i)>0:
count+=l1[:(k-i)+1].count(1)
if (k+i)<l:
count+=l1[k+i:].count(1)
print(count)
``` | instruction | 0 | 92,216 | 1 | 184,432 |
No | output | 1 | 92,216 | 1 | 184,433 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image>
Submitted Solution:
```
buff = input().split(" ")
n = int(buff[0])
a = int(buff[1])
t = input().split(" ")
res = 0
if n-a > a-1:
for i in range(n-a+1):
if t[(a+i-1)%n] == "1" and t[(a-i-1)%n] == "1":
if i == 0:
res += 1
else:
res += 2
else:
for i in range(a):
if t[(a+i-1)%n] == "1" and t[(a-i-1)%n] == "1":
if i == 0:
res+=1
else:
res+=2
print(res)
``` | instruction | 0 | 92,217 | 1 | 184,434 |
No | output | 1 | 92,217 | 1 | 184,435 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.
Limak is a police officer. He lives in a city a. His job is to catch criminals. It's hard because he doesn't know in which cities criminals are. Though, he knows that there is at most one criminal in each city.
Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.
You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.
Input
The first line of the input contains two integers n and a (1 β€ a β€ n β€ 100) β the number of cities and the index of city where Limak lives.
The second line contains n integers t1, t2, ..., tn (0 β€ ti β€ 1). There are ti criminals in the i-th city.
Output
Print the number of criminals Limak will catch.
Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.
<image>
Using the BCD gives Limak the following information:
* There is one criminal at distance 0 from the third city β Limak is sure that this criminal is exactly in the third city.
* There is one criminal at distance 1 from the third city β Limak doesn't know if a criminal is in the second or fourth city.
* There are two criminals at distance 2 from the third city β Limak is sure that there is one criminal in the first city and one in the fifth city.
* There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.
In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak's city. There is only one city at distance 2 so Limak is sure where a criminal is.
<image>
Submitted Solution:
```
n, a = map(int, input().split())
ar = list(map(int, input().split()))
a = a - 1
c = ar[a]
if n == 1:
print(ar[0])
exit(0)
b = min(n - a, a)
for i in range(1,b):
c += ((ar[a - i] & ar[a + i]) << 1)
for i in range(a - b):
c += ar[i]
for i in range(a + b, n):
c += ar[i]
print(c)
``` | instruction | 0 | 92,218 | 1 | 184,436 |
No | output | 1 | 92,218 | 1 | 184,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost.
There are n towns in the region, numbered from 1 to n. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts:
* Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique;
* Let li be the number of roads on the shortest path from town i to the capital, then li β₯ li - 1 holds for all 2 β€ i β€ n;
* For town i, the number of roads connected to it is denoted by di, which equals either 2 or 3.
You are to count the number of different ways in which the towns are connected, and give the answer modulo 109 + 7. Two ways of connecting towns are considered different if a pair (u, v) (1 β€ u, v β€ n) exists such there is a road between towns u and v in one of them but not in the other.
Input
The first line of input contains a positive integer n (3 β€ n β€ 50) β the number of towns.
The second line contains n space-separated integers d1, d2, ..., dn (2 β€ di β€ 3) β the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even.
Output
Output one integer β the total number of different possible ways in which the towns are connected, modulo 109 + 7.
Examples
Input
4
3 2 3 2
Output
1
Input
5
2 3 3 2 2
Output
2
Input
5
2 2 2 2 2
Output
2
Input
20
2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2
Output
82944
Note
In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1.
<image>
In the second example, the following two structures satisfy the constraints.
<image>
Submitted Solution:
```
n = input() #X is the rank of the current highest, each that rank disappear and the rank Y below become the new highest rank X
a = input() #Each turn the current first node on Z will try to connect to X and move into Y, no 2 nodes in X connect the same node
a = a.split(' ')#Strategy: Dynamic programming, there are 2 case, the first node in Z connect one of the nodes in X or every node in Z does not connect X
a = list(map(int,a))#After the second case happen, the nodes in X will connect each other to terminate X and replace X with Y
X = [a[0]] #The number in each node represent the number of nodes it can continue connecting so each time it connects, its number minus 1
Y = []
Z = a[1:]
def pair(x,y):
if y == 0:
if x == 0:
return 1
return (x-1)*pair(x-2,0)
if x == 0:
if y == 1 or y == 2:
return 0
return (y-1)*(y-2)*pair(2,y-3)/2
return y*pair(x,y-1) + (x-1)*pair(x-2,y)
def around(a):
t = 0
for i in a:
if i == 1:
t += 1
return pair(t,len(a)-t)
def tree(A,B,C): #the main function
if A == []:
if B == []:
return 1
return tree(B,[],C) #Move Y to X
elif sum(A) % 2 == 1:
if C == []: #In this case, contracdict
return 0
else: #Because the sum is odd, it cannot connect each other
t = 0
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
elif C == []: #reaching the end
return around(A)*tree(B,[],[])
else:
if B == []: #because Y is empty, the around function also can't happen
t = 0
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
t = around(A)*tree(B,[],C) #this is where the 2 cases happen
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
print(tree(X,Y,Z)) #Note: There's a lot of replication, you can use clear them out if you can
``` | instruction | 0 | 92,299 | 1 | 184,598 |
No | output | 1 | 92,299 | 1 | 184,599 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost.
There are n towns in the region, numbered from 1 to n. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts:
* Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique;
* Let li be the number of roads on the shortest path from town i to the capital, then li β₯ li - 1 holds for all 2 β€ i β€ n;
* For town i, the number of roads connected to it is denoted by di, which equals either 2 or 3.
You are to count the number of different ways in which the towns are connected, and give the answer modulo 109 + 7. Two ways of connecting towns are considered different if a pair (u, v) (1 β€ u, v β€ n) exists such there is a road between towns u and v in one of them but not in the other.
Input
The first line of input contains a positive integer n (3 β€ n β€ 50) β the number of towns.
The second line contains n space-separated integers d1, d2, ..., dn (2 β€ di β€ 3) β the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even.
Output
Output one integer β the total number of different possible ways in which the towns are connected, modulo 109 + 7.
Examples
Input
4
3 2 3 2
Output
1
Input
5
2 3 3 2 2
Output
2
Input
5
2 2 2 2 2
Output
2
Input
20
2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2
Output
82944
Note
In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1.
<image>
In the second example, the following two structures satisfy the constraints.
<image>
Submitted Solution:
```
n = input() #X is the rank of the current highest, each that rank disappear and the rank Y below become the new highest rank X
a = input() #Each turn the current first node on Z will try to connect to X and move into Y, no 2 nodes in X connect the same node
a = a.split(' ')#Strategy: Dynamic programming, there are 2 case, the first node in Z connect one of the nodes in X or every node in Z does not connect X
a = list(map(int,a))#After the second case happen, the nodes in X will connect each other to terminate X and replace X with Y
X = [a[0]] #The number in each node represent the number of nodes it can continue connecting so each time it connects, its number minus 1
Y = []
Z = a[1:]
def pair(x,y):
if y == 0:
if x == 0:
return 1
return (x-1)*pair(x-2,0)
if x == 0:
if y == 1:
return 0
return (y-1)*pair(2,y-2)
return y*pair(x,y-1) + (x-1)*pair(x-2,y)
def around(a):
t = 0
for i in a:
if i == 1:
t += 1
return pair(t,len(a)-t)
def tree(A,B,C): #the main function
if A == []:
if B == []:
return 1
return tree(B,[],C) #Move Y to X
elif sum(A) % 2 == 1:
if C == []: #In this case, contracdict
return 0
else: #Because the sum is odd, it cannot connect each other
t = 0
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
elif C == []: #reaching the end
return around(A)*tree(B,[],[])
else:
if B == []: #because Y is empty, the around function also can't happen
t = 0
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
t = around(A)*tree(B,[],C) #this is where the 2 cases happen
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
print(tree(X,Y,Z)) #Note: There's a lot of replication, you can use clear them out if you can
``` | instruction | 0 | 92,300 | 1 | 184,600 |
No | output | 1 | 92,300 | 1 | 184,601 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost.
There are n towns in the region, numbered from 1 to n. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts:
* Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique;
* Let li be the number of roads on the shortest path from town i to the capital, then li β₯ li - 1 holds for all 2 β€ i β€ n;
* For town i, the number of roads connected to it is denoted by di, which equals either 2 or 3.
You are to count the number of different ways in which the towns are connected, and give the answer modulo 109 + 7. Two ways of connecting towns are considered different if a pair (u, v) (1 β€ u, v β€ n) exists such there is a road between towns u and v in one of them but not in the other.
Input
The first line of input contains a positive integer n (3 β€ n β€ 50) β the number of towns.
The second line contains n space-separated integers d1, d2, ..., dn (2 β€ di β€ 3) β the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even.
Output
Output one integer β the total number of different possible ways in which the towns are connected, modulo 109 + 7.
Examples
Input
4
3 2 3 2
Output
1
Input
5
2 3 3 2 2
Output
2
Input
5
2 2 2 2 2
Output
2
Input
20
2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2
Output
82944
Note
In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1.
<image>
In the second example, the following two structures satisfy the constraints.
<image>
Submitted Solution:
```
n = input() #X is the rank of the current highest, each that rank disappear and the rank Y below become the new highest rank X
a = input() #Each turn the current first node on Z will try to connect to X and move into Y, no 2 nodes in X connect the same node
a = a.split(' ')#Strategy: Dynamic programming, there are 2 case, the first node in Z connect one of the nodes in X or every node in Z does not connect X
a = list(map(int,a))#After the second case happen, the nodes in X will connect each other to terminate X and replace X with Y
X = [a[0]] #The number in each node represent the number of nodes it can continue connecting so each time it connects, its number minus 1
Y = []
Z = a[1:]
def around(a): #The number of ways the nodes in X can connect each other, the sum of every nodes must be even
if a == []:
return 1
t = 0
if a[0] == 1:
for i in range(1,len(a)):
x = a[:]
if a[i] != 1:
x[i] -= 1
x = x[1:]
t += around(x)
return t
if len(a) >= 3:
for i in range(1, len(a)-1):
for j in range(i+1,len(a)):
x = a[:]
if x[j] != 1:
x[j] -= 1
if x[i] != 1:
x[i] -= 1
x = x[1:]
t += around(x)
return t
return 0
def tree(A,B,C): #the main function
if A == []:
if B == []:
return 1
return tree(B,[],C) #Move Y to X
elif sum(A) % 2 == 1:
if C == []: #In this case, contracdict
return 0
else: #Because the sum is odd, it cannot connect each other
t = 0
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] != 1:
x[i] -= 1
t += tree(x,E,D)
return t
elif C == []: #reaching the end
return around(A)*tree(B,[],[])
else:
if B == []: #because Y is empty, the around function also can't happen
t = 0
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] != 1:
x[i] -= 1
t += tree(x,E,D)
return t
t = around(A)*tree(B,[],C) #this is where the 2 cases happen
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] != 1:
x[i] -= 1
t += tree(x,E,D)
return t
print(tree(X,Y,Z)) #Note: There's a lot of replication, you can use clear them out if you can
``` | instruction | 0 | 92,301 | 1 | 184,602 |
No | output | 1 | 92,301 | 1 | 184,603 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost.
There are n towns in the region, numbered from 1 to n. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts:
* Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique;
* Let li be the number of roads on the shortest path from town i to the capital, then li β₯ li - 1 holds for all 2 β€ i β€ n;
* For town i, the number of roads connected to it is denoted by di, which equals either 2 or 3.
You are to count the number of different ways in which the towns are connected, and give the answer modulo 109 + 7. Two ways of connecting towns are considered different if a pair (u, v) (1 β€ u, v β€ n) exists such there is a road between towns u and v in one of them but not in the other.
Input
The first line of input contains a positive integer n (3 β€ n β€ 50) β the number of towns.
The second line contains n space-separated integers d1, d2, ..., dn (2 β€ di β€ 3) β the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even.
Output
Output one integer β the total number of different possible ways in which the towns are connected, modulo 109 + 7.
Examples
Input
4
3 2 3 2
Output
1
Input
5
2 3 3 2 2
Output
2
Input
5
2 2 2 2 2
Output
2
Input
20
2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2
Output
82944
Note
In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1.
<image>
In the second example, the following two structures satisfy the constraints.
<image>
Submitted Solution:
```
n = input() #X is the rank of the current highest, each that rank disappear and the rank Y below become the new highest rank X
a = input() #Each turn the current first node on Z will try to connect to X and move into Y, no 2 nodes in X connect the same node
a = a.split(' ')#Strategy: Dynamic programming, there are 2 case, the first node in Z connect one of the nodes in X or every node in Z does not connect X
a = list(map(int,a))#After the second case happen, the nodes in X will connect each other to terminate X and replace X with Y
X = [a[0]] #The number in each node represent the number of nodes it can continue connecting so each time it connects, its number minus 1
Y = []
Z = a[1:]
def pair(x,y):
if y == 0:
if x == 0:
return 1
return (x-1)*pair(x-2,0)
if x == 0:
if y == 1 or y == 2:
return 0
return (y-1)*int((y-2)/2)*pair(2,y-3)
return y*pair(x,y-1) + (x-1)*pair(x-2,y)
def around(a):
t = 0
for i in a:
if i == 1:
t += 1
return pair(t,len(a)-t)
def tree(A,B,C): #the main function
if A == []:
if B == []:
return 1
return tree(B,[],C) #Move Y to X
elif sum(A) % 2 == 1:
if C == []: #In this case, contracdict
return 0
else: #Because the sum is odd, it cannot connect each other
t = 0
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
elif C == []: #reaching the end
return around(A)*tree(B,[],[])
else:
if B == []: #because Y is empty, the around function also can't happen
t = 0
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
t = around(A)*tree(B,[],C) #this is where the 2 cases happen
E = B + [C[0]-1]
D = C[1:]
for i in range(len(A)):
x = A[:]
if x[i] == 1:
x = x[:i] + x[i+1:]
else:
x[i] -= 1
t += tree(x,E,D)
return t
print(tree(X,Y,Z)) #Note: There's a lot of replication, you can use clear them out if you can
``` | instruction | 0 | 92,302 | 1 | 184,604 |
No | output | 1 | 92,302 | 1 | 184,605 |
Provide a correct Python 3 solution for this coding contest problem.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None | instruction | 0 | 92,540 | 1 | 185,080 |
"Correct Solution:
```
import sys
from heapq import*
def g(s,g):
F=[1e7]*-~n;F[s]=0
H=[(0,s)]
while H:
c,u=heappop(H)
if u==g:return c
for f,v in E[u]:
t=c+f
if t<F[v]:
F[v]=t
heappush(H,(t,v))
return-1
for e in iter(input,'0 0'):
n,k=map(int,e.split())
E=[[]for _ in[0]*-~n]
for _ in[0]*k:
f=input()
if'0'==f[0]:print(g(*map(int,f[2:].split())))
else:
c,d,e=map(int,f[2:].split())
E[c]+=[(e,d)];E[d]+=[(e,c)]
``` | output | 1 | 92,540 | 1 | 185,081 |
Provide a correct Python 3 solution for this coding contest problem.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None | instruction | 0 | 92,541 | 1 | 185,082 |
"Correct Solution:
```
while 1:
INF = 10**9
N, K = map(int, input().split())
if N == K == 0:
break
E = [[INF]*N for i in range(N)]
for i in range(N):
E[i][i] = 0
ans = []
for i in range(K):
q, *c = map(int, input().split())
if q == 0:
a, b = c
cost = E[a-1][b-1]
if INF <= cost:
ans.append(-1)
else:
ans.append(cost)
else:
c, d, e = c
if e < E[c-1][d-1]:
E[c-1][d-1] = E[d-1][c-1] = e
Ec = E[c-1]; Ed = E[d-1]
for i in range(N):
Ei = E[i]
Eic = Ei[c-1] + e
Eid = Ei[d-1] + e
for j in range(i+1, N):
Ei[j] = E[j][i] = min(Ei[j], Eic + Ed[j], Eid + Ec[j])
print(*ans, sep='\n')
``` | output | 1 | 92,541 | 1 | 185,083 |
Provide a correct Python 3 solution for this coding contest problem.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None | instruction | 0 | 92,542 | 1 | 185,084 |
"Correct Solution:
```
import sys
from heapq import*
r=sys.stdin.readline
def g(E,n,s,g):
F=[1e7]*-~n;F[s]=0
H=[(0,s)]
while H:
c,u=heappop(H)
if u==g:return c
for f,v in E[u]:
t=c+f
if t<F[v]:F[v]=t;heappush(H,(t,v))
return-1
def s():
for e in iter(input,'0 0'):
n,k=map(int,e.split())
E=[[]for _ in[0]*-~n]
for _ in[0]*k:
f=[*map(int,r().split())]
if f[0]:
c,d,e=f[1:]
E[c]+=[(e,d)];E[d]+=[(e,c)]
else:print(g(E,n,*f[1:]))
if'__main__'==__name__:s()
``` | output | 1 | 92,542 | 1 | 185,085 |
Provide a correct Python 3 solution for this coding contest problem.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None | instruction | 0 | 92,543 | 1 | 185,086 |
"Correct Solution:
```
from heapq import*
def g(n,E,s,g):
F=[1e7]*-~n;F[s]=0
H=[(0,s)]
while H:
c,u=heappop(H)
if u==g:return c
for f,v in E[u]:
t=c+f
if t<F[v]:F[v]=t;heappush(H,(t,v))
return-1
def s():
for e in iter(input,'0 0'):
n,k=map(int,e.split())
E=[[]for _ in[0]*-~n]
for _ in[0]*k:
f=[*map(int,input().split())]
if f[0]:
c,d,e=f[1:]
E[c]+=[(e,d)];E[d]+=[(e,c)]
else:print(g(n,E,*f[1:]))
if'__main__'==__name__:s()
``` | output | 1 | 92,543 | 1 | 185,087 |
Provide a correct Python 3 solution for this coding contest problem.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None | instruction | 0 | 92,544 | 1 | 185,088 |
"Correct Solution:
```
def solve():
max_fare = 99000001
def dijkstra(start, goal):
unvisited = [i for i in range(n)]
fare = [max_fare] * n
fare[start] = 0
while True:
min_fare = max_fare
for i in unvisited:
f = fare[i]
if f < min_fare:
min_fare = f
u = i
if u == goal:
return min_fare
elif min_fare == max_fare:
return -1
unvisited.remove(u)
for v, f in adj[u]:
if v in unvisited:
fare[v] = min(min_fare + f, fare[v])
import sys
file_input = sys.stdin
while True:
n, k = map(int, file_input.readline().split())
if n == 0 and k == 0:
break
adj = [[] for i in range(n)]
for i in range(k):
line = file_input.readline()
if line[0] == '0':
a, b = map(int, line[2:].split())
print(dijkstra(a - 1, b - 1))
else:
c, d, e = map(int, line[2:].split())
adj[c - 1].append((d - 1, e))
adj[d - 1].append((c - 1, e))
solve()
``` | output | 1 | 92,544 | 1 | 185,089 |
Provide a correct Python 3 solution for this coding contest problem.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None | instruction | 0 | 92,545 | 1 | 185,090 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0526
TLE
"""
import sys
from sys import stdin
from heapq import heappush, heappop
input = stdin.readline
def dijkstra(s, G):
pq = []
d = [float('inf')] * len(G)
d[s] = 0
heappush(pq, (0, s))
while pq:
c, v = heappop(pq)
if d[v] < c:
continue
for t, c in G[v].items(): # ?????????v?????Β£??\??????????????Β±????????Β¨?????????????????Β°?????Β§?????????????????Β΄??Β°
if d[t] > d[v] + c: # ???????????Β§??\??Β£?????????????????????????????????v????????Β±????????????????????????????????????????????Β°???????????????????????Β΄??Β°
d[t] = d[v] + c
heappush(pq, (d[t], t)) # python???heapq??????????Β°???????????????????pop?????????????????????????????Β§???-1??????????????????
return d
def main(args):
while True:
n, k = map(int, input().split())
if n == 0 and k == 0:
break
d = [{} for _ in range(n+1)]
costs = [[float('inf')]*(n+1) for _ in range(n+1)]
updated = False
for _ in range(k):
data = [int(x) for x in input().split()]
if data[0] == 0:
f, t = data[1], data[2]
if updated:
costs[f] = dijkstra(f, d)
if costs[f][t] == float('inf'):
print(-1)
else:
print(costs[f][t])
else:
f, t, c = data[1], data[2], data[3]
if t in d[f]:
d[f][t] = min(d[f][t], c)
else:
d[f][t] = c
if f in d[t]:
d[t][f] = min(d[t][f], c)
else:
d[t][f] = c
updated = True
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 92,545 | 1 | 185,091 |
Provide a correct Python 3 solution for this coding contest problem.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None | instruction | 0 | 92,546 | 1 | 185,092 |
"Correct Solution:
```
from heapq import heappush, heappop, heapify
def dijkstra(s, t, links):
heap = list(links[s])
heapify(heap)
visited = set()
while heap:
fare, node = heappop(heap)
if node == t:
return fare
if node in visited:
continue
visited.add(node)
for fare2, node2 in links[node]:
if node2 not in visited:
heappush(heap, (fare + fare2, node2))
return -1
while True:
n, k = map(int, input().split())
if not n:
break
links = [set() for _ in range(n)]
for _ in range(k):
o = map(int, input().split())
if next(o):
c, d, e = o
c, d = c - 1, d - 1
links[c].add((e, d))
links[d].add((e, c))
else:
a, b = o
print(dijkstra(a - 1, b - 1, links))
``` | output | 1 | 92,546 | 1 | 185,093 |
Provide a correct Python 3 solution for this coding contest problem.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None | instruction | 0 | 92,547 | 1 | 185,094 |
"Correct Solution:
```
import sys
from heapq import heappush,heappop
r=sys.stdin.readline
def g(s,g):
F=[1e7]*-~n;F[s]=0
H=[(0,s)]
while H:
c,u=heappop(H)
if u==g:return c
for f,v in E[u]:
t=c+f
if t<F[v]:F[v]=t;heappush(H,(t,v))
return-1
for e in iter(input,'0 0'):
n,k=map(int,e.split())
E=[[]for _ in[0]*-~n]
for _ in[0]*k:
f=[*map(int,r().split())]
if f[0]:
c,d,e=f[1:]
E[c]+=[(e,d)];E[d]+=[(e,c)]
else:print(g(*f[1:]))
``` | output | 1 | 92,547 | 1 | 185,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None
Submitted Solution:
```
import sys
from heapq import*
r=sys.stdin.readline
def g(s,g):
F=[1e7]*-~n;F[s]=0
H=[(0,s)]
while H:
c,u=heappop(H)
if u==g:return c
for f,v in E[u]:
t=c+f
if t<F[v]:
F[v]=t
heappush(H,(t,v))
return-1
for e in iter(input,'0 0'):
n,k=map(int,e.split())
E=[[]for _ in[0]*-~n]
for _ in[0]*k:
f=[*map(int,r().split())]
if f[0]:
c,d,e=f[1:]
E[c]+=[(e,d)];E[d]+=[(e,c)]
else:print(g(*f[1:]))
``` | instruction | 0 | 92,548 | 1 | 185,096 |
Yes | output | 1 | 92,548 | 1 | 185,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None
Submitted Solution:
```
I=float('inf')
for e in iter(input,'0 0'):
n,k=map(int,e.split())
F=[[I]*-~n for _ in[0]*-~n]
for i in range(1,n+1):F[i][i]=0
for _ in[0]*k:
f=input();g=map(int,f[2:].split())
if'0'==f[0]:a,b=g;A=F[a][b];print([A,-1][A==I])
else:
c,d,e=g
if e<F[c][d]:
for i in range(2,n+1):
for j in range(1,i):
F[i][j]=min(F[i][j],F[i][c]+e+F[d][j],F[i][d]+e+F[c][j])
for j in range(2,n+1):
for i in range(1,j):
F[i][j]=F[j][i]
``` | instruction | 0 | 92,549 | 1 | 185,098 |
Yes | output | 1 | 92,549 | 1 | 185,099 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0526
TLE
"""
import sys
from sys import stdin
from heapq import heappush, heappop
from collections import namedtuple
input = stdin.readline
def warshall_floyd(d, V):
for k in range(V):
for i in range(V):
for j in range(V):
new_cost = d[i][k] + d[k][j]
if new_cost < d[i][j]:
d[i][j] = new_cost
def main1(args):
while True:
n, k = map(int, input().split())
if n == 0 and k == 0:
break
d = [[float('inf')] * (n+1) for _ in range(n+1)]
#for i in range(n+1):
# d[i][i] = 0
updated = False
for _ in range(k):
data = [int(x) for x in input().split()]
if data[0] == 0:
if updated:
warshall_floyd(d, n+1)
updated = False
f, t = data[1], data[2]
if d[f][t] == float('inf'):
print(-1)
else:
print(d[f][t])
else:
f, t, c = data[1], data[2], data[3]
if c < d[f][t]:
d[f][t] = c
updated = True
if c < d[t][f]:
d[t][f] = c
updated = True
edge = namedtuple('edge', ['t', 'c'])
def dijkstra(s, G):
pq = []
d = [float('inf')] * len(G)
d[s] = 0
heappush(pq, (0, s))
while pq:
c, v = heappop(pq)
if d[v] < c:
continue
for e in G[v]: # ?????????v?????Β£??\??????????????Β±????????Β¨?????????????????Β°?????Β§?????????????????Β΄??Β°
if d[e.t] > d[v] + e.c: # ???????????Β§??\??Β£?????????????????????????????????v????????Β±????????????????????????????????????????????Β°???????????????????????Β΄??Β°
d[e.t] = d[v] + e.c
heappush(pq, (d[e.t], e.t)) # python???heapq??????????Β°???????????????????pop?????????????????????????????Β§???-1??????????????????
return d
def main(args):
while True:
n, k = map(int, input().split())
if n == 0 and k == 0:
break
d = [[] for _ in range(n+1)]
costs = [[float('inf')]*(n+1) for _ in range(n+1)]
updated = False
for _ in range(k):
data = [int(x) for x in input().split()]
if data[0] == 0:
f, t = data[1], data[2]
if updated:
costs[f] = dijkstra(f, d)
if costs[f][t] == float('inf'):
print(-1)
else:
print(costs[f][t])
else:
f, t, c = data[1], data[2], data[3]
d[f].append(edge(t, c))
d[t].append(edge(f, c))
updated = True
if __name__ == '__main__':
main(sys.argv[1:])
``` | instruction | 0 | 92,550 | 1 | 185,100 |
Yes | output | 1 | 92,550 | 1 | 185,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None
Submitted Solution:
```
# AOJ 0526: Boat Travel
# Python3 2018.7.1 bal4u
INF = 0x7fffffff
while True:
n, k = map(int, input().split())
if n == 0: break
fee = [[INF for j in range(101)] for i in range(101)]
for i in range(n+1): fee[i][i] = 0
for i in range(k):
v = list(map(int, input().split()))
a, b = v[1], v[2]
if v[0] == 1:
e = v[3]
if fee[a][b] > e:
fee[a][b], fee[b][a] = e, e
for c in range(1, n+1):
for d in range(c+1, n+1):
t = fee[c][a] + e + fee[b][d]
if t < fee[c][d]: fee[c][d], fee[d][c] = t, t
t = fee[c][b] + e + fee[a][d]
if t < fee[c][d]: fee[c][d], fee[d][c] = t, t;
else:
e = fee[a][b]
if e >= INF: e = -1
print(e)
``` | instruction | 0 | 92,551 | 1 | 185,102 |
Yes | output | 1 | 92,551 | 1 | 185,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None
Submitted Solution:
```
INF = 1000000000
def solve(s,g,E,n):
D = [0 if i == s else INF for i in range(n)]
U = [0 for i in range(n)]
V = [s]
while V:
v = V.pop(0)
U[v] = 1
if v == g:
break
for e in E[v]:
if not U[e[0]] and D[v] + e[1] < D[e[0]]:
D[e[0]] = D[v] + e[1]
V.append(e[0])
V = sorted(V,key=lambda x:D[x])
if D[g] >= INF:
print(-1)
else:
print(D[g])
while True:
try:
n,k = list(map(int,input().split()))
if n == 0 and k == 0:
break
E = [[] for i in range(n)]
for i in range(k):
lst = list(map(int,input().split()))
a = min(lst[1],lst[2])
b = max(lst[1],lst[2])
if lst[0] == 0:
solve(a - 1, b - 1, E, n)
elif lst[0] == 1:
E[a - 1].append([b - 1, lst[3]])
E[b - 1].append([a - 1, lst[3]])
except EOFError:
break
``` | instruction | 0 | 92,552 | 1 | 185,104 |
No | output | 1 | 92,552 | 1 | 185,105 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None
Submitted Solution:
```
inf = 10**6+1
def warshall_floyd():
global n
global cost
for i in range(n):
for j in range(n):
for k in range(n):
if cost[i][j] > cost[j][k]+cost[k][i]:
cost[i][j] = cost[j][k]+cost[k][i]
while True:
n,k = map(int, input().split())
if (n|k)==0: break
flag = True
cost = [[inf]*n for i in range (n)]
for inline in range(k):
query = list(map(int, input().split()))
if query[0]:
cost[query[1]-1][query[2]-1] = min(cost[query[1]-1][query[2]-1], query[3])
cost[query[2]-1][query[1]-1] = cost[query[1]-1][query[2]-1]
flag = True
else:
if flag: warshall_floyd()
mon = cost[query[1]-1][query[2]-1]
print(mon if mon != inf else -1)
flag = False
``` | instruction | 0 | 92,553 | 1 | 185,106 |
No | output | 1 | 92,553 | 1 | 185,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None
Submitted Solution:
```
INF = 1000000000
def solve(s,g,E,n):
D = [INF for i in range(n)]
D[s] = 0
V = [s]
while V:
v = V.pop(0)
for e in E[v]:
if D[v] + e[1] < D[e[0]]:
D[e[0]] = D[v] + e[1]
V.append(e[0])
if D[g] >= INF:
print(-1)
else:
print(D[g])
while True:
try:
n,k = list(map(int,input().split()))
if n == 0 and k == 0:
break
E = [[] for i in range(n)]
for i in range(k):
lst = list(map(int,input().split()))
a = min(lst[1],lst[2])
b = max(lst[1],lst[2])
if lst[0] == 0:
solve(a - 1, b - 1, E, n)
elif lst[0] == 1:
E[a - 1].append([b - 1, lst[3]])
E[b - 1].append([a - 1, lst[3]])
except EOFError:
break
``` | instruction | 0 | 92,554 | 1 | 185,108 |
No | output | 1 | 92,554 | 1 | 185,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
There are n islands in JOI, and each island is numbered from 1 to n. Currently, the JOI country is developing a route network connecting each island.
You work at a ticket center that handles ship tickets. There are many people in JOI who want to travel between islands by boat, as cheaply as possible, and they fill out the order form with their origin and destination, and they are at your place. Will be sent to.
Your job is to transfer several vessels as soon as you receive the order form from the customer, calculate the cheapest fare on the route connecting the departure point and the destination, and tell the customer. However, depending on the itinerary, it may not be possible to travel by ship. At that time, it is necessary to tell the customer that "I cannot travel by ship". Also, in JOI, new vessels connecting the islands are starting to operate one after another, and you will be informed of this information from time to time. When replying to customers, you must keep in mind the latest information.
Create a program that asks for a reply to the customer when the customer's order form or operation information of the newly started vessel is given as input.
The execution status of Input Example 1 and Output Example 1 is shown in Fig. 1.
input
The input consists of multiple datasets. Each dataset is given in the following format.
Two integers n, k (1 β€ n β€ 100, 1 β€ k β€ 5000) are written on the first line of the input. This means that the number of islands is n and the input consists of k + 1 lines. On the first line of i + (1 β€ i β€ k), three or four integers are written separated by blanks.
* When the first number is 0, this line represents the customer's order form.
Three integers 0, a, b (1 β€ a β€ n, 1 β€ b β€ n, a β b) are written on this line, separated by blanks. This means that the customer has sent an order form with island a as the starting point and island b as the destination.
* When the first number is 1, this line represents the operation information of the newly started vessel.
This line contains four integers 1, c, d, e (1 β€ c β€ n, 1 β€ d β€ n, c β d, 1 β€ e β€ 1000000).
This means that a vessel that goes back and forth between island c and island d has newly started operation, and the fare from island c to island d and the fare from island d to island c are both e.
The order form after this line must be answered in consideration of this vessel.
At the first stage, it is assumed that no vessel is in operation. Of the inputs, the number of lines representing ship operation information is 1000 or less. Also note that multiple vessels may operate between islands.
When both n and k are 0, it indicates the end of input. The number of data sets does not exceed 5.
output
Output in the following format for each data set.
Let m be the number of lines representing the order form in the input. The output of each dataset consists of m lines, and on the i-th line (1 β€ i β€ m), an integer representing the reply to the i-th order form is written. That is, if it is possible to travel between the departure point and the destination of the i-th order form by connecting several vessels, write the minimum value of the total fare. If it is impossible to travel, output -1.
Examples
Input
3 8
1 3 1 10
0 2 3
1 2 3 20
1 1 2 5
0 3 2
1 1 3 7
1 2 1 9
0 2 3
5 16
1 1 2 343750
1 1 3 3343
1 1 4 347392
1 1 5 5497
1 2 3 123394
1 2 4 545492
1 2 5 458
1 3 4 343983
1 3 5 843468
1 4 5 15934
0 2 1
0 4 1
0 3 2
0 4 2
0 4 3
0 5 3
0 0
Output
-1
15
12
5955
21431
9298
16392
24774
8840
Input
None
Output
None
Submitted Solution:
```
INF = 1000000000
def solve(s,g,E,n):
D = [INF for i in range(n)]
D[s] = 0
V = [(0,s)]
U = [0 for i in range(n)]
while V:
v = V.pop(0)
U[v[1]] = 1
for e in E[v[1]]:
if U[e[0]] == 0 and D[v[1]] + e[1] < D[e[0]]:
D[e[0]] = D[v[1]] + e[1]
V.append((D[e[0]],e[0]))
V.sort()
if D[g] >= INF:
print(-1)
else:
print(D[g])
while True:
try:
n,k = list(map(int,input().split()))
if n == 0 and k == 0:
break
E = [[] for i in range(n)]
for i in range(k):
lst = list(map(int,input().split()))
if lst[0] == 0:
solve(lst[1] - 1, lst[2] - 1, E, n)
elif lst[0] == 1:
E[lst[1] - 1].append([lst[2] - 1, lst[3]])
E[lst[2] - 1].append([lst[1] - 1, lst[3]])
except EOFError:
break
``` | instruction | 0 | 92,555 | 1 | 185,110 |
No | output | 1 | 92,555 | 1 | 185,111 |
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 | 92,991 | 1 | 185,982 |
Tags: combinatorics, implementation, math
Correct Solution:
```
from sys import stdin
def c2 (x):
return x * (x - 1)
def gcd (p, q):
if q == 0:
return p
return gcd(q, p % q)
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
arr.sort()
denom = n * (n - 1)
ans = 0
for i in range(n):
L = i
R = n - i - 1
add = 0
add += 2 * c2(L)
add += 2 * L
add += L
add -= R
add -= 2 * c2(R)
ans += add * arr[i]
gc = gcd(ans, denom)
ans //= gc
denom //= gc
print(ans, denom)
``` | output | 1 | 92,991 | 1 | 185,983 |
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 | 92,992 | 1 | 185,984 |
Tags: combinatorics, implementation, math
Correct Solution:
```
from fractions import Fraction
n=int(input())
a=[int(i) for i in input().split()]
a=sorted(a)
s=[]
s.append(0)
for i in range(n):
s.append(s[-1]+a[i])
ans=0
for i in range(1,n+1):
ans+=s[n]-s[i-1]-(n-i+1)*a[i-1]
ans=ans*2+sum(a)
ans=Fraction(ans,n)
print("{0:d} {1:d}".format(ans.numerator,ans.denominator))
``` | output | 1 | 92,992 | 1 | 185,985 |
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 | 92,993 | 1 | 185,986 |
Tags: combinatorics, implementation, math
Correct Solution:
```
import math
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)
k = 0
s1 = 0
s2 = 0
for i in a:
s2 += s1 - k * i
s1 += i
k += 1
s2 *= 2
s2 += sum(a)
gcd = math.gcd(s2, n)
print(s2 // gcd, n // gcd)
``` | output | 1 | 92,993 | 1 | 185,987 |
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 | 92,994 | 1 | 185,988 |
Tags: combinatorics, implementation, math
Correct Solution:
```
import math
def read_int():
return int(input().strip())
def read_ints():
return list(map(int, input().strip().split(' ')))
def solve():
"""
a[i] -> a[j]
(n-1)!*|a[i]-a[j]| for all j
(n-1)!*(for i=1..n, for j=1..n sum(|a[i]-a[j]|))
/
n!
"""
n = read_int()
a = read_ints()
a.sort()
R = sum(abs(a[i]-a[0]) for i in range(1, n))
L = 0
S = R+sum(a)
# a[0]+a[1]+...+a[n-1]
# (a[0]-a[0])+(a[1]-a[0])+(a[2]-a[0])+...+(a[n-1]-a[0])
# (a[1]-a[0])+(a[1]-a[1])+(a[2]-a[1])+...+(a[n-1]-a[1])
# (a[2]-a[0])+(a[2]-a[1])+(a[2]-a[2])+...+(a[n-1]-a[2])
# S-n*a[0]
# S-a[0]
for i in range(1, n):
L += (a[i]-a[i-1])*i
R -= (a[i]-a[i-1])*(n-i)
S += (L+R)
gcd = math.gcd(S, n)
print(S//gcd, n//gcd)
if __name__ == '__main__':
solve()
``` | output | 1 | 92,994 | 1 | 185,989 |
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 | 92,995 | 1 | 185,990 |
Tags: combinatorics, implementation, math
Correct 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))
``` | output | 1 | 92,995 | 1 | 185,991 |
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 | 92,996 | 1 | 185,992 |
Tags: combinatorics, implementation, math
Correct Solution:
```
#!/usr/bin/env python3
def read_string():
return input()
def read_strings(return_type = iter, split = None, skip = 0):
return return_type(input().split(split)[skip:])
def read_lines(height, return_type = iter):
return return_type(read_string() for i in range(height))
def read_number():
return int(input())
def read_numbers(return_type = iter, skip = 0):
return return_type(int(i) for i in input().split()[skip:])
def read_values(*types, array = None):
line = input().split()
result = []
for return_type, i in zip(types, range(len(types))):
result.append(return_type(line[i]))
if array != None:
array_type, array_contained = array
result.append(array_type(array_contained(i) for i in line[len(types):]))
return result
def read_array(item_type = int, return_type = iter, skip = 0):
return return_type(item_type(i) for i in input().split()[skip:])
def read_martix(height, **args):
return_type = args["return_type"] if "return_type" in args else iter
return_type_inner = args["return_type_inner"] if "return_type_inner" in args else return_type
return_type_outer = args["return_type_outer"] if "return_type_outer" in args else return_type
item_type = args["item_type"] if "item_type" in args else int
return return_type_outer(read_array(item_type = item_type, return_type = return_type_inner) for i in range(height))
def read_martix_linear(width, skip = 0, item_type = int, skiped = None):
num = read_array(item_type = item_type, skip = skip)
height = len(num) / width
return [num[i * width: (i + 1) * width] for i in range(height)]
def gcd(n, m):
if m == 0:
return n
return gcd(m, n % m)
def main():
n = read_number()
arr = sorted(read_numbers(list))
sum_x = sum(arr)
sum_xy = 0
for i in range(1, n):
sum_xy += (arr[i] - arr[i - 1]) * i * (n - i)
nu = sum_x + sum_xy * 2
de = n
g = gcd(nu, de)
nu //= g
de //= g
print(nu, de)
if __name__ == '__main__':
main()
``` | output | 1 | 92,996 | 1 | 185,993 |
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 | 92,997 | 1 | 185,994 |
Tags: combinatorics, implementation, math
Correct Solution:
```
from fractions import *
def solve():
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans1 = 0
pres = a[0]
for i in range(1,n):
ans1+=i*a[i] -pres
pres+=a[i]
ans1 *= 2;
tmp = pres+ans1
tmp1 = n
s = Fraction(tmp,tmp1)
print(s.numerator,s.denominator)
solve()
``` | output | 1 | 92,997 | 1 | 185,995 |
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 | 92,998 | 1 | 185,996 |
Tags: combinatorics, implementation, math
Correct Solution:
```
from fractions import *
n=int(input());
a=list(map(int,input().split()));
a=sorted(a);
now=a[0];
ans=0;
for i in range(1,n):
ans+=i*a[i]-now;
now=now+a[i];
ans=ans*2+now;
s=Fraction(ans,n);
print(s.numerator,s.denominator);
``` | output | 1 | 92,998 | 1 | 185,997 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.