message stringlengths 2 48.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 318 108k | cluster float64 8 8 | __index_level_0__ int64 636 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried.
Mr. Kobouβs ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 Γ 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as "#" the two series of numbers become "0,2,2,1,1" and "1,1,1,3".
| 0| 2| 2| 1| 1
---|---|---|---|---|---
1| | | #| |
1| | #| | |
1| | | | | #
3| | #| #| #|
Mr. Kobouβs ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 Γ 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information.
Given the set of information written on the pieces of paper, make a program to judge if the information is relevant.
Input
The input is given in the following format.
$W$ $H$
$a_1$ $a_2$ $...$ $a_W$
$b_1$ $b_2$ $...$ $b_H$
The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$).
Output
Output "1" if the information written on the paper is relevant, or "0" otherwise.
Examples
Input
5 4
0 2 2 1 1
1 1 1 3
Output
1
Input
5 5
3 2 3 0 0
4 2 0 0 2
Output
0 | instruction | 0 | 87,447 | 8 | 174,894 |
"Correct Solution:
```
input()
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
ans = 0
A.sort(reverse=True)
for a in A:
B.sort(reverse=True)
for i in range(a):
B[i] -= 1
if min(B) < 0:
ans = 0
break
if max(B) == 0:
ans = 1
print(ans)
``` | output | 1 | 87,447 | 8 | 174,895 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17 | instruction | 0 | 87,669 | 8 | 175,338 |
Tags: dp, shortest paths
Correct Solution:
```
# BHUPAT JANGID (codechef id : bhupat2000) (codeforces id : bhupat2000)
# linkedin id : https://www.linkedin.com/in/bhupat-jangid-1b7b53170
import sys
from heapq import heapify, heappop, heappush
from itertools import *
from collections import *
from math import *
#import collection => Counter deque OrderedDict defaultdict
sys.setrecursionlimit(10 ** 6)
# f = open('input.txt')
# f.close()
input = lambda: sys.stdin.readline() # f.readline()
inp = lambda: int(input())
nm = lambda: map(int, input().split())
arr = lambda: list(nm())
INF = int(1e18)
mod = int(1e9) + 7 # 998244353
def find(dp,n):
while dp[n]>0:
n=dp[n]
return n
def solve():
#d1=deque()
#d=defaultdict(list)
n,m=nm()
lst1=arr()
lst2=arr()
dp1=[0]*(n)
dp2=[0]*n
for i in range(n-1):
if i==0:
dp1[i+1]=lst1[i]
dp2[i+1]=lst2[i]+m
else:
dp1[i+1]=lst1[i]+min(dp1[i],dp2[i])
dp2[i+1]=lst2[i]+min(dp1[i]+m,dp2[i])
for i in range(n):
print(min(dp1[i],dp2[i]),end=" ")
t = 1#inp()
for i in range(t):
solve()
``` | output | 1 | 87,669 | 8 | 175,339 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17 | instruction | 0 | 87,670 | 8 | 175,340 |
Tags: dp, shortest paths
Correct Solution:
```
n,c=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
dp=[[0]*2for i in range(n+1)]
print(0,end=" ")
for i in range(n-1):
if i>0:
dp[i][0]=min(dp[i-1][0]+y[i],dp[i-1][1]+y[i]+c)
dp[i][1]=min(dp[i-1][0],dp[i-1][1])+x[i]
else:
dp[0][0]=y[0]+c
dp[0][1]=x[0]
print(min(dp[i][0],dp[i][1]),end=" ")
``` | output | 1 | 87,670 | 8 | 175,341 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17 | instruction | 0 | 87,671 | 8 | 175,342 |
Tags: dp, shortest paths
Correct Solution:
```
#hint: https://codeforces.com/blog/entry/70779
inp = lambda : map(int, input().split())
n, c = inp()
a = list(inp())
b = list(inp())
ans = list()
ans.append(0)
arr = [[1000000000,10000000000] for i in range(n)]
arr[0][0] = 0
arr[0][1] = c
for i in range(n-1):
arr[i+1][0] = min(arr[i+1][0], arr[i][0] + a[i])
arr[i+1][0] = min(arr[i+1][0], arr[i][1] + a[i])
arr[i+1][1] = min(arr[i+1][1], arr[i][1] + b[i])
arr[i+1][1] = min(arr[i+1][1], arr[i][0] + b[i] + c)
ans = []
for i in range(n):
ans.append(min(arr[i][0], arr[i][1]))
print(*ans)
``` | output | 1 | 87,671 | 8 | 175,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17 | instruction | 0 | 87,672 | 8 | 175,344 |
Tags: dp, shortest paths
Correct Solution:
```
n,c=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
dp=[[0,c] for i in range(n)]
for i in range(1,n):
dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i-1]
dp[i][1]=min(dp[i-1][0]+c+b[i-1],dp[i-1][1]+b[i-1])
ans=[]
for i in range(n):
ans.append(min(dp[i][0],dp[i][1]))
print (*ans)
``` | output | 1 | 87,672 | 8 | 175,345 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17 | instruction | 0 | 87,673 | 8 | 175,346 |
Tags: dp, shortest paths
Correct Solution:
```
"""
NTC here
"""
from sys import stdin, setrecursionlimit
setrecursionlimit(10**7)
def iin(): return int(stdin.readline())
def lin(): return list(map(int, stdin.readline().split()))
# range = xrange
# input = raw_input
def main():
n,c=lin()
s=lin()
e=lin()
sol=[[0,0] for i in range(n)]
sol[0]=[c+e[0],s[0]]
for i in range(1,n-1):
sol[i]=[e[i]+min(sol[i-1][0], c+sol[i-1][1]),min(sol[i-1][0],sol[i-1][1])+s[i]]
ans=[0]+[min(sol[i]) for i in range(n-1)]
print(*ans)
main()
# try:
# main()
# except Exception as e: print(e)
``` | output | 1 | 87,673 | 8 | 175,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17 | instruction | 0 | 87,674 | 8 | 175,348 |
Tags: dp, shortest paths
Correct Solution:
```
import sys
input = lambda :sys.stdin.readline().rstrip('\r\n')
from math import log,ceil
from collections import defaultdict
n,c = map(int,input().split())
# 0 for currently in stairs
# 1 for in the elevator
dp = [[float('inf'),float('inf')] for _ in range(n)]
dp[0][0] = 0
dp[0][1] = c
a = [0]+[int(x) for x in input().split()]
b = [0]+[int(x) for x in input().split()]
for i in range(1,n):
dp[i][0] = min(dp[i][0],dp[i-1][0]+a[i])
dp[i][0] = min(dp[i][0],dp[i-1][1]+a[i])
dp[i][1] = min(dp[i][1],dp[i-1][1]+b[i])
dp[i][1] = min(dp[i][1],dp[i-1][0]+b[i]+c)
# print(dp[i])
print(*[min(x[0],x[1]) for x in dp])
``` | output | 1 | 87,674 | 8 | 175,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17 | instruction | 0 | 87,675 | 8 | 175,350 |
Tags: dp, shortest paths
Correct Solution:
```
n,c=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=[0]
INF=1e18
dp=[[INF,INF] for _ in range(n)]
dp[1][0]=a[0]
dp[1][1]=b[0]+c
ans.append(min(dp[1]))
for i in range(1,n-1):
temp=0
dp[i+1][0]=min(dp[i+1][0],dp[i][0]+a[i])
dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + a[i])
dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + b[i]+c)
dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + b[i] )
ans.append(min(dp[i+1]))
print(*ans)
``` | output | 1 | 87,675 | 8 | 175,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17 | instruction | 0 | 87,676 | 8 | 175,352 |
Tags: dp, shortest paths
Correct Solution:
```
R = lambda:list(map(int,input().split()))
n, c = R()
stair = R()
elevator = R()
ans, dp_of_stair, dp_of_elevator = [], [0], [c]
for i in range(n-1):
dp_of_stair.append(min(dp_of_stair[-1] + stair[i], dp_of_elevator[-1]+ stair[i]))
dp_of_elevator.append(min(dp_of_stair[-2] + elevator[i] + c, dp_of_elevator[-1]+ elevator[i]))
for i in range(n):
ans.append(min(dp_of_stair[i], dp_of_elevator[i]))
print(' '.join([str(x) for x in ans]))
``` | output | 1 | 87,676 | 8 | 175,353 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
Submitted Solution:
```
n, c = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
stair = 0
lift = c
print(0)
for i in range(n-1):
stair = min(stair+a[i], lift+b[i])
lift = min(stair+c, lift+b[i])
print(stair)
``` | instruction | 0 | 87,677 | 8 | 175,354 |
Yes | output | 1 | 87,677 | 8 | 175,355 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
Submitted Solution:
```
n, c = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [[0]*2 for i in range(n)]
dp[0][0] = 0
dp[0][1] = c
for i in range(n-1):
dp[i+1][0] = min(dp[i][0] + a[i], dp[i][1] + a[i])
dp[i+1][1] = min(dp[i][0] + b[i] + c, dp[i][1] + b[i])
ans = [0]*n
for i in range(n):
print(min(dp[i][0],dp[i][1]),end=" ")
``` | instruction | 0 | 87,678 | 8 | 175,356 |
Yes | output | 1 | 87,678 | 8 | 175,357 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
Submitted Solution:
```
from sys import stdin,stdout
for _ in range(1):#int(stdin.readline())):
# n=int(stdin.readline())
n,c=list(map(int,stdin.readline().split()))
a=list(map(int,stdin.readline().split()))
b=list(map(int,stdin.readline().split()))
stairs,lift=0,c
for i in range(n-1):
print(min(stairs,lift),end=' ')
stairs,lift=min(stairs+a[i],lift+b[i]),min(lift+b[i],stairs+c+a[i])
print(min(stairs, lift), end=' ')
``` | instruction | 0 | 87,679 | 8 | 175,358 |
Yes | output | 1 | 87,679 | 8 | 175,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
Submitted Solution:
```
import sys
input = sys.stdin.readline
def main():
N, C = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
dp = [0] * 2
ans = 0
print(ans, end=" ")
is_use_elev = False
dp[0] = A[0]
dp[1] = B[0] + C
print(min(dp[0], dp[1]), end=" ")
for i in range(1, N - 1):
x = min(dp[0], dp[1]) + A[i]
y = min(dp[0] + C, dp[1]) + B[i]
print(min(x, y), end=" ")
dp[0] = x
dp[1] = y
if __name__ == '__main__':
main()
``` | instruction | 0 | 87,680 | 8 | 175,360 |
Yes | output | 1 | 87,680 | 8 | 175,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
Submitted Solution:
```
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = [0 for i in range(n)]
flag = [0 for i in range(n)]
for i in range(0,n-1):
if flag == 1:
if a[i] < b[i]:
c[i+1] = a[i] + c[i]
flag[i+1] = 0
else:
c[i+1] = b[i] + c[i]
flag[i+1] = 1
else:
if a[i] < b[i]+k:
c[i+1] = a[i] + c[i]
flag[i+1] = 0
else:
c[i+1] = b[i] + k + c[i]
flag[i+1] = 1
if flag[i] == 0 and i > 0:
if b[i-1] + b[i] + c[i-1] + k < c[i+1]:
c[i+1] = b[i-1] + b[i] + c[i-1] + k
flag[i] = 1
for i in c:
print(i,end = " ")
``` | instruction | 0 | 87,681 | 8 | 175,362 |
No | output | 1 | 87,681 | 8 | 175,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
Submitted Solution:
```
a, b = list(map(int, input().split()))
stairs = list(map(int, input().split()))
lift = list(map(int, input().split()))
dp = [[0, 0] for i in range(a)]
dp[0] = [0, 0]
t = 0
v = 0
for i in range(1, a):
if dp[i - 1][1] == 0:
if dp[t][0] + v + lift[i - 1] < stairs[i - 1] + dp[i - 1][0] and dp[t][0] + v + lift[i - 1] < lift[i - 1] + dp[i - 1][0] + b and t != 0:
dp[i][0] = dp[t][0] + v + lift[i - 1]
dp[i][1] = 1
v = 0
elif lift[i - 1] + b > stairs[i - 1]:
dp[i][1] = 0
v += lift[i - 1]
dp[i][0] = dp[i - 1][0] + stairs[i - 1]
else:
dp[i][1] = 1
t = i - 1
dp[i][0] = dp[i - 1][0] + min(lift[i - 1] + b, stairs[i - 1])
else:
k1 = min((lift[i - 1]), stairs[i - 1])
if lift[i - 1] > stairs[i - 1]:
dp[i][1] = 0
v += lift[i - 1]
else:
dp[i][1] = 1
t = i
dp[i][0] = dp[i - 1][0] + min(lift[i - 1], stairs[i - 1])
for i in range(len(dp)):
print(dp[i][0], end = ' ')
``` | instruction | 0 | 87,682 | 8 | 175,364 |
No | output | 1 | 87,682 | 8 | 175,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
Submitted Solution:
```
n, c = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
time = [0] * n
level = 1
fromElevator = False
while level < n:
e = c
if fromElevator:
e = 0
stairs = time[level-1] + a[level - 1]
elevator = time[level-1] + b[level - 1] + e
if elevator <= stairs:
time[level] = elevator
fromElevator = True
else:
time[level] = stairs
fromElevator = False
level += 1
print(' '.join([str(x) for x in time]))
``` | instruction | 0 | 87,683 | 8 | 175,366 |
No | output | 1 | 87,683 | 8 | 175,367 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are planning to buy an apartment in a n-floor building. The floors are numbered from 1 to n from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.
Let:
* a_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs;
* b_i for all i from 1 to n-1 be the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator, also there is a value c β time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!).
In one move, you can go from the floor you are staying at x to any floor y (x β y) in two different ways:
* If you are using the stairs, just sum up the corresponding values of a_i. Formally, it will take β_{i=min(x, y)}^{max(x, y) - 1} a_i time units.
* If you are using the elevator, just sum up c and the corresponding values of b_i. Formally, it will take c + β_{i=min(x, y)}^{max(x, y) - 1} b_i time units.
You can perform as many moves as you want (possibly zero).
So your task is for each i to determine the minimum total time it takes to reach the i-th floor from the 1-st (bottom) floor.
Input
The first line of the input contains two integers n and c (2 β€ n β€ 2 β
10^5, 1 β€ c β€ 1000) β the number of floors in the building and the time overhead for the elevator rides.
The second line of the input contains n - 1 integers a_1, a_2, ..., a_{n-1} (1 β€ a_i β€ 1000), where a_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the stairs.
The third line of the input contains n - 1 integers b_1, b_2, ..., b_{n-1} (1 β€ b_i β€ 1000), where b_i is the time required to go from the i-th floor to the (i+1)-th one (and from the (i+1)-th to the i-th as well) using the elevator.
Output
Print n integers t_1, t_2, ..., t_n, where t_i is the minimum total time to reach the i-th floor from the first floor if you can perform as many moves as you want.
Examples
Input
10 2
7 6 18 6 16 18 1 17 17
6 9 3 10 9 1 10 1 5
Output
0 7 13 18 24 35 36 37 40 45
Input
10 1
3 2 3 1 3 3 1 4 1
1 2 3 4 4 1 2 1 3
Output
0 2 4 7 8 11 13 14 16 17
Submitted Solution:
```
n, c = list(map(int, input().split()))
n = n - 1
a, b = [0] * n, [0] * n
a = list(map(int, input().split()))
b = list(map(int, input().split()))
el, tm = False, 0
print(0, end=" ")
for x in range(n):
if el == False:
if a[x] >= b[x] + c:
tm += b[x] + c
el = True
else:
tm += a[x]
else:
if a[x] >= b[x]:
tm += b[x]
else:
tm += a[x]
el = False
print(tm, end=" ")
``` | instruction | 0 | 87,684 | 8 | 175,368 |
No | output | 1 | 87,684 | 8 | 175,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. | instruction | 0 | 87,899 | 8 | 175,798 |
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
from sys import stdin
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
a = sorted([(a[x], x) for x in range(n*2)])
group = {}
for x,ind in a:
if x in group:
group[x].append(ind)
else:
group[x] = [ind]
g2 = []
for x in group:
g2.append([len(group[x]), group[x]])
g2.sort()
left = 0
right = 0
board = [0 for x in range(n*2)]
ind = 0
for x,l in g2:
if x == 1:
ind += 1
if left <= right:
left += 1
board[l[0]] = '1'
else:
right += 1
board[l[0]] = '2'
else:
break
if right > left:
turn = True
else:
turn = False
for x,l in g2[ind:]:
left += 1
right += 1
if x%2 == 1:
last = l.pop()
if turn:
board[last] = '1'
else:
board[last] = '2'
turn = not turn
for n in l[::2]:
board[n] = '1'
for n in l[1::2]:
board[n] = '2'
print(left*right)
print(' '.join(board))
``` | output | 1 | 87,899 | 8 | 175,799 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. | instruction | 0 | 87,900 | 8 | 175,800 |
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
R = range(100)
c = [[] for _ in [0]*100]
for i in range(n*2):
c[a[i]].append(i)
d = [0]*200
heap = 1
z = [0, 0, 0]
for i in R:
if len(c[i]) == 1:
z[heap]+=1
d[c[i][0]] = heap
heap = 3 - heap;
for i in R:
if len(c[i]) > 1:
z[1]+=1
z[2]+=1
while len(c[i]):
d[c[i].pop()] = heap
heap = 3 - heap
print(z[1]*z[2])
print(' '.join(map(str, d[:n*2])))
# Made By Mostafa_Khaled
``` | output | 1 | 87,900 | 8 | 175,801 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. | instruction | 0 | 87,901 | 8 | 175,802 |
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
from collections import defaultdict as defdict
n = int(input())
d = defdict(int)
a = list(map(int, input().split()))
for i in a:
d[i] += 1
b = set(i for i in a if d[i] > 1)
c = [i for i in a if d[i] == 1]
##print('b =', b)
mx = len(b) + (len(c) // 2)
d = defdict(int)
answ = [None] * (n * 2)
cnt_diff_1 = 0
cnt_diff_2 = 0
for i, x in enumerate(a):
if cnt_diff_1 < mx:
if x in b:
if d[x] == 0:
answ[i] = '1'
d[x] += 1
cnt_diff_1 += 1
for i, x in enumerate(a):
if cnt_diff_1 < mx:
if x not in b:
answ[i] = '1'
cnt_diff_1 += 1
##print(d)
cnt1 = cnt_diff_1
cnt2 = 0
##print(cnt_diff_1)
##print(answ)
##print(mx)
for i, x in enumerate(a):
if answ[i] == '1': continue
if x in b:
if d[x] == 0:
answ[i] = '2'
d[x] = 2
cnt_diff_2 += 1
cnt2 += 1
elif d[x] == 1:
answ[i] = '2'
d[x] += 1
cnt_diff_2 += 1
cnt2 += 1
else:
if cnt1 < n:
answ[i] = '1'
cnt1 += 1
else:
answ[i] = '2'
cnt2 += 1
else:
if cnt2 < n:
answ[i] = '2'
cnt2 += 1
cnt_diff_2 += 1
else:
answ[i] = '1'
cnt1 += 1
## print('cnt2 =', cnt2)
## print(cnt_diff_2)
## print('cnt1 =', cnt1)
cnt_diff_1 += 1
##print(cnt_diff_1, cnt_diff_2)
##print()
##cnt1 = len(set(a[i] for i in range(2 * n) if answ[i] == '1'))
##cnt2 = len(set(a[i] for i in range(2 * n) if answ[i] == '2'))
##print(cnt1, cnt2)
print(cnt_diff_1 * cnt_diff_2)
print(' '.join(answ))
``` | output | 1 | 87,901 | 8 | 175,803 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. | instruction | 0 | 87,902 | 8 | 175,804 |
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split()))
g=[[] for i in range(100)]
for i in range(2*n):
g[arr[i]].append(i)
x=0
y=0
curr=1
r=[]
for i in range(10,100):
if len(g[i])==1:
arr[g[i][0]]=curr
if curr==1:
x+=1
else:
y+=1
curr=3-curr
if len(g[i])>1:
arr[g[i][0]]=1
arr[g[i][1]]=2
x+=1
y+=1
for j in range(2,len(g[i])):
r.append(g[i][j])
for i in range(len(r)):
arr[r[i]]=2-(i%2)
print(x*y)
print(*arr)
``` | output | 1 | 87,902 | 8 | 175,805 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. | instruction | 0 | 87,903 | 8 | 175,806 |
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
from math import*
from random import*
n = int(input()) * 2
A = list(map(int, input().split()))
amount = [0] * 101
B = []
for i in range(n):
if amount[A[i]] < 2:
amount[A[i]] += 1
B += [(A[i], i)]
B.sort()
x, y = [], []
for i in range(len(B)):
if(i % 2 == 0):
x.append(B[i][1])
else:
y.append(B[i][1])
lolka = 0
aaa = 0
# print(x)
# print(y)
print(len(x) * len(y))
for i in range(n):
if i in x:
lolka += 1
aaa += 1
print(1, end = ' ')
elif i in y:
print(2, end = ' ')
else:
if len(x) - lolka + aaa < n // 2:
print(1, end = ' ')
aaa += 1
else:
print(2, end = ' ')
print()
# B, C = [], []
# for i in range(n):
# S = list(set(A))
# where = [0] * 101
# am1, am2 = 0, 0
# for i in range(len(S)):
# if(i % 2 == 0):
# where[S[i]] = 1
# am1 += 1
# else:
# where[S[i]] = 2
# am2 += 1
# used = [0] * 201
# for i in range(n):
# if not used[A[i]]:
# print(where[A[i]])
# used[A[i]] = True
# else:
# print(3 - where[A[i]])
``` | output | 1 | 87,903 | 8 | 175,807 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. | instruction | 0 | 87,904 | 8 | 175,808 |
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
c = [0] * 100
for i in a:
c[i] += 1
x = [0] * 100
y = [0] * 100
j = 0
for i in range(100):
if c[i] == 1:
[x, y][j][i] += 1
j = 1 - j
for i in range(100):
if c[i] != 1:
x[i] += c[i] // 2
y[i] += c[i] // 2
if c[i] % 2 == 1:
[x, y][j][i] += 1
j = 1 - j
xk = len(list(filter(lambda it: it, x)))
yk = len(list(filter(lambda it: it, y)))
print(xk * yk)
zs = [None] * (2*n)
for i in range(2*n):
if x[a[i]] > 0:
x[a[i]] -= 1
zs[i] = 1
else:
assert y[a[i]] > 0
y[a[i]] -= 1
zs[i] = 2
print(*zs)
``` | output | 1 | 87,904 | 8 | 175,809 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. | instruction | 0 | 87,905 | 8 | 175,810 |
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
from collections import *
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
for i in range(2*n):
a[i] = [a[i],i]
a.sort()
ans = [0 for i in range(2*n)]
da, db = {}, {}
f = 0
for i in range(2*n):
if(c[a[i][0]] == 1): continue
ans[a[i][1]] = f + 1
if(f&1):
da[a[i][0]] = 1
else:
db[a[i][0]] = 1
f ^= 1
for i in range(2*n):
if(ans[a[i][1]] != 0): continue
ans[a[i][1]] = f+1
if(f&1):
da[a[i][0]] = 1
else:
db[a[i][0]] = 1
f ^= 1
print(len(da)*len(db))
print(*ans)
``` | output | 1 | 87,905 | 8 | 175,811 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345. | instruction | 0 | 87,906 | 8 | 175,812 |
Tags: combinatorics, constructive algorithms, greedy, implementation, math, sortings
Correct Solution:
```
n, t = 2 * int(input()), map(int, input().split())
d = [[] for i in range(100)]
for i, j in enumerate(t):
d[j].append(i)
y, x = [], False
p, q = [], ['1'] * n
for i in d[10: 100]:
if i:
if len(i) == 1:
if x: y.append(i[0])
x = not x
else:
y.append(i[0])
p += i[2: ]
k, l = len(p), len(y)
print(l * (n - k - l))
for i in y:
q[i] = '2'
for i in (p[k // 2: ] if x else p[: k // 2]):
q[i] = '2'
print(' '.join(q))
``` | output | 1 | 87,906 | 8 | 175,813 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
N = int(input())
Nums = list(map(int, input().split()))
Good = [1] * (N * 2)
Amounts = [0] * 100
Mono, Duo = 0, 0
for Num in Nums:
if Amounts[Num] == 0:
Mono += 1
elif Amounts[Num] == 1:
Duo += 1
Mono -= 1
Amounts[Num] += 1
Flag = Mono % 2
Duo_Flag = False
Counts = [0] * 100
for i in range(2 * N):
Num = Nums[i]
if Amounts[Num] == 1:
if Flag:
Good[i] = 1
else:
Good[i] = 2
Flag = not Flag
else:
if Counts[Num] == 0:
Good[i] = 1
elif Counts[Num] == 1:
Good[i] = 2
else:
if Duo_Flag:
Good[i] = 1
else:
Good[i] = 2
Duo_Flag = not Duo_Flag
Counts[Num] += 1
print((Duo + (Mono // 2)) * (Duo + ((Mono + 1) // 2)))
print(*Good)
``` | instruction | 0 | 87,907 | 8 | 175,814 |
Yes | output | 1 | 87,907 | 8 | 175,815 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n = 2 * int(input())
t = list(map(int, input().split()))
q, p, c = [0] * n, [0] * 100, [0, 0]
for i in t:
p[i] += 1
for i in p:
if i: c[i == 1] += 1
k = c[1] // 2
print((c[0] + k) * (c[0] + c[1] - k))
r = sorted([(p[j], j, i) for i, j in enumerate(t)])
for i in r[0: n: 2]:
q[i[2]] = '1'
for i in r[1: n: 2]:
q[i[2]] = '2'
print(' '.join(q))
``` | instruction | 0 | 87,908 | 8 | 175,816 |
Yes | output | 1 | 87,908 | 8 | 175,817 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
#!/usr/bin/python3
def readln(): return tuple(map(int, input().split()))
n, = readln()
b = readln()
cnt = [0] * 101
for v in b:
cnt[v] += 1
f = []
s = []
x = []
y = []
for i in range(10, 101):
if cnt[i] > 1:
f.extend([i] * (cnt[i] // 2))
s.extend([i] * (cnt[i] // 2))
if cnt[i] % 2:
y.append(i)
cnt[i] = 0
elif cnt[i] == 1:
x.append(i)
f.extend(x[:len(x)//2])
f.extend(y[:n - len(f)])
s.extend(x[len(x)//2:])
if len(s) < n:
s.extend(y[-n + len(s):])
print(len(set(f)) * len(set(s)));
ans = [1] * 2 * n
for v in s:
for i in range(2 * n):
if b[i] == v and ans[i] == 1:
ans[i] = 2
break
print(*ans)
``` | instruction | 0 | 87,909 | 8 | 175,818 |
Yes | output | 1 | 87,909 | 8 | 175,819 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
def s():
input()
l = [[]for _ in range(100)]
a = list(map(int,input().split()))
for i,v in enumerate(a):
l[v].append(i)
c = 0
cc = 0
fs = [0,0]
for i in l:
if len(i) == 0:
continue
if len(i) == 1:
a[i[0]] = c+1
fs[c]+=1
c = 1 - c
continue
fs[c] += 1
fs[c-1] += 1
for e in i[:len(i)//2]:
a[e] = 1 + cc
for e in i[len(i)//2:]:
a[e] = 2 - cc
if len(i) % 2 == 1:
cc = 1 - cc
print(fs[0]*fs[1])
print(*a)
s()
``` | instruction | 0 | 87,910 | 8 | 175,820 |
Yes | output | 1 | 87,910 | 8 | 175,821 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n = int(input())
set1 = set()
set2 = set()
res = [0] * (2 * n)
S = list(map(int, input().split()))
#A = [[S[i], i] for i in range(2 * n)]
c = 0
for i in sorted([[S[i], i] for i in range(2 * n)]):
if c == 0:
set1.add(i[0])
res[i[1]] = 1
c = 1
else:
set2.add(i[0])
res[i[1]] = 2
c = 0
print(len(set1) * len(set2))
print(' '.join(map(str, res)))
``` | instruction | 0 | 87,911 | 8 | 175,822 |
No | output | 1 | 87,911 | 8 | 175,823 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n=int(input())
arr=list(map(int,input().split()))
flip2=0
la=0
lb=0
a={}
b={}
for i in range(2*n):
if arr[i] in a and arr[i] in b:
if flip2==0 and la<n:
a[arr[i]]+=1
arr[i]="1"
flip2=1
la+=1
else:
b[arr[i]]+=1
arr[i]="2"
flip2=0
lb+=1
elif la<=lb:
if arr[i] in b:
a[arr[i]]=1
arr[i]="1"
la+=1
else:
if arr[i] in a:
if lb<n:
b[arr[i]]=1
arr[i]="2"
lb+=1
else:
a[arr[i]]+=1
arr[i]="1"
la+=1
else:
a[arr[i]]=1
arr[i]="1"
la+=1
else:
if arr[i] in a:
b[arr[i]]=1
arr[i]="2"
lb+=1
else:
if arr[i] in b:
if la<n:
a[arr[i]]=1
arr[i]="1"
la+=1
else:
b[arr[i]]+=1
arr[i]="2"
lb+=1
else:
b[arr[i]]=1
arr[i]="2"
lb+=1
print(len(a)*len(b))
print(" ".join(arr))
``` | instruction | 0 | 87,912 | 8 | 175,824 |
No | output | 1 | 87,912 | 8 | 175,825 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
first = []
second = []
ans = []
d = {}
for i in list(set(a)):
d[i] = i
for i in a:
if d[i] % 2 == 0:
first.append(i)
ans.append(1)
else:
second.append(i)
ans.append(2)
d[i] += 1
#if len(first) != len(second):
# exit(1)
print(len(list(set(first))) * len(list(set(second))))
print(*ans)
``` | instruction | 0 | 87,913 | 8 | 175,826 |
No | output | 1 | 87,913 | 8 | 175,827 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valera has 2Β·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap.
Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer β the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.
Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?
Input
The first line contains integer n (1 β€ n β€ 100). The second line contains 2Β·n space-separated integers ai (10 β€ ai β€ 99), denoting the numbers on the cubes.
Output
In the first line print a single number β the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2Β·n numbers bi (1 β€ bi β€ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division.
If there are multiple optimal ways to split the cubes into the heaps, print any of them.
Examples
Input
1
10 99
Output
1
2 1
Input
2
13 24 13 45
Output
4
1 2 2 1
Note
In the first test case Valera can put the first cube in the first heap, and second cube β in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.
In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345.
Submitted Solution:
```
n = int(input())
heap_1 = set()
heap_2 = set()
arr = list(map(int, input().split()))
ans = [0] * (2 * n)
for i in range(2 * n):
new = arr[i]
if new not in heap_1 and new not in heap_2:
if len(heap_1) > len(heap_2):
heap_2.add(new)
ans[i] = 2
else:
heap_1.add(new)
ans[i] = 1
elif new not in heap_1 and new in heap_2:
heap_1.add(new)
ans[i] = 1
elif new not in heap_2 and new in heap_1:
heap_2.add(new)
ans[i] = 2
print(len(heap_1) * len(heap_2))
print(*ans)
``` | instruction | 0 | 87,914 | 8 | 175,828 |
No | output | 1 | 87,914 | 8 | 175,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai β€ bi).
Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
Input
The first line of the input contains one integer n (2 β€ n β€ 100 000) β number of cola cans.
The second line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 109) β volume of remaining cola in cans.
The third line contains n space-separated integers that b1, b2, ..., bn (ai β€ bi β€ 109) β capacities of the cans.
Output
Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes).
You can print each letter in any case (upper or lower).
Examples
Input
2
3 5
3 6
Output
YES
Input
3
6 8 9
6 10 12
Output
NO
Input
5
0 0 5 0 0
1 1 8 10 5
Output
YES
Input
4
4 1 0 3
5 2 2 3
Output
YES
Note
In the first sample, there are already 2 cans, so the answer is "YES".
Submitted Solution:
```
n = int(input())
cola = list(map(int, input().split()))
volume = list(map(int, input().split()))
volume.sort()
if volume[-1] + volume[-2] >= sum(cola):
print('YES')
else:
print('NO')
``` | instruction | 0 | 88,075 | 8 | 176,150 |
Yes | output | 1 | 88,075 | 8 | 176,151 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai β€ bi).
Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
Input
The first line of the input contains one integer n (2 β€ n β€ 100 000) β number of cola cans.
The second line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 109) β volume of remaining cola in cans.
The third line contains n space-separated integers that b1, b2, ..., bn (ai β€ bi β€ 109) β capacities of the cans.
Output
Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes).
You can print each letter in any case (upper or lower).
Examples
Input
2
3 5
3 6
Output
YES
Input
3
6 8 9
6 10 12
Output
NO
Input
5
0 0 5 0 0
1 1 8 10 5
Output
YES
Input
4
4 1 0 3
5 2 2 3
Output
YES
Note
In the first sample, there are already 2 cans, so the answer is "YES".
Submitted Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [tuple(map(int, l.split())) for l in sys.stdin]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n = I()
a = LI()
b = LI()
s = sum(a)
t = sorted(b)
if t[-1] + t[-2] >= s:
return 'YES'
return 'NO'
print(main())
``` | instruction | 0 | 88,077 | 8 | 176,154 |
Yes | output | 1 | 88,077 | 8 | 176,155 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai β€ bi).
Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
Input
The first line of the input contains one integer n (2 β€ n β€ 100 000) β number of cola cans.
The second line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 109) β volume of remaining cola in cans.
The third line contains n space-separated integers that b1, b2, ..., bn (ai β€ bi β€ 109) β capacities of the cans.
Output
Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes).
You can print each letter in any case (upper or lower).
Examples
Input
2
3 5
3 6
Output
YES
Input
3
6 8 9
6 10 12
Output
NO
Input
5
0 0 5 0 0
1 1 8 10 5
Output
YES
Input
4
4 1 0 3
5 2 2 3
Output
YES
Note
In the first sample, there are already 2 cans, so the answer is "YES".
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=sum(a)
y=max(b)
b.remove(y)
z=max(b)
if y+z<x:
print('NO')
else:
print('YES')
``` | instruction | 0 | 88,078 | 8 | 176,156 |
Yes | output | 1 | 88,078 | 8 | 176,157 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai β€ bi).
Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!
Input
The first line of the input contains one integer n (2 β€ n β€ 100 000) β number of cola cans.
The second line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 109) β volume of remaining cola in cans.
The third line contains n space-separated integers that b1, b2, ..., bn (ai β€ bi β€ 109) β capacities of the cans.
Output
Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes).
You can print each letter in any case (upper or lower).
Examples
Input
2
3 5
3 6
Output
YES
Input
3
6 8 9
6 10 12
Output
NO
Input
5
0 0 5 0 0
1 1 8 10 5
Output
YES
Input
4
4 1 0 3
5 2 2 3
Output
YES
Note
In the first sample, there are already 2 cans, so the answer is "YES".
Submitted Solution:
```
number_of_cans = int(input())
volume = list(map(int, input().split()))
capacity = list(map(int, input().split()))
volume_of_all = 0
for volume_N in volume:
volume_of_all+=volume_N
max_capacity = capacity[0]
for counter in range(1,len(capacity)):
if capacity[counter] > capacity[counter-1]:
max_capacity = capacity[counter]
capacity.remove(max_capacity)
max_capacity2 = capacity[0]
for counter in range(1,len(capacity)):
if capacity[counter] > capacity[counter-1]:
max_capacity2 = capacity[counter]
if volume_of_all <= (max_capacity + max_capacity2):
print('YES')
else:
print('NO')
``` | instruction | 0 | 88,081 | 8 | 176,162 |
No | output | 1 | 88,081 | 8 | 176,163 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6. | instruction | 0 | 88,356 | 8 | 176,712 |
Tags: dfs and similar, graphs
Correct Solution:
```
# 1027D
import collections
def do():
n = int(input())
costs = [int(c) for c in input().split(" ")]
next = [int(c)-1 for c in input().split(" ")]
ind = [0] * n
for i,c in enumerate(next):
if i != c:
ind[c] += 1
seen = [0] * n
res_seen = [0] * n
res = 0
def get(entry):
m = costs[entry]
p = entry
while True:
if res_seen[p]:
return 0
res_seen[p] = 1
m = min(m, costs[p])
p = next[p]
if p == entry:
break
return m
def dfs(i):
stack = [i]
seen[i] = 1
while stack:
cur = stack.pop()
nei = next[cur]
if seen[nei]:
return get(nei)
seen[nei] = 1
stack.append(nei)
return 0
for i in range(n):
if ind[i] == 0:
res += dfs(i)
for i in range(n):
if seen[i] == 0:
res += dfs(i)
return res
print(do())
``` | output | 1 | 88,356 | 8 | 176,713 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6. | instruction | 0 | 88,357 | 8 | 176,714 |
Tags: dfs and similar, graphs
Correct Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(input())
C = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [a-1 for a in A]
visit = [False]*n
loops = []
for i in range(n):
if not visit[i]:
s = [i]
temp = set()
temp.add(i)
flag = False
while s:
v = s.pop()
if visit[A[v]]:
break
if A[v] in temp:
flag = True
p = A[v]
break
else:
s.append(A[v])
temp.add(A[v])
if flag:
loop = [p]
nv = A[p]
cnt = 0
while nv != p:
loop.append(nv)
nv = A[nv]
loops.append(loop)
for v in temp:
visit[v] = True
#print(loops)
ans = 0
for l in loops:
m = 10**18
for i in l:
m = min(m, C[i])
ans += m
print(ans)
``` | output | 1 | 88,357 | 8 | 176,715 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6. | instruction | 0 | 88,358 | 8 | 176,716 |
Tags: dfs and similar, graphs
Correct Solution:
```
def count(n,c,a):
vis=[0]*(n+1)
z=[]
p=[]
for i in range(1,n+1):
x=i
while vis[x]==0:
vis[x]=i
x=a[x-1]
if vis[x]==i:
p.append(x)
return p
def opium(n,c,a):
sum=0
p=list(set(count(n,c,a)))
#print(p)
for x in p:
v=x
cd=c[x-1]
while v!=a[x-1]:
x=a[x-1]
cd=min(cd,c[x-1])
sum=sum+cd
#print(sum)
return sum
def main():
n=int(input())
c=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
return opium(n,c,a)
print(main())
``` | output | 1 | 88,358 | 8 | 176,717 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6. | instruction | 0 | 88,359 | 8 | 176,718 |
Tags: dfs and similar, graphs
Correct Solution:
```
n=int(input())
c=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=0
v=[False for i in range(n)]
for i in range(n):
if v[i]:continue
p=set()
pl=[]
s=set([i])
t=True
while s and t:
x=s.pop()
v[x]=True
p.add(x)
pl.append(x)
nex=a[x]-1
s.add(nex)
if nex in p:
j=pl.index(nex)
za=s.pop()
elif v[nex]:t=False;za=s.pop()
if not t:continue
an=float("INF")
for k in range(j,len(pl)):
an=min(c[pl[k]],an)
ans+=an
print(ans)
``` | output | 1 | 88,359 | 8 | 176,719 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6. | instruction | 0 | 88,360 | 8 | 176,720 |
Tags: dfs and similar, graphs
Correct Solution:
```
#yeh dil maange more
n = int(input())
c = [0]+(list(map(int,input().split())))
a = [0]+(list(map(int,input().split())))
vis = [0] * (n+1)
ans = 0
for i in range(1,n+1):
x = i
while vis[x] == 0:
vis[x] = i
x = a[x]
if vis[x] != i:
continue
v = x
mn = c[x]
#print("v = ",v," mn = ",mn)
while a[x] != v:
x = a[x]
#print("x = ",x," a[x] = ",a[x])
mn = min(mn,c[x])
ans+=mn
#print(vis)
print(ans)
``` | output | 1 | 88,360 | 8 | 176,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6. | instruction | 0 | 88,361 | 8 | 176,722 |
Tags: dfs and similar, graphs
Correct Solution:
```
n = int(input())
cost = [0] + [int(item) for item in input().split()]
route = [0] + [int(r) for r in input().split()]
visited = [0] + [False for i in range(n)]
ans = 0
for i in range(1,n+1):
if visited[i] == True:
continue
circle = []
node = i
while visited[node] == False:
visited[node] = True
circle.append(node)
node = route[node]
temp = circle.copy()
for vertex in temp:
if vertex == node:
break
tmp = circle.pop(0)
if circle != []:
ans += min([cost[item] for item in circle])
print(ans)
``` | output | 1 | 88,361 | 8 | 176,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6. | instruction | 0 | 88,362 | 8 | 176,724 |
Tags: dfs and similar, graphs
Correct Solution:
```
import sys
rd = lambda : sys.stdin.readline().rstrip()
n = int(rd())
c = list(map(int, rd().split()))
a = list(map(lambda x: int(x)-1, rd().split()))
visited = [-1] * (n)
res = 0
for i in range(n):
trace = []
t = i
mn = 1e9
while visited[t] == -1:
visited[t] = i
trace.append(t)
t = a[t]
if visited[t] != i:
continue
while len(trace) > 0:
v = trace.pop()
mn = min(mn, c[v])
if t == v: break
res += mn
print(res)
``` | output | 1 | 88,362 | 8 | 176,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6. | instruction | 0 | 88,363 | 8 | 176,726 |
Tags: dfs and similar, graphs
Correct Solution:
```
def kormin(kezd,K,a,c):
i=kezd
hal=[kezd]
K[kezd]=1
elert=1
while(K[a[i]]==0 and korben[a[i]]==0):
K[a[i]]=1
hal.append(a[i])
elert=elert+1
i=a[i]
minkor=0
if(korben[a[i]]==0 and a[i] in hal):
vege=i
minkor=c[i]
korben[i]=1
while(a[i]!=vege):
korben[a[i]]=1
if(c[a[i]]<minkor):
minkor=c[a[i]]
i=a[i]
return([elert,minkor])
n=int(input())
c=[]
t=input().split()
for i in range(n):
c.append(int(t[i]))
a=[]
t=input().split()
for i in range(n):
a.append(int(t[i])-1)
korben=[]
K=[]
for i in range(n):
K.append(0)
korben.append(0)
feldolg=0
osszeg=0
kezd=0
while(feldolg<n):
while(K[kezd]==1):
kezd=kezd+1
x=kormin(kezd,K,a,c)
feldolg=feldolg+x[0]
osszeg=osszeg+x[1]
print(osszeg)
``` | output | 1 | 88,363 | 8 | 176,727 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6.
Submitted Solution:
```
n = int(input())
c = [int(x) for x in input().split()]
c.insert(0,0)
a = [int(x) for x in input().split()]
a.insert(0,0)
done = [0]*(n+1)
sol = 0
c_i = 0
for i in range(1,n+1):
c_i+=2
cost=0
if done[i]!=0:
continue
while done[i]==0:
done[i]=c_i
i=a[i]
if done[i]==c_i:
# found a new cycle
cost = c[i]
while done[i]==c_i:
done[i]+=1
i=a[i]
if c[i]<cost:
cost=c[i]
sol+=cost
print(sol)
``` | instruction | 0 | 88,364 | 8 | 176,728 |
Yes | output | 1 | 88,364 | 8 | 176,729 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6.
Submitted Solution:
```
# ///////////////////////////////////////////////////////////////////////////
# //////////////////// PYTHON IS THE BEST ////////////////////////
# ///////////////////////////////////////////////////////////////////////////
import sys,os,io
from sys import stdin
import math
from collections import defaultdict
from heapq import heappush, heappop, heapify
from bisect import bisect_left , bisect_right
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
alphabets = list('abcdefghijklmnopqrstuvwxyz')
#for deep recursion__________________________________________-
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,p - 2, p)) % p
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(int(i))
n = n / i
if n > 2:
l.append(n)
# c = dict(Counter(l))
return list(set(l))
# return c
def power(x, y, p) :
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
#____________________GetPrimeFactors in log(n)________________________________________
def sieveForSmallestPrimeFactor():
MAXN = 100001
spf = [0 for i in range(MAXN)]
spf[1] = 1
for i in range(2, MAXN):
spf[i] = i
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, math.ceil(math.sqrt(MAXN))):
if (spf[i] == i):
for j in range(i * i, MAXN, i):
if (spf[j] == j):
spf[j] = i
return spf
def getPrimeFactorizationLOGN(x):
spf = sieveForSmallestPrimeFactor()
ret = list()
while (x != 1):
ret.append(spf[x])
x = x // spf[x]
return ret
#____________________________________________________________
def SieveOfEratosthenes(n):
#time complexity = nlog(log(n))
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
def si():
return input()
def divideCeil(n,x):
if (n%x==0):
return n//x
return n//x+1
def ii():
return int(input())
def li():
return list(map(int,input().split()))
# ///////////////////////////////////////////////////////////////////////////
# //////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
# ///////////////////////////////////////////////////////////////////////////
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def solve():
n = ii()
c = [0] + li()
a = [0] + li()
vis = [False]*(n+1)
ans = 0
d = defaultdict(lambda:0)
cycleno = 0
for i in range(1,n+1):
if (vis[i]==False):
cur = i
first = i
while vis[cur]==False:
d[cur] = cycleno
vis[cur]=True
cur = a[cur]
# print(first,cur)
# print("_______________________________")
if d[cur]==cycleno:
min_ = c[cur]
first = cur
cur = a[cur]
while first!=cur:
# print(first,cur)
min_ = min(c[cur],min_)
cur = a[cur]
ans+=min_
cycleno+=1
# print(cur,first)
print(ans)
t = 1
# t = ii()
for _ in range(t):
solve()
``` | instruction | 0 | 88,365 | 8 | 176,730 |
Yes | output | 1 | 88,365 | 8 | 176,731 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6.
Submitted Solution:
```
if __name__ == '__main__':
n = int(input())
carr = list(map(int, input().split()))
marr = list(map(int, input().split()))
v = set()
cs = []
for x in marr:
if x not in v:
new = True
cv = set()
cv.add(x)
while marr[x - 1] not in cv:
x = marr[x - 1]
cv.add(x)
if x in v:
new = False
break
v |= cv
if new:
cs.append((x, marr[x - 1]))
c = 0
for x, y in cs:
p = set()
p.add(x)
p.add(y)
while marr[y - 1] not in p:
y = marr[y - 1]
p.add(y)
c += min(map(lambda o: carr[o - 1], p))
print(c)
``` | instruction | 0 | 88,366 | 8 | 176,732 |
Yes | output | 1 | 88,366 | 8 | 176,733 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6.
Submitted Solution:
```
n = int(input())
c = list(map(int, input().split()))
a = list(map(lambda x: int(x) - 1, input().split()))
deg_in = [0 for _ in range(n)]
d_in_to_v_set = {i: set() for i in range(n + 1)}
for ai in a:
deg_in[ai] += 1
for i in range(n):
d_in_to_v_set[deg_in[i]].add(i)
while d_in_to_v_set[0] != set():
l = list(d_in_to_v_set[0])
for u in l:
d_in_to_v_set[deg_in[a[u]]].remove(a[u])
d_in_to_v_set[deg_in[a[u]] - 1].add(a[u])
d_in_to_v_set[0].remove(u)
deg_in[a[u]] -= 1
p = [0 for _ in range(n)]
for v in d_in_to_v_set[1]:
p[v] = 1
ans, cur = 0, 10**5
for i in range(n):
s = i
cur = 10**5
while p[s] != 0:
cur = min(cur, c[s])
p[s] = 0
s = a[s]
ans += cur if cur < 10**5 else 0
print(ans)
``` | instruction | 0 | 88,367 | 8 | 176,734 |
Yes | output | 1 | 88,367 | 8 | 176,735 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.
The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
Input
The first line contains as single integers n (1 β€ n β€ 2 β
10^5) β the number of rooms in the dormitory.
The second line contains n integers c_1, c_2, ..., c_n (1 β€ c_i β€ 10^4) β c_i is the cost of setting the trap in room number i.
The third line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n) β a_i is the room the mouse will run to the next second after being in room i.
Output
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
Examples
Input
5
1 2 3 2 10
1 3 4 3 3
Output
3
Input
4
1 10 2 10
2 4 2 2
Output
10
Input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
2
Note
In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.
In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.
Here are the paths of the mouse from different starts from the third example:
* 1 β 2 β 2 β ...;
* 2 β 2 β ...;
* 3 β 2 β 2 β ...;
* 4 β 3 β 2 β 2 β ...;
* 5 β 6 β 7 β 6 β ...;
* 6 β 7 β 6 β ...;
* 7 β 6 β 7 β ...;
So it's enough to set traps in rooms 2 and 6.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
# Bootstrap https://github.com/cheran-senthil/PyRival/blob/master/tests/misc/test_bootstrap.py
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
#################
from collections import defaultdict
INF = pow(2, 63)
class DisjointSet:
def __init__(self, set_counts: list) -> None:
"""
Initialize with a list of the number of items in each set
and with rank = 1 for each set
"""
self.set_counts = set_counts
self.max_set = max(set_counts)
num_sets = len(set_counts)
self.ranks = [1] * num_sets
self.parents = list(range(num_sets))
def merge(self, src: int, dst: int) -> bool:
"""
Merge two sets together using Union by rank heuristic
Return True if successful
Merge two disjoint sets
>>> A = DisjointSet([1, 1, 1])
>>> A.merge(1, 2)
True
>>> A.merge(0, 2)
True
>>> A.merge(0, 1)
False
"""
src_parent = self.get_parent(src)
dst_parent = self.get_parent(dst)
if src_parent == dst_parent:
return False
if self.ranks[dst_parent] >= self.ranks[src_parent]:
self.set_counts[dst_parent] += self.set_counts[src_parent]
self.set_counts[src_parent] = 0
self.parents[src_parent] = dst_parent
if self.ranks[dst_parent] == self.ranks[src_parent]:
self.ranks[dst_parent] += 1
joined_set_size = self.set_counts[dst_parent]
else:
self.set_counts[src_parent] += self.set_counts[dst_parent]
self.set_counts[dst_parent] = 0
self.parents[dst_parent] = src_parent
joined_set_size = self.set_counts[src_parent]
self.max_set = max(self.max_set, joined_set_size)
return True
def get_parent(self, disj_set: int) -> int:
"""
Find the Parent of a given set
>>> A = DisjointSet([1, 1, 1])
>>> A.merge(1, 2)
True
>>> A.get_parent(0)
0
>>> A.get_parent(1)
2
"""
if self.parents[disj_set] == disj_set:
return disj_set
self.parents[disj_set] = self.get_parent(self.parents[disj_set])
return self.parents[disj_set]
from collections import deque
def solve(n, carr, garr):
djs = DisjointSet([1] * n)
dg = defaultdict(list)
for i, ed in enumerate(garr):
dg[ed - 1].append(i)
carrtup = sorted([(x, y) for x, y in enumerate(carr)], key=lambda x: x[1])
for c in carrtup:
cost = c[1]
ind = c[0]
if djs.get_parent(c[0]) != c[0]:
continue
bfsarr = deque()
bfsarr.append(c[0])
while bfsarr:
node = bfsarr.popleft()
children = dg[node]
for child in children:
if djs.get_parent(child) == child and child != c[0]:
bfsarr.append(child)
djs.merge(djs.get_parent(child), c[0])
else:
djs.merge(djs.get_parent(child), c[0])
ans = 0
for i in range(n):
if djs.set_counts[i]:
ans += carr[i]
return ans
@bootstrap
def rec(i, k, arr, mp, f=0):
# return str((arr[n - 1] - arr[0]) * (arr[-1] - arr[n]))
pass
def main():
# n, k = map(int, input().strip().split())
# a = []
# for _ in range(n):
# a.append(input().strip())
# print(solve(n, k, a))
# for _ in range(int(input().strip())):
n = int(input().strip())
# [n, m] = list(map(int, input().strip().split()))
arrn = list(map(int, input().strip().split()))
arrm = list(map(int, input().strip().split()))
ans = solve(n, arrn, arrm)
print(ans)
######## Python 2 and 3 footer by Pajenegod and c1729
# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.
# So on cf, use PyPy2 for best string performance.
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO, self).read()
def readline(self):
while self.newlines == 0:
s = self._fill();
self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# Cout implemented in Python
import sys
class ostream:
def __lshift__(self, a):
sys.stdout.write(str(a))
return self
cout = ostream()
endl = '\n'
# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero=0):
conv = ord if py2 else lambda x: x
A = [];
numb = zero;
sign = 1;
i = 0;
s = sys.stdin.buffer.read()
try:
while True:
if s[i] >= b'0'[0]:
numb = 10 * numb + conv(s[i]) - 48
elif s[i] == b'-'[0]:
sign = -1
elif s[i] != b'\r'[0]:
A.append(sign * numb)
numb = zero;
sign = 1
i += 1
except:
pass
if s and s[-1] >= b'0'[0]:
A.append(sign * numb)
return A
if __name__ == "__main__":
#for _ in range(4): # todo for testing else remove for loop
main()
``` | instruction | 0 | 88,368 | 8 | 176,736 |
No | output | 1 | 88,368 | 8 | 176,737 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.