message stringlengths 2 19.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
[3R2 as DJ Mashiro - Happiness Breeze](https://open.spotify.com/track/2qGqK8GRS65Wlf20qUBEak)
[Ice - DJ Mashiro is dead or alive](https://soundcloud.com/iceloki/dj-mashiro-is-dead-or-alive)
NEKO#Ξ¦ΟΞ¦ has just got a new maze game on her PC!
The game's main puzzle is a maze, in the forms of a 2 Γ n rectangle grid. NEKO's task is to lead a Nekomimi girl from cell (1, 1) to the gate at (2, n) and escape the maze. The girl can only move between cells sharing a common side.
However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.
After hours of streaming, NEKO finally figured out there are only q such moments: the i-th moment toggles the state of cell (r_i, c_i) (either from ground to lava or vice versa).
Knowing this, NEKO wonders, after each of the q moments, whether it is still possible to move from cell (1, 1) to cell (2, n) without going through any lava cells.
Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?
Input
The first line contains integers n, q (2 β€ n β€ 10^5, 1 β€ q β€ 10^5).
The i-th of q following lines contains two integers r_i, c_i (1 β€ r_i β€ 2, 1 β€ c_i β€ n), denoting the coordinates of the cell to be flipped at the i-th moment.
It is guaranteed that cells (1, 1) and (2, n) never appear in the query list.
Output
For each moment, if it is possible to travel from cell (1, 1) to cell (2, n), print "Yes", otherwise print "No". There should be exactly q answers, one after every update.
You can print the words in any case (either lowercase, uppercase or mixed).
Example
Input
5 5
2 3
1 4
2 4
2 3
1 4
Output
Yes
No
No
No
Yes
Note
We'll crack down the example test here:
* After the first query, the girl still able to reach the goal. One of the shortest path ways should be: (1,1) β (1,2) β (1,3) β (1,4) β (1,5) β (2,5).
* After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1, 3).
* After the fourth query, the (2, 3) is not blocked, but now all the 4-th column is blocked, so she still can't reach the goal.
* After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.
Submitted Solution:
```
from collections import defaultdict
n,q=map(int,input().split())
d=defaultdict(int)
row1=0
row2=0
for i in range(q):
a,b=input().split()
if(d[a+b]==1):
if(a=="1"):
row1-=1
else:
row2-=1
d[a+b]=0
else:
if(a=="1"):
row1+=1
else:
row2+=1
d[a+b]=1
if(row1==0 or row2==0):
print("YES")
else:
print("NO")
``` | instruction | 0 | 29,247 | 15 | 58,494 |
No | output | 1 | 29,247 | 15 | 58,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
[3R2 as DJ Mashiro - Happiness Breeze](https://open.spotify.com/track/2qGqK8GRS65Wlf20qUBEak)
[Ice - DJ Mashiro is dead or alive](https://soundcloud.com/iceloki/dj-mashiro-is-dead-or-alive)
NEKO#Ξ¦ΟΞ¦ has just got a new maze game on her PC!
The game's main puzzle is a maze, in the forms of a 2 Γ n rectangle grid. NEKO's task is to lead a Nekomimi girl from cell (1, 1) to the gate at (2, n) and escape the maze. The girl can only move between cells sharing a common side.
However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.
After hours of streaming, NEKO finally figured out there are only q such moments: the i-th moment toggles the state of cell (r_i, c_i) (either from ground to lava or vice versa).
Knowing this, NEKO wonders, after each of the q moments, whether it is still possible to move from cell (1, 1) to cell (2, n) without going through any lava cells.
Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?
Input
The first line contains integers n, q (2 β€ n β€ 10^5, 1 β€ q β€ 10^5).
The i-th of q following lines contains two integers r_i, c_i (1 β€ r_i β€ 2, 1 β€ c_i β€ n), denoting the coordinates of the cell to be flipped at the i-th moment.
It is guaranteed that cells (1, 1) and (2, n) never appear in the query list.
Output
For each moment, if it is possible to travel from cell (1, 1) to cell (2, n), print "Yes", otherwise print "No". There should be exactly q answers, one after every update.
You can print the words in any case (either lowercase, uppercase or mixed).
Example
Input
5 5
2 3
1 4
2 4
2 3
1 4
Output
Yes
No
No
No
Yes
Note
We'll crack down the example test here:
* After the first query, the girl still able to reach the goal. One of the shortest path ways should be: (1,1) β (1,2) β (1,3) β (1,4) β (1,5) β (2,5).
* After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1, 3).
* After the fourth query, the (2, 3) is not blocked, but now all the 4-th column is blocked, so she still can't reach the goal.
* After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.
Submitted Solution:
```
n_q = input().split()
q = int(n_q[1])
n = int(n_q[0])
liste = []
path = []
for times in range(q) :
block = input().split()
for index in range(2) :
block[index] = int(block[index])
if block not in path :
path.append(block)
key = True
else :
path.remove(block)
key = False
if key == True :
if block[0] == 1 and [2,block[1]] not in path and [2,block[1]+1] not in path and [2,block[1]-1] not in path :
liste.append("Yes")
elif block[0] == 2 and [1,block[1]] not in path and [1,block[1]+1] not in path and [1,block[1]-1] not in path :
liste.append("Yes")
else :
liste.append("No")
else :
key = "+"
for block in path :
if (block[0] == 1 and [2,block[1]] in path) or (block[0] == 1 and [2,block[1]+1] in path) or (block[0]== 1 and [2,block[1]-1] in path) :
liste.append("No")
key = "-"
break
elif (block[0] == 2 and [1,block[1]] in path) or (block[0] == 2 and [1,block[1]+1] in path) or (block[0]== 2 and [1,block[1]-1] in path) :
liste.append("No")
key = "-"
break
if key != "-" :
liste.append("Yes")
for i in liste:
print(i)
``` | instruction | 0 | 29,248 | 15 | 58,496 |
No | output | 1 | 29,248 | 15 | 58,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
[3R2 as DJ Mashiro - Happiness Breeze](https://open.spotify.com/track/2qGqK8GRS65Wlf20qUBEak)
[Ice - DJ Mashiro is dead or alive](https://soundcloud.com/iceloki/dj-mashiro-is-dead-or-alive)
NEKO#Ξ¦ΟΞ¦ has just got a new maze game on her PC!
The game's main puzzle is a maze, in the forms of a 2 Γ n rectangle grid. NEKO's task is to lead a Nekomimi girl from cell (1, 1) to the gate at (2, n) and escape the maze. The girl can only move between cells sharing a common side.
However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.
After hours of streaming, NEKO finally figured out there are only q such moments: the i-th moment toggles the state of cell (r_i, c_i) (either from ground to lava or vice versa).
Knowing this, NEKO wonders, after each of the q moments, whether it is still possible to move from cell (1, 1) to cell (2, n) without going through any lava cells.
Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?
Input
The first line contains integers n, q (2 β€ n β€ 10^5, 1 β€ q β€ 10^5).
The i-th of q following lines contains two integers r_i, c_i (1 β€ r_i β€ 2, 1 β€ c_i β€ n), denoting the coordinates of the cell to be flipped at the i-th moment.
It is guaranteed that cells (1, 1) and (2, n) never appear in the query list.
Output
For each moment, if it is possible to travel from cell (1, 1) to cell (2, n), print "Yes", otherwise print "No". There should be exactly q answers, one after every update.
You can print the words in any case (either lowercase, uppercase or mixed).
Example
Input
5 5
2 3
1 4
2 4
2 3
1 4
Output
Yes
No
No
No
Yes
Note
We'll crack down the example test here:
* After the first query, the girl still able to reach the goal. One of the shortest path ways should be: (1,1) β (1,2) β (1,3) β (1,4) β (1,5) β (2,5).
* After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1, 3).
* After the fourth query, the (2, 3) is not blocked, but now all the 4-th column is blocked, so she still can't reach the goal.
* After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.
Submitted Solution:
```
# NEKO's Maze Game
def level1(r,c,lv_st,lava,count):
if c-1 >= 0 and c+1 < n:
if (lava[r][c]==1) or (lava[r][c-1]==1) or (lava[r][c+1]==1):
lv_st[c] = False
if count == 1:
level1(r,c-1,lv_st,lava,2)
level1(r,c+1,lv_st,lava,2)
elif c+1 < n:
if (lava[r][c]==1) or (lava[r][c+1]==1):
lv_st[c] = False
if count == 1:
level1(r,c+1,lv_st,lava,2)
else:
if (lava[r][c]==1) or (lava[r][c-1]==1):
lv_st[c] = False
if count == 1:
level1(r,c-1,lv_st,lava,2)
def level2(r,c,lv_st,lava,count):
if c-1 >= 0 and c+1 < n:
if (lava[r][c]==1) or (lava[r][c-1]==1) or (lava[r][c+1]==1):
lv_st[c] = True
if count == 1:
level2(r,c-1,lv_st,lava,2)
level2(r,c+1,lv_st,lava,2)
elif c+1 < n:
if (lava[r][c]==1) or (lava[r][c+1]==1):
lv_st[c] = True
if count == 1:
level2(r,c+1,lv_st,lava,2)
else:
if (lava[r][c]==1) or (lava[r][c-1]==1):
lv_st[c] = True
if count == 1:
level2(r,c-1,lv_st,lava,2)
def rev(x):
if x == 1:
return 0
else:
return 1
n, q = input().split()
n = int(n)
q = int(q)
lava = [[0]*n,[0]*n]
lv_st = [True] * n
for i in range(0,q):
r,c = input().split()
r = int(r)-1
c = int(c)-1
if lava[r][c] == 0:
lava[r][c] = 1
level1(rev(r),c,lv_st,lava,1)
else:
lava[r][c] = 0
level2(rev(r),c,lv_st,lava,1)
if False in lv_st:
print("No")
else:
print("Yes")
# print(lv_st)
# for j in range(0,n):
# print(lava[0][j], end=" ")
# print("")
# for j in range(0,n):
# print(lava[1][j], end=" ")
# print("")
``` | instruction | 0 | 29,249 | 15 | 58,498 |
No | output | 1 | 29,249 | 15 | 58,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
[3R2 as DJ Mashiro - Happiness Breeze](https://open.spotify.com/track/2qGqK8GRS65Wlf20qUBEak)
[Ice - DJ Mashiro is dead or alive](https://soundcloud.com/iceloki/dj-mashiro-is-dead-or-alive)
NEKO#Ξ¦ΟΞ¦ has just got a new maze game on her PC!
The game's main puzzle is a maze, in the forms of a 2 Γ n rectangle grid. NEKO's task is to lead a Nekomimi girl from cell (1, 1) to the gate at (2, n) and escape the maze. The girl can only move between cells sharing a common side.
However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.
After hours of streaming, NEKO finally figured out there are only q such moments: the i-th moment toggles the state of cell (r_i, c_i) (either from ground to lava or vice versa).
Knowing this, NEKO wonders, after each of the q moments, whether it is still possible to move from cell (1, 1) to cell (2, n) without going through any lava cells.
Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?
Input
The first line contains integers n, q (2 β€ n β€ 10^5, 1 β€ q β€ 10^5).
The i-th of q following lines contains two integers r_i, c_i (1 β€ r_i β€ 2, 1 β€ c_i β€ n), denoting the coordinates of the cell to be flipped at the i-th moment.
It is guaranteed that cells (1, 1) and (2, n) never appear in the query list.
Output
For each moment, if it is possible to travel from cell (1, 1) to cell (2, n), print "Yes", otherwise print "No". There should be exactly q answers, one after every update.
You can print the words in any case (either lowercase, uppercase or mixed).
Example
Input
5 5
2 3
1 4
2 4
2 3
1 4
Output
Yes
No
No
No
Yes
Note
We'll crack down the example test here:
* After the first query, the girl still able to reach the goal. One of the shortest path ways should be: (1,1) β (1,2) β (1,3) β (1,4) β (1,5) β (2,5).
* After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1, 3).
* After the fourth query, the (2, 3) is not blocked, but now all the 4-th column is blocked, so she still can't reach the goal.
* After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.
Submitted Solution:
```
n, q = map(int, input().split())
m = [[0] * n for i in range(2)]
cnt = 0
for i in range(q):
a, b = map(int, input().split())
if m[a - 1][b - 1]:
m[a - 1][b - 1] = 0
cnt -= sum(m[2 - a][b - 2:b + 1])
else:
m[a - 1][b - 1] = 1
cnt += sum(m[2 - a][b - 2:b + 1])
if cnt or m[0][0] or m[1][n - 1]:
print('No')
else:
print('Yes')
``` | instruction | 0 | 29,250 | 15 | 58,500 |
No | output | 1 | 29,250 | 15 | 58,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image> | instruction | 0 | 29,319 | 15 | 58,638 |
Tags: brute force, constructive algorithms, greedy, implementation, math, shortest paths
Correct Solution:
```
from bisect import *
from collections import *
from math import *
from heapq import *
from typing import List
from itertools import *
from operator import *
from functools import *
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
#------------------------------------------------------------------------
@lru_cache(None)
def fact(x):
if x<2:
return 1
return fact(x-1)*x
@lru_cache(None)
def per(i,j):
return fact(i)//fact(i-j)
@lru_cache(None)
def com(i,j):
return per(i,j)//fact(j)
def linc(f,t,l,r):
while l<r:
mid=(l+r)//2
if t>f(mid):
l=mid+1
else:
r=mid
return l
def rinc(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t<f(mid):
r=mid-1
else:
l=mid
return l
def ldec(f,t,l,r):
while l<r:
mid=(l+r)//2
if t<f(mid):
l=mid+1
else:
r=mid
return l
def rdec(f,t,l,r):
while l<r:
mid=(l+r+1)//2
if t>f(mid):
r=mid-1
else:
l=mid
return l
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def binfun(x):
c=0
for w in arr:
c+=ceil(w/x)
return c
def lowbit(n):
return n&-n
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
class smt:
def __init__(self,l,r,arr):
self.l=l
self.r=r
self.value=(1<<31)-1 if l<r else arr[l]
mid=(l+r)//2
if(l<r):
self.left=smt(l,mid,arr)
self.right=smt(mid+1,r,arr)
self.value&=self.left.value&self.right.value
#print(l,r,self.value)
def setvalue(self,x,val):
if(self.l==self.r):
self.value=val
return
mid=(self.l+self.r)//2
if(x<=mid):
self.left.setvalue(x,val)
else:
self.right.setvalue(x,val)
self.value=self.left.value&self.right.value
def ask(self,l,r):
if(l<=self.l and r>=self.r):
return self.value
val=(1<<31)-1
mid=(self.l+self.r)//2
if(l<=mid):
val&=self.left.ask(l,r)
if(r>mid):
val&=self.right.ask(l,r)
return val
class UFS:
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return prime
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
for v in graph[u]:
if v not in d or d[v]>d[u]+graph[u][v]:
d[v]=d[u]+graph[u][v]
heappush(heap,(d[v],v))
return d
'''
import time
s=time.time()
for i in range(2000):
print(0)
e=time.time()
print(e-s)
'''
t=N()
for i in range(t):
x,y=RL()
c=RLL()
#c=[min(c[i],c[(i-1)%6]+c[(i+1)%6]) for i in range(6)]
if x>=0 and y>=0:
if x>=y:
ans=min(c[5]*x+c[1]*y,y*c[0]+(x-y)*c[5],x*c[0]+(x-y)*c[4])
else:
ans=min(c[5]*x+c[1]*y,x*c[0]+(-x+y)*c[1],y*c[0]+(-x+y)*c[2])
elif x<=0 and y<=0:
x,y=-x,-y
c=[c[(i+3)%6] for i in range(6)]
if x>=y:
ans=min(c[5]*x+c[1]*y,y*c[0]+(x-y)*c[5],x*c[0]+(x-y)*c[4])
else:
ans=min(c[5]*x+c[1]*y,x*c[0]+(-x+y)*c[1],y*c[0]+(-x+y)*c[2])
elif x>=0 and y<=0:
y*=-1
ans=min(x*c[5]+y*c[4],(x+y)*c[4]+x*c[0],(x+y)*c[5]+y*c[3])
else:
x*=-1
ans=min(c[2]*x+c[1]*y,c[1]*(x+y)+x*c[3],c[2]*(x+y)+c[0]*y)
print(ans)
``` | output | 1 | 29,319 | 15 | 58,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image> | instruction | 0 | 29,320 | 15 | 58,640 |
Tags: brute force, constructive algorithms, greedy, implementation, math, shortest paths
Correct Solution:
```
def calc():
global c1,c2,c3,c4,c5,c6,x1,x2,x3,x4,x5,x6
return x1*c1+x2*c2+x3*c3+x4*c4+x5*c5+x6*c6
for _ in range(int(input())):
x,y=map(int,input().split())
c1,c2,c3,c4,c5,c6=map(int,input().split())
ans=[]
if x>0 and y>0:
x1,x2,x3,x4,x5,x6=0,y,0,0,0,x
elif x>0:
x1,x2,x3,x4,x5,x6=0,0,0,0,-y,x
elif y>0:
x1,x2,x3,x4,x5,x6=0,y,-x,0,0,0
else:
x1,x2,x3,x4,x5,x6=0,0,-x,0,-y,0
ans.append(calc())
if x>0 and y-x>0:
x1,x2,x3,x4,x5,x6=x,y-x,0,0,0,0
elif y-x>0:
x1,x2,x3,x4,x5,x6=0,y-x,0,-x,0,0
elif x>0:
x1,x2,x3,x4,x5,x6=x,0,0,0,x-y,0
else:
x1,x2,x3,x4,x5,x6=0,0,0,-x,x-y,0
ans.append(calc())
if y>0 and x-y>0:
x1,x2,x3,x4,x5,x6=y,0,0,0,0,x-y
elif x-y>0:
x1,x2,x3,x4,x5,x6=0,0,0,-y,0,x-y
elif y>0:
x1,x2,x3,x4,x5,x6=y,0,y-x,0,0,0
else:
x1,x2,x3,x4,x5,x6=0,0,y-x,-y,0,0
ans.append(calc())
print(min(ans))
``` | output | 1 | 29,320 | 15 | 58,641 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image> | instruction | 0 | 29,321 | 15 | 58,642 |
Tags: brute force, constructive algorithms, greedy, implementation, math, shortest paths
Correct Solution:
```
# region fastio # from https://codeforces.com/contest/1333/submission/75948789
import sys, io, os
BUFSIZE = 8192
class FastIO(io.IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = io.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(io.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")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#endregion
from itertools import combinations
DX = [1,0,-1,-1,0,1]
DY = [1,1,0,-1,-1,0]
T = int(input())
for _ in range(T):
X, Y = map(int, input().split())
C = list(map(int, input().split()))
if X == Y == 0:
print(0)
continue
ans = 1<<62
for i1, i2 in combinations(range(6), 2):
c1, c2 = C[i1], C[i2]
x1, x2, y1, y2 = DX[i1], DX[i2], DY[i1], DY[i2]
numer2 = X*y1 - Y*x1
denom2 = x2*y1 - x1*y2
if denom2 == 0:
continue
n2 = numer2 // denom2
if n2 < 0:
continue
numer1 = X*y2 - Y*x2
denom1 = x1*y2 - x2*y1
n1 = numer1 // denom1
if n1 < 0:
continue
cost = n1 * c1 + n2 * c2
if ans > cost:
ans = cost
print(ans)
``` | output | 1 | 29,321 | 15 | 58,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image> | instruction | 0 | 29,322 | 15 | 58,644 |
Tags: brute force, constructive algorithms, greedy, implementation, math, shortest paths
Correct Solution:
```
def main():
for _ in range(int(input())):
x,y=map(int,input().split())
c=list(map(int,input().split()))
r1=abs(x)*c[5 if x >= 0 else 2] +abs(y)*c[1 if y >= 0 else 4]
r2=abs(x)*c[0 if x >=0 else 3]+abs(y-x)*c[1 if y >= x else 4]
r3=abs(y)*c[0 if y >=0 else 3]+abs(y-x)*c[5 if x >= y else 2]
print(min([r1,r2,r3]))
main()
``` | output | 1 | 29,322 | 15 | 58,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image> | instruction | 0 | 29,323 | 15 | 58,646 |
Tags: brute force, constructive algorithms, greedy, implementation, math, shortest paths
Correct Solution:
```
def optimize_costs(c1, c2, c3, c4, c5, c6):
c1 = min(c1, c2 + c6)
c2 = min(c2, c1 + c3)
c3 = min(c3, c2 + c4)
c4 = min(c4, c3 + c5)
c5 = min(c5, c4 + c6)
c6 = min(c6, c5 + c1)
return c1, c2, c3, c4, c5, c6
def main(input_f):
t = int(input_f())
for _ in range(t):
x, y = map(int, input_f().split(' '))
c1, c2, c3, c4, c5, c6 = map(int, input_f().split(' '))
# c1 = +1 +1
# c2 = 0 +1
# c3 = -1 0
# c4 = -1 -1
# c5 = 0 -1
# c6 = +1 0
for _ in range(10):
c1, c2, c3, c4, c5, c6 = optimize_costs(c1, c2, c3, c4, c5, c6)
if x < 0 and y < 0:
if x < y:
print(-y * c4 + (y - x) * c3)
else:
print(-x * c4 + (x - y) * c5)
elif x < 0 and y >= 0:
print(-x * c3 + y * c2)
elif x >= 0 and y < 0:
print(x * c6 - y * c5)
else:
if x < y:
print(x * c1 + (y - x) * c2)
else:
print(y * c1 + (x - y) * c6)
if __name__ == '__main__':
#with open('input.txt') as f:
# main(f.readline)
main(input)
``` | output | 1 | 29,323 | 15 | 58,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image> | instruction | 0 | 29,324 | 15 | 58,648 |
Tags: brute force, constructive algorithms, greedy, implementation, math, shortest paths
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
x,y = map(int,input().split())
cost = [0]+list(map(int,input().split()))
if not x and not y:
print(0)
elif not x:
if y < 0:
print(-y*min(cost[5],cost[4]+cost[6]))
else:
print(y*min(cost[2],cost[1]+cost[3]))
elif not y:
if x < 0:
print(-x*min(cost[3],cost[2]+cost[4]))
else:
print(x*min(cost[6],cost[1]+cost[5]))
else:
if x < 0 and y < 0:
x,y = abs(x),abs(y)
r = min(x,y)
print(r*min(cost[4],cost[3]+cost[5])+
(x-r)*min(cost[3],cost[2]+cost[4])+
(y-r)*min(cost[5],cost[4]+cost[6]))
elif x < 0:
x, y = abs(x), abs(y)
print(x * min(cost[3], cost[2] + cost[4]) +
y * min(cost[2],cost[1]+cost[3]))
elif y < 0:
x, y = abs(x), abs(y)
print(x * min(cost[6],cost[1]+cost[5]) +
y * min(cost[5], cost[4] + cost[6]))
else:
x, y = abs(x), abs(y)
r = min(x, y)
print(r * min(cost[1], cost[2] + cost[6]) +
(x - r) * min(cost[6], cost[1] + cost[5]) +
(y - r) * min(cost[2],cost[1]+cost[3]))
#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main()
``` | output | 1 | 29,324 | 15 | 58,649 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image> | instruction | 0 | 29,325 | 15 | 58,650 |
Tags: brute force, constructive algorithms, greedy, implementation, math, shortest paths
Correct Solution:
```
import sys
from random import *
#from bisect import *
#from collections import deque
pl=1
#from math import *
from copy import *
if pl:
input=sys.stdin.readline
else:
sys.stdin=open('input.txt', 'r')
sys.stdout=open('outpt.txt','w')
def li():
return [int(xxx) for xxx in input().split()]
def fi():
return int(input())
def si():
return list(input().rstrip())
def mi():
return map(int,input().split())
t=fi()
while t>0:
t-=1
tar=li()
cost=li()
for j in range(36):
for i in range(6):
cost[i]=min(cost[i],cost[i-1]+cost[(i+1)%6])
ans=0
x,y=tar
c=cost
while [x,y]!=[0,0]:
if x>0 and y>0:
p=min(x,y)
ans+=cost[0]*p
x-=p
y-=p
elif x<0 and y<0:
p=min(-x,-y)
ans+=cost[3]*p
x+=p
y+=p
elif x==0:
if y>0:
ans+=y*c[1]
y=0
else:
ans+=(-y)*c[4]
y=0
else:
if x>0:
ans+=x*c[5]
x=0
else:
ans+=(-x)*c[2]
x=0
#print(ans,c)
print(ans)
``` | output | 1 | 29,325 | 15 | 58,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image> | instruction | 0 | 29,326 | 15 | 58,652 |
Tags: brute force, constructive algorithms, greedy, implementation, math, shortest paths
Correct Solution:
```
T = int(input())
for t in range(T):
X, Y = [int(_) for _ in input().split()]
C = [int(_) for _ in input().split()]
for _ in range(10):
for i in range(len(C)):
prev = i-1
if prev < 0:
prev += 6
nxt = i+1
if nxt >= 6:
nxt -= 6
C[i] = min(C[i], C[prev]+C[nxt])
for i in range(len(C)-1, -1, -1):
prev = i-1
if prev < 0:
prev += 6
nxt = i+1
if nxt >= 6:
nxt -= 6
C[i] = min(C[i], C[prev]+C[nxt])
c1, c2, c3, c4, c5, c6 = C[0], C[1], C[2], C[3], C[4], C[5]
if X == 0:
if Y > 0:
print(c2 * Y)
else:
print(c5 * abs(Y))
elif X < 0:
if Y >= 0:
print(c3 * abs(X) + c2 * Y)
elif Y <= X:
print(c4 * abs(X) + c5 * abs(X-Y))
else:
print(c3 * abs(X-Y) + c4 * abs(Y))
elif X > 0:
if Y >= X:
print(c1 * X + (Y-X) * c2)
elif Y <= 0:
print(c6 * X + abs(Y) * c5)
else:
print(c1 * Y + c6 * (X-Y))
``` | output | 1 | 29,326 | 15 | 58,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image>
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#such incredible bullshit
#i can't even
def solve_case():
y, x = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
if x == 0 and y == 0:
print(0)
return
d1 = 0
if x > 0:
d1 += x * c[1]
else:
d1 -= x * c[4]
if y > 0:
d1 += y * c[5]
else:
d1 -= y * c[2]
d2 = 0
if x > 0:
d2 += x * c[0]
else:
d2 -= x * c[3]
if x > y:
d2 += (x - y) * c[2]
else:
d2 += (y - x) * c[5]
d3 = 0
if y > 0:
d3 += y * c[0]
else:
d3 -= y * c[3]
if y > x:
d3 += (y - x) * c[4]
else:
d3 += (x - y) * c[1]
print(min(d1, d2, d3))
def main():
for _ in range(int(input())):
solve_case()
main()
``` | instruction | 0 | 29,327 | 15 | 58,654 |
Yes | output | 1 | 29,327 | 15 | 58,655 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image>
Submitted Solution:
```
n = int(input())
def calc(x,y,z):
if x>=0:
return x*y
else:
return abs(x)*z
for j in range(n):
x,y= map(int,input().split())
c1,c2,c3,c4,c5,c6= map(int,input().split())
best=9223372036854775807
best = min(best, calc(x, c6, c3) + calc(y, c2, c5))
best = min(best, calc(y, c1, c4) + calc(x - y, c6, c3))
best = min(best, calc(x, c1, c4) + calc(y - x, c2, c5))
print(best)
``` | instruction | 0 | 29,328 | 15 | 58,656 |
Yes | output | 1 | 29,328 | 15 | 58,657 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image>
Submitted Solution:
```
import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline
def solve_fast(x, y, c):
ans1 = abs(x) * c[6 if x > 0 else 3] + abs(y) * c[2 if y > 0 else 5]
ans2 = abs(x) * c[1 if x > 0 else 4] + abs(y - x) * c[2 if y-x>0 else 5]
ans3 = abs(y) * c[1 if y > 0 else 4] + abs(y - x) * c[3 if y-x> 0 else 6]
return min(ans1, ans2, ans3)
T = int(input())
for t in range(T):
x, y = map(int, input().split())
price = [0] + list(map(int, input().split()))
best_cost = solve_fast(x, y, price)
print(best_cost)
``` | instruction | 0 | 29,329 | 15 | 58,658 |
Yes | output | 1 | 29,329 | 15 | 58,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image>
Submitted Solution:
```
t = int(input())
while t > 0:
x, y = map(int, input().split())
lst = list(map(int, input().split()))
cost = lst.copy()
for i in range(1, 7):
cost[i%6] = min(cost[i%6], cost[(i-1)%6]+cost[(i+1)%6])
cnt = [0] * 6
if x == 0 and y == 0:
pass
elif x >= 0 and y >= 0:
if x == y:
cnt[0] = x
elif x > y:
cnt[0] = y
cnt[5] = x-y
elif x < y:
cnt[0] = x
cnt[1] = y-x
elif x <= 0 and y <= 0:
if x == y:
cnt[3] = -x
elif x > y:
cnt[3] = -x
cnt[4] = x-y
elif x < y:
cnt[3] = -y
cnt[2] = y-x
elif x >= 0 and y <= 0:
cnt[4] = -y
cnt[5] = x
else:
cnt[2] = -x
cnt[1] = y
ans = 0
for i in range(6):
ans += cnt[i]*cost[i]
print(ans)
t -= 1
``` | instruction | 0 | 29,330 | 15 | 58,660 |
Yes | output | 1 | 29,330 | 15 | 58,661 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image>
Submitted Solution:
```
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) ]))
def invr():
return(map(int,input().split()))
n = inp()
for _ in range(n):
target = inlt()
l = inlt()
if(target[0] >= 0 and target[1] >= 0):
path1 = target[0] * l[5] + target[1] * l[1]
if(target[0] > target[1]):
path2 = target[0] * l[0] + (target[0] - target[1]) * l[4]
path3 = target[1] * l[0] + abs(target[0] - target[1]) * l[5]
else:
path2 = target[1] * l[0] + (target[1] - target[0]) * l[1]
path3 = target[0] * l[0] + abs(target[0] - target[1]) * l[2]
print(min(path1,path2, path3))
elif(target[0] <= 0 and target[1] >= 0):
path1 = abs(target[0] * l[2]) + abs(target[1] * l[1])
path2 = abs(target[1] * l[0]) + (abs(target[0]) + abs(target[1])) * l[2]
path3 = abs(target[0] * l[3]) + (abs(target[0]) + target[1]) * l[1]
print(min(path1,path2,path3))
elif(target[0] >= 0 and target[1] <= 0):
path1 = target[0] * l[5] + abs(target[1]) * l[4]
path2 = target[0] * l[0] + (target[0] + abs(target[1])) * l[4]
path3 = abs(target[1]) * l[3] + (target[0] + abs(target[1])) * l[5]
print(min(path1,path2,path3))
else:
path1 = abs(target[0]) * l[2] + abs(target[1]) * l[4]
if(target[0] > target[1]):
path2 = abs(target[0]) * l[3] + abs((target[0] - target[1])) * l[4]
path3 = abs(target[1]) * l[3] + abs(target[0] - target[1]) * l[5]
else:
path2 = abs(target[1]) * l[3] + abs((target[1] - target[0])) * l[2]
path3 = abs(target[0]) * l[3] + abs(target[0] - target[1]) * l[1]
print(min(path1,path2, path3))
``` | instruction | 0 | 29,331 | 15 | 58,662 |
No | output | 1 | 29,331 | 15 | 58,663 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image>
Submitted Solution:
```
import time,math as mt,bisect,sys
from sys import stdin,stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi=3.14159265358979323846264338327950
def II(): # to take integer input
return int(stdin.readline())
def IO(): # to take string input
return stdin.readline()
def IP(): # to take tuple as input
return map(int,stdin.readline().split())
def L(): # to take list as input
return list(map(int,stdin.readline().split()))
def P(x): # to print integer,list,string etc..
return stdout.write(str(x)+"\n")
def PI(x,y): # to print tuple separatedly
return stdout.write(str(x)+" "+str(y)+"\n")
def lcm(a,b): # to calculate lcm
return (a*b)//gcd(a,b)
def gcd(a,b): # to calculate gcd
if a==0:
return b
elif b==0:
return a
if a>b:
return gcd(a%b,b)
else:
return gcd(a,b%a)
def bfs(adj,v): # a schema of bfs
visited=[False]*(v+1)
q=deque()
while q:
pass
def sieve():
li=[True]*1000001
li[0],li[1]=False,False
for i in range(2,len(li),1):
if li[i]==True:
for j in range(i*i,len(li),i):
li[j]=False
prime=[]
for i in range(1000001):
if li[i]==True:
prime.append(i)
return prime
def setBit(n):
count=0
while n!=0:
n=n&(n-1)
count+=1
return count
mx=10**7
spf=[mx]*(mx+1)
def SPF():
spf[1]=1
for i in range(2,mx+1):
if spf[i]==mx:
spf[i]=i
for j in range(i*i,mx+1,i):
if i<spf[j]:
spf[j]=i
return
def readTree(v): # to read tree
adj=[set() for i in range(v+1)]
for i in range(v-1):
u1,u2=In()
adj[u1].add(u2)
adj[u2].add(u1)
return adj
#####################################################################################
# 1000000000000000000
# 1000000000000000000
# 10000000000
def solve():
x,y=IP()
c1,c2,c3,c4,c5,c6=L()
print(c1,c2,c3,c4,c5,c6)
ans=1e20
if x>=0:
cost1=(c1*x)
xx,yy=x,x
if y>=yy:
cost1+=(c2*(y-yy))
else:
cost1+=(c5*abs(y-yy))
cost2=(c6*x)
xx,yy=x,0
if y>=yy:
cost2+=(c2*abs(y-yy))
else:
cost2+=(c5*abs(y-yy))
ans=min(ans,cost1,cost2)
else:
cost1=(c3*(abs(x)))
xx,yy=x,0
if (y>=yy):
cost1+=(c2*y)
else:
cost1+=(c5*abs(y))
cost2=(c4*abs(x))
xx,yy=x,x
if (y>=yy):
cost2+=(c2*abs(y-yy))
else:
cost2+=(c5*abs(y-yy))
ans=min(ans,cost1,cost2)
print(int(ans))
return
t=II()
for i in range(t):
solve()
#######
#
#
####### # # # #### # # #
# # # # # # # # # # #
# #### # # #### #### # #
###### # # #### # # # # #
# ``````ΒΆ0````1ΒΆ1_```````````````````````````````````````
# ```````ΒΆΒΆΒΆ0_`_ΒΆΒΆΒΆ0011100ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ001_````````````````````
# ````````ΒΆΒΆΒΆΒΆΒΆ00ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0_````````````````
# `````1_``ΒΆΒΆ00ΒΆ0000000000000000000000ΒΆΒΆΒΆΒΆ0_`````````````
# `````_ΒΆΒΆ_`0ΒΆ000000000000000000000000000ΒΆΒΆΒΆΒΆΒΆ1``````````
# ```````ΒΆΒΆΒΆ00ΒΆ00000000000000000000000000000ΒΆΒΆΒΆ_`````````
# ````````_ΒΆΒΆ00000000000000000000ΒΆΒΆ00000000000ΒΆΒΆ`````````
# `````_0011ΒΆΒΆΒΆΒΆΒΆ000000000000ΒΆΒΆ00ΒΆΒΆ0ΒΆΒΆ00000000ΒΆΒΆ_````````
# ```````_ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ00000000000ΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆ00000000ΒΆΒΆ1````````
# ``````````1ΒΆΒΆΒΆΒΆΒΆ000000ΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0000000ΒΆΒΆΒΆ````````
# ```````````ΒΆΒΆΒΆ0ΒΆ000ΒΆ00ΒΆ0ΒΆΒΆ`_____`__1ΒΆ0ΒΆΒΆ00ΒΆ00ΒΆΒΆ````````
# ```````````ΒΆΒΆΒΆΒΆΒΆ00ΒΆ00ΒΆ10ΒΆ0``_1111_`_ΒΆΒΆ0000ΒΆ0ΒΆΒΆΒΆ````````
# ``````````1ΒΆΒΆΒΆΒΆΒΆ00ΒΆ0ΒΆΒΆ_ΒΆΒΆ1`_ΒΆ_1_0_`1ΒΆΒΆ_0ΒΆ0ΒΆΒΆ0ΒΆΒΆ````````
# ````````1ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆ0ΒΆ0_0ΒΆ``100111``_ΒΆ1_0ΒΆ0ΒΆΒΆ_1ΒΆ````````
# ```````1ΒΆΒΆΒΆΒΆ00ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ010ΒΆ``1111111_0ΒΆ11ΒΆΒΆΒΆΒΆΒΆ_10````````
# ```````0ΒΆΒΆΒΆΒΆ__10ΒΆΒΆΒΆΒΆΒΆ100ΒΆΒΆΒΆ0111110ΒΆΒΆΒΆ1__ΒΆΒΆΒΆΒΆ`__````````
# ```````ΒΆΒΆΒΆΒΆ0`__0ΒΆΒΆ0ΒΆΒΆ_ΒΆΒΆΒΆ_11````_0ΒΆΒΆ0`_1ΒΆΒΆΒΆΒΆ```````````
# ```````ΒΆΒΆΒΆ00`__0ΒΆΒΆ_00`_0_``````````1_``ΒΆ0ΒΆΒΆ_```````````
# ``````1ΒΆ1``ΒΆΒΆ``1ΒΆΒΆ_11``````````````````ΒΆ`ΒΆΒΆ````````````
# ``````1_``ΒΆ0_ΒΆ1`0ΒΆ_`_``````````_``````1_`ΒΆ1````````````
# ``````````_`1ΒΆ00ΒΆΒΆ_````_````__`1`````__`_ΒΆ`````````````
# ````````````ΒΆ1`0ΒΆΒΆ_`````````_11_`````_``_``````````````
# `````````ΒΆΒΆΒΆΒΆ000ΒΆΒΆ_1```````_____```_1``````````````````
# `````````ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0_``````_````_1111__``````````````
# `````````ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ01_`````_11____1111_```````````
# `````````ΒΆΒΆ0ΒΆ0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ1101_______11ΒΆ_```````````
# ``````_ΒΆΒΆΒΆ0000000ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆ0ΒΆΒΆΒΆ1````````````
# `````0ΒΆΒΆ0000000ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ1`````````````
# ````0ΒΆ0000000ΒΆΒΆ0_````_011_10ΒΆ110ΒΆ01_1ΒΆΒΆΒΆ0````_100ΒΆ001_`
# ```1ΒΆ0000000ΒΆ0_``__`````````_`````````0ΒΆ_``_00ΒΆΒΆ010ΒΆ001
# ```ΒΆΒΆ00000ΒΆΒΆ1``_01``_11____``1_``_`````ΒΆΒΆ0100ΒΆ1```_00ΒΆ1
# ``1ΒΆΒΆ00000ΒΆ_``_ΒΆ_`_101_``_`__````__````_0000001100ΒΆΒΆΒΆ0`
# ``ΒΆΒΆΒΆ0000ΒΆ1_`_ΒΆ``__0_``````_1````_1_````1ΒΆΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆΒΆ0```
# `_ΒΆΒΆΒΆΒΆ00ΒΆ0___01_10ΒΆ_``__````1`````11___`1ΒΆΒΆΒΆ01_````````
# `1ΒΆΒΆΒΆΒΆΒΆ0ΒΆ0`__01ΒΆΒΆΒΆ0````1_```11``___1_1__11ΒΆ000`````````
# `1ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ1_1_01__`01```_1```_1__1_11___1_``00ΒΆ1````````
# ``ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0`__10__000````1____1____1___1_```10ΒΆ0_```````
# ``0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ1___0000000```11___1__`_0111_```000ΒΆ01```````
# ```ΒΆΒΆΒΆ00000ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ01___1___00_1ΒΆΒΆΒΆ`_``1ΒΆΒΆ10ΒΆΒΆ0```````
# ```1010000ΒΆ000ΒΆΒΆ0100_11__1011000ΒΆΒΆ0ΒΆ1_10ΒΆΒΆΒΆ_0ΒΆΒΆ00``````
# 10ΒΆ000000000ΒΆ0________0ΒΆ000000ΒΆΒΆ0000ΒΆΒΆΒΆΒΆ000_0ΒΆ0ΒΆ00`````
# ΒΆΒΆΒΆΒΆΒΆΒΆ0000ΒΆΒΆΒΆΒΆ_`___`_0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ00000000000000_0ΒΆ00ΒΆ01````
# ΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ_``_1ΒΆΒΆΒΆ00000000000000000000_0ΒΆ000ΒΆ01```
# 1__```1ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ00ΒΆΒΆΒΆΒΆ00000000000000000000ΒΆ_0ΒΆ0000ΒΆ0_``
# ```````ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ00000000000000000000010ΒΆ00000ΒΆΒΆ_`
# ```````0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ00000000000000000000ΒΆ10ΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆ0`
# ````````ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆ00000000000000000000010ΒΆΒΆΒΆ0011```
# ````````1ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆ0000000000000000000ΒΆ100__1_`````
# `````````ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ000000000000000000ΒΆ11``_1``````
# `````````1ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆ00000000000000000ΒΆ11___1_`````
# ``````````ΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆ0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0000000000000000ΒΆ11__``1_````
# ``````````ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆ000000000000000ΒΆ1__````__```
# ``````````ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0000000000000000__`````11``
# `````````_ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ000ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ000000000000011_``_1ΒΆΒΆΒΆ0`
# `````````_ΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆ000000ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ000000000000100ΒΆΒΆΒΆΒΆ0_`_
# `````````1ΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆ000000000ΒΆΒΆΒΆΒΆΒΆΒΆ000000000ΒΆ00ΒΆΒΆ01`````
# `````````ΒΆΒΆΒΆΒΆΒΆ0ΒΆ0ΒΆΒΆΒΆ0000000000000ΒΆ0ΒΆ00000000011_``````_
# ````````1ΒΆΒΆ0ΒΆΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆ000000000000000000000ΒΆ11___11111
# ````````ΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆΒΆΒΆ00ΒΆΒΆΒΆΒΆΒΆΒΆ000000000000000000ΒΆ011111111_
# ```````_ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ0000000ΒΆ0ΒΆ00000000000000000ΒΆ01_1111111
# ```````0ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ000000000000000000000000000ΒΆ01___`````
# ```````ΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆ000000000000000000000000000ΒΆ01___1````
# ``````_ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ00000000000000000000000000000011_111```
# ``````0ΒΆΒΆ0ΒΆΒΆΒΆ0ΒΆΒΆ0000000000000000000000000000ΒΆ01`1_11_``
# ``````ΒΆΒΆΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆ0000000000000000000000000000001`_0_11_`
# ``````ΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆΒΆ00000000000000000000000000000ΒΆ01``_0_11`
# ``````ΒΆΒΆΒΆΒΆ0ΒΆΒΆΒΆΒΆ00000000000000000000000000000001```_1_11
``` | instruction | 0 | 29,332 | 15 | 58,664 |
No | output | 1 | 29,332 | 15 | 58,665 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image>
Submitted Solution:
```
n = int(input())
for j in range(n):
x,y= map(int,input().split())
c1,c2,c3,c4,c5,c6= map(int,input().split())
if x==0 and y==0:
print(0)
if x>0 and y==0:
print(min(x*(c3+c1),x*c2))
if y>0 and x==0:
print(min(x*(c6+c4),x*c5))
if x>0 and y>0:
if c6+c2 <c1:
if x>y:
print(y*(c6+c2)+(x-y)*(c6))
else:
print(x*(c6+c2)+(y-x)*(c2))
else:
if x>y:
print(y*(c1)+(x-y)*(c6))
else:
print(x*(c1)+(y-x)*(c2))
if x<0 and y<0:
if c3+c5 <c4:
if x>y:
print(abs(y)*(c3+c5)+abs(x-y)*(c5))
else:
print(abs(x)*(c3+c5)+abs(y-x)*(c3))
else:
if x>y:
print(abs(y)*(c4)+abs(x-y)*(c5))
else:
print(abs(x)*(c4)+abs(y-x)*(c3))
if x>0 and y<0:
print(min(x*c6+abs(y)*c5,x*c6+abs(y)*(c4+c6)))
if x<0 and y>0:
print(min(abs(x)*c3+y*c2,abs(x)*c3+y*(c3+c1)))
``` | instruction | 0 | 29,333 | 15 | 58,666 |
No | output | 1 | 29,333 | 15 | 58,667 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the cell marked (0, 0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0, 0) to (1, 1) will take the exact same cost as going from (-2, -1) to (-1, 0). The costs are given in the input in the order c_1, c_2, c_3, c_4, c_5, c_6 as in the picture below.
<image>
Print the smallest cost of a path from the origin which has coordinates (0, 0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^{4}). Description of the test cases follows.
The first line of each test case contains two integers x and y (-10^{9} β€ x, y β€ 10^{9}) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c_1, c_2, c_3, c_4, c_5, c_6 (1 β€ c_1, c_2, c_3, c_4, c_5, c_6 β€ 10^{9}) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c_3 3 times and c_2 once, amounting to 5+5+5+3=18.
<image>
Submitted Solution:
```
import math as mt
import bisect
import sys
input=sys.stdin.readline
t=int(input())
import collections
import heapq
#t=1
p=10**9+7
def ncr_util():
inv[0]=inv[1]=1
fact[0]=fact[1]=1
for i in range(2,300001):
inv[i]=(inv[i%p]*(p-p//i))%p
for i in range(1,300001):
inv[i]=(inv[i-1]*inv[i])%p
fact[i]=(fact[i-1]*i)%p
def solve(x,y):
for i in range(36):
l[1]=min(l[1],l[2]+l[6])
l[2]=min(l[2],l[1]+l[3])
l[3]=min(l[3],l[2]+l[4])
l[4]=min(l[4],l[3]+l[5])
l[5]=min(l[5],l[4]+l[6])
l[6]=min(l[6],l[1]+l[5])
if x==0 and y==0:
return 0
ans=0
if x>0 and y>0:
mini=min(x,y)
x-=mini
y-=mini
ans+=mini*(l[1])
if y>0:
ans+=y*(l[2])
else:
ans+=x*(l[6])
elif x<0 and y<0:
x=abs(x)
y=abs(y)
mini=min(x,y)
x-=mini
y-=mini
ans+=mini*(l[4])
if y>0:
ans+=y*(l[5])
else:
ans+=x*(l[3])
elif x>0 and y<0:
ans+=abs(x)*l[6]
ans+=abs(y)*l[5]
else:
ans+=abs(y)*l[2]
ans+=abs(x)*l[3]
return ans
for _ in range(t):
#n=int(input())
#s=input()
#n=int(input())
x,y=(map(int,input().split()))
#n1=n
#a=int(input())
#b=int(input())
#n,k,x=map(int,input().l.insert(0,0)
l=list(map(int,input().split()))
l.insert(0,0)
#a,b=map(int,input().split())
#n=int(input())
#s=input()
#s1=input()
#p=input()
#l=list(map(int,input().split()))
#l.sort(revrese=True)
#l2=list(map(int,input().split()))
#l=str(n)
#l.sort(reverse=True)
#l2.sort(reverse=True)
#l1.sort(reverse=True)
#print(ans)
print(solve(x,y))
``` | instruction | 0 | 29,334 | 15 | 58,668 |
No | output | 1 | 29,334 | 15 | 58,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image> | instruction | 0 | 29,349 | 15 | 58,698 |
Tags: brute force, math
Correct Solution:
```
for _ in range(int(input())):
n,u,v=map(int,input().split())
a=list(map(int,input().split()))
flag=0
for i in range(n-1):
if abs(a[i]-a[i+1])>1:
flag=2
break
elif abs(a[i]-a[i+1])==1:
flag=1
if flag==2:
print("0")
elif flag==1:
print(min(u,v))
else:
print(min(2*v,u+v))
``` | output | 1 | 29,349 | 15 | 58,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image> | instruction | 0 | 29,350 | 15 | 58,700 |
Tags: brute force, math
Correct Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, u, v = map(int, input().split())
a = list(map(int, input().split()))
for i in range(n - 1):
if abs(a[i] - a[i + 1]) >= 2: break
else:
if len(set(a)) == 1:
print(min(u, v) + v)
continue
print(min(u, v))
continue
print(0)
``` | output | 1 | 29,350 | 15 | 58,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image> | instruction | 0 | 29,351 | 15 | 58,702 |
Tags: brute force, math
Correct Solution:
```
t=int(input())
for _ in range(t):
n,p,q=map(int,input().split())
ob=list(map(int,input().split()))
c=0
if(len(set(ob))==1):
print(min(p+q,2*q))
else:
for i in range(len(ob)-1):
if(abs(ob[i]-ob[i+1])>1):
c=1
break
if(c):
if(ob[n-2]==1000001 and ob[n-1]==1000000):
print(min(p,q))
else:
print(0)
else:
print(min(p,q))
``` | output | 1 | 29,351 | 15 | 58,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image> | instruction | 0 | 29,352 | 15 | 58,704 |
Tags: brute force, math
Correct Solution:
```
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n,u,v=map(int,input().split())
A=list(map(int,input().split()))
if len(set(A))==1:
print(min(u+v,2*v))
continue
for i in range(1,n):
if abs(A[i]-A[i-1])>=2:
print(0)
break
else:
print(min(u,v))
``` | output | 1 | 29,352 | 15 | 58,705 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image> | instruction | 0 | 29,353 | 15 | 58,706 |
Tags: brute force, math
Correct Solution:
```
R=lambda:map(int,input().split())
t,=R()
exec(t*'n,u,v=R();a=*R(),;print((v+min(u,v),max(0,min(u,*(v*(2-abs(x-y))for x,y in zip(a,a[1:])))))[len({*a})>1]);')
``` | output | 1 | 29,353 | 15 | 58,707 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image> | instruction | 0 | 29,354 | 15 | 58,708 |
Tags: brute force, math
Correct Solution:
```
from collections import Counter, defaultdict, OrderedDict, deque
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from typing import List
import itertools
import math
import heapq
import string
import random
MIN, MAX, MOD = -0x3f3f3f3f, 0x3f3f3f3f, 1000000007
# ------------------- fast io --------------------
import os
import sys
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")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
for _ in range(N()):
n, u, v = RL()
a = RLL()
f = False
for i in range(n - 1):
if abs(a[i] - a[i+1]) > 1:
f = True
print(0)
break
else:
if len(set(a)) == 1: print(min(v + v, u + v))
else:
print(min(u, v))
``` | output | 1 | 29,354 | 15 | 58,709 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image> | instruction | 0 | 29,355 | 15 | 58,710 |
Tags: brute force, math
Correct Solution:
```
for u in range(int(input())):
n, u, v = map(int, input().split())
x = [int(w) for w in input().split()]
ans = 0
t = 0
if(x[0] == 1 and x[1] == 0):
ans += min(u, v)
if(x[n-1] == 1000000 and x[n-2] == 1000001):
ans += min(u, v)
if(ans == 0):
for i in range(1, n):
if(x[i-1] == x[i]):
continue
if(abs(x[i-1] - x[i]) > 1):
t = 1
break
elif(abs(x[i-1] - x[i]) == 1):
t = 2
if(t == 0):
ans += min(u+v, 2*v)
elif(t == 2):
ans += min(u, v)
print(ans)
``` | output | 1 | 29,355 | 15 | 58,711 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image> | instruction | 0 | 29,356 | 15 | 58,712 |
Tags: brute force, math
Correct Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, u, v = map(int, input().split())
a = list(map(int, input().split()))
ans = -1
c = 0
for i in range(n - 1):
c += abs(a[i] - a[i + 1])
if abs(a[i] - a[i + 1]) >= 2:
ans = 0
break
if not ans == 0:
if c:
ans = min(u, v)
else:
ans = min(u + v, 2 * v)
print(ans)
``` | output | 1 | 29,356 | 15 | 58,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image>
Submitted Solution:
```
t=int(input())
for t in range(t):
n,u,v = map(int, input().split())
a=[int(i) for i in input().split()]
prev = a[0]
max_diff = -1
for i in range(1,len(a)):
max_diff = max(max_diff,abs(a[i]-prev))
prev=a[i]
if max_diff == 0:
print(min(u+v,2*v))
elif max_diff == 1:
print(min(u,v))
else:
print(0)
``` | instruction | 0 | 29,357 | 15 | 58,714 |
Yes | output | 1 | 29,357 | 15 | 58,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image>
Submitted Solution:
```
import sys
import math
import heapq
import bisect
from collections import Counter
from collections import defaultdict
from io import BytesIO, IOBase
import string
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
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
self.BUFSIZE = 8192
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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 = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.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:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def get_int():
return int(input())
def get_ints():
return list(map(int, input().split(' ')))
def get_int_grid(n):
return [get_ints() for _ in range(n)]
def get_str():
return input().strip()
def get_strs():
return input().strip().split(' ')
def yes_no(b):
if b:
return "YES"
else:
return "NO"
def binary_search(good, left, right, delta=1, right_true=False):
"""
Performs binary search
----------
Parameters
----------
:param good: Function used to perform the binary search
:param left: Starting value of left limit
:param right: Starting value of the right limit
:param delta: Margin of error, defaults value of 1 for integer binary search
:param right_true: Boolean, for whether the right limit is the true invariant
:return: Returns the most extremal value interval [left, right] which is good function evaluates to True,
alternatively returns False if no such value found
"""
limits = [left, right]
while limits[1] - limits[0] > delta:
if delta == 1:
mid = sum(limits) // 2
else:
mid = sum(limits) / 2
if good(mid):
limits[int(right_true)] = mid
else:
limits[int(~right_true)] = mid
if good(limits[int(right_true)]):
return limits[int(right_true)]
else:
return False
def prefix_sums(a, drop_zero=False):
p = [0]
for x in a:
p.append(p[-1] + x)
if drop_zero:
return p[1:]
else:
return p
def prefix_mins(a, drop_zero=False):
p = [float('inf')]
for x in a:
p.append(min(p[-1], x))
if drop_zero:
return p[1:]
else:
return p
def solve_a():
n, q = get_ints()
a = get_ints()
s = sum(a)
ans = []
for query in range(q):
t, k = get_ints()
if t == 1:
if a[k - 1] == 1:
a[k - 1] = 0
s -= 1
else:
a[k - 1] = 1
s += 1
else:
ans.append(int(s >= k))
return ans
def solve_b():
n, u, v = get_ints()
a = get_ints()
if min(a) == max(a):
return min(v * 2, u + v)
else:
for i in range(1, n):
if abs(a[i] - a[i - 1]) >= 2:
return 0
return min(u, v)
t = get_int()
for _ in range(t):
print(solve_b())
``` | instruction | 0 | 29,358 | 15 | 58,716 |
Yes | output | 1 | 29,358 | 15 | 58,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image>
Submitted Solution:
```
t = int(input())
for k in range(t):
n, u, v = map(int, input().split())
arr = list(map(int, input().split()))
OK = 0
for i in range(0, n - 1):
if abs(arr[i] - arr[i + 1]) >= 2:
OK = 2
break
elif abs(arr[i] - arr[i + 1]) == 1:
OK = 1
if OK == 2:
print(0)
elif OK == 1:
print(min(u, v))
else:
print(min(u + v, v * 2))
``` | instruction | 0 | 29,359 | 15 | 58,718 |
Yes | output | 1 | 29,359 | 15 | 58,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image>
Submitted Solution:
```
t = int(input())
for _ in range(t):
n, u, v = map(int, input().split())
a = [int(i) for i in input().split()]
ans = int(2e9 + 10)
for i in range(n - 1):
if a[i] != a[i + 1] - 1 and a[i] != a[i + 1] and a[i] != a[i + 1] + 1:
ans = 0
break
else:
if a[i] == a[i + 1]:
ans = min(ans, 2 * v)
ans = min(ans, u + v)
if i < n - 2 and a[i + 2] != a[i + 1]:
ans = min(ans, u)
if i > 0 and a[i - 1] != a[i]:
ans = min(ans, u)
else:
ans = min(ans, u)
ans = min(ans, v)
print(ans)
``` | instruction | 0 | 29,360 | 15 | 58,720 |
Yes | output | 1 | 29,360 | 15 | 58,721 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image>
Submitted Solution:
```
itype = int
def inp(): return int(input())
def tinput(): return list(map(itype, input().split(" ")))
for tc in range(int(input())):
n,u,v=tinput()
a=tinput()
ans=v+min(u,v)
for i in range(len(a)-1):
if a[i]!=a[i+1]:
if abs(a[i]-a[i+1])>1:
ans=0
else:
ans-=v
break
print(ans)
``` | instruction | 0 | 29,361 | 15 | 58,722 |
No | output | 1 | 29,361 | 15 | 58,723 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image>
Submitted Solution:
```
def solv():
x, y, z = map(int, input().split())
s = list(map(int, input().split()))
for n in range(1, x):
if abs(s[n]-s[n-1]) >= 2:
print(0)
return
if abs(s[n]-s[n-1]) == 1:
print(min(y, z))
return
print(z+min(y, z))
for _ in range(int(input())):
solv()
``` | instruction | 0 | 29,362 | 15 | 58,724 |
No | output | 1 | 29,362 | 15 | 58,725 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image>
Submitted Solution:
```
if __name__ == '__main__':
for _ in range (int(input())):
n,u,v = map(int,input().split())
l = list(map(int,input().split()))
ans1 = 0
ans2 = 0
if l[-1]==l[-2]:
ans1 = u+v
else:
if u>v:
ans1 = v
else:
ans1=u
print(ans1)
``` | instruction | 0 | 29,363 | 15 | 58,726 |
No | output | 1 | 29,363 | 15 | 58,727 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle β at node (i, a_i). You want to move some obstacles so that you can reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs u or v coins, as below:
* If there is an obstacle in the node (i, j), you can use u coins to move it to (i-1, j) or (i+1, j), if such node exists and if there is no obstacle in that node currently.
* If there is an obstacle in the node (i, j), you can use v coins to move it to (i, j-1) or (i, j+1), if such node exists and if there is no obstacle in that node currently.
* Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from (1,1) to (0,1).
Refer to the picture above for a better understanding.
Now you need to calculate the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains three integers n, u and v (2 β€ n β€ 100, 1 β€ u, v β€ 10^9) β the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^6) β where a_i represents that the obstacle in the i-th row is in node (i, a_i).
It's guaranteed that the sum of n over all test cases doesn't exceed 2 β
10^4.
Output
For each test case, output a single integer β the minimal number of coins you need to spend to be able to reach node (n, 10^6+1) from node (1, 0) by moving through edges of this graph without passing through obstacles.
It can be shown that under the constraints of the problem there is always a way to make such a trip possible.
Example
Input
3
2 3 4
2 2
2 3 4
3 2
2 4 3
3 2
Output
7
3
3
Note
In the first sample, two obstacles are at (1, 2) and (2,2). You can move the obstacle on (2, 2) to (2, 3), then to (1, 3). The total cost is u+v = 7 coins.
<image>
In the second sample, two obstacles are at (1, 3) and (2,2). You can move the obstacle on (1, 3) to (2, 3). The cost is u = 3 coins.
<image>
Submitted Solution:
```
#!/usr/bin/env python3
from sys import stdin as cin
from itertools import accumulate
lmap = lambda f, v: list(map(f, v))
def min_cost(a, u, v):
max_diff = min(abs(x-y) for (x,y) in zip(a[1:], a[:-1]))
if max_diff > 1:
return 0
elif max_diff == 1:
return min(u, v)
else: #max_diff == 0:
return v + min(u, v)
def main():
t = int(next(cin).strip())
for i in range(t):
n, u, v = lmap(int, next(cin).strip().split())
a = lmap(int, next(cin).strip().split())
if t == 3110:
if i == 146:
print(n, u, v)
else:
continue
print(min_cost(a, u, v))
main()
``` | instruction | 0 | 29,364 | 15 | 58,728 |
No | output | 1 | 29,364 | 15 | 58,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 β€ x, y β€ n and ax, y β 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.
Help Okabe determine whether a given lab is good!
Input
The first line of input contains the integer n (1 β€ n β€ 50) β the size of the lab.
The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 β€ ai, j β€ 105).
Output
Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
Examples
Input
3
1 1 2
2 3 1
6 4 1
Output
Yes
Input
3
1 5 2
1 1 1
1 2 3
Output
No
Note
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | instruction | 0 | 29,647 | 15 | 59,294 |
Tags: implementation
Correct Solution:
```
n = int(input())
mat = [list(map(int,input().split())) for i in range(n)]
c = []
for i in range(n):
c.append(set())
for j in range(n):
c[i].add(mat[j][i])
ans = "YES"
for i in range(n):
for j in range(n):
curr = mat[i][j]
if curr==1:continue
for k in range(n):
if curr-mat[i][k] in c[j]:
break
else:
#print(curr,i,j)
ans="NO"
break
print(ans)
``` | output | 1 | 29,647 | 15 | 59,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 β€ x, y β€ n and ax, y β 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.
Help Okabe determine whether a given lab is good!
Input
The first line of input contains the integer n (1 β€ n β€ 50) β the size of the lab.
The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 β€ ai, j β€ 105).
Output
Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
Examples
Input
3
1 1 2
2 3 1
6 4 1
Output
Yes
Input
3
1 5 2
1 1 1
1 2 3
Output
No
Note
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | instruction | 0 | 29,649 | 15 | 59,298 |
Tags: implementation
Correct Solution:
```
from itertools import product
n = int(input())
A = [list(map(int, input().split())) for _ in range(n)]
for i,j in product(range(n), repeat=2):
if A[i][j] > 1 and all(A[i][k]+A[m][j]!=A[i][j]
for k,m in product(range(n), repeat=2)):
print('No')
break
else:
print('Yes')
``` | output | 1 | 29,649 | 15 | 59,299 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 β€ x, y β€ n and ax, y β 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.
Help Okabe determine whether a given lab is good!
Input
The first line of input contains the integer n (1 β€ n β€ 50) β the size of the lab.
The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 β€ ai, j β€ 105).
Output
Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
Examples
Input
3
1 1 2
2 3 1
6 4 1
Output
Yes
Input
3
1 5 2
1 1 1
1 2 3
Output
No
Note
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | instruction | 0 | 29,650 | 15 | 59,300 |
Tags: implementation
Correct Solution:
```
Size = int(input())
Matrix = list()
for i in range(Size):
Matrix.append(list(map(int, input().split())))
for i in range(Size):
for j in range(Size):
Stop = False
if Matrix[i][j] != 1:
for t in range(Size):
for s in range(Size):
if i != t and s != j:
if Matrix[i][j] == Matrix[i][s] + Matrix[t][j]:
Stop = True
break
if Stop == True:
break
if Stop == False:
print("NO")
exit()
print("YES")
``` | output | 1 | 29,650 | 15 | 59,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 β€ x, y β€ n and ax, y β 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.
Help Okabe determine whether a given lab is good!
Input
The first line of input contains the integer n (1 β€ n β€ 50) β the size of the lab.
The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 β€ ai, j β€ 105).
Output
Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
Examples
Input
3
1 1 2
2 3 1
6 4 1
Output
Yes
Input
3
1 5 2
1 1 1
1 2 3
Output
No
Note
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | instruction | 0 | 29,651 | 15 | 59,302 |
Tags: implementation
Correct Solution:
```
def is_good(matrix, n, i, j):
if matrix[i][j] == 1:
return True
row = [matrix[i][s] for s in range(n) if s != j]
col = [matrix[t][j] for t in range(n) if t != i]
for r in row:
for c in col:
if r + c == matrix[i][j]:
return True
return False
def main():
n = int(input())
matrix = [[]] * n
for i in range(n):
matrix[i] = [int(_) for _ in input().split()]
found = False
for i in range(n):
if found:
break
for j in range(n):
if found:
break
found = not is_good(matrix, n, i, j)
print('No' if found else 'Yes')
if __name__ == '__main__':
main()
``` | output | 1 | 29,651 | 15 | 59,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 β€ x, y β€ n and ax, y β 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.
Help Okabe determine whether a given lab is good!
Input
The first line of input contains the integer n (1 β€ n β€ 50) β the size of the lab.
The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 β€ ai, j β€ 105).
Output
Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
Examples
Input
3
1 1 2
2 3 1
6 4 1
Output
Yes
Input
3
1 5 2
1 1 1
1 2 3
Output
No
Note
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | instruction | 0 | 29,653 | 15 | 59,306 |
Tags: implementation
Correct Solution:
```
n = int(input())
l = []
lc = []
for _ in range(n):
lc.append([])
for _ in range(n):
li = list(map(int, input().split()))
l.append(li)
for i in range(n):
lc[i].append(li[i])
for i in range(n):
for j in range(n):
if l[i][j] != 1:
co = 0
for k in l[i]:
for m in lc[j]:
if k + m == l[i][j]:
co = 1
if co == 0:
print('NO')
exit()
print('YES')
``` | output | 1 | 29,653 | 15 | 59,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 β€ x, y β€ n and ax, y β 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.
Help Okabe determine whether a given lab is good!
Input
The first line of input contains the integer n (1 β€ n β€ 50) β the size of the lab.
The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 β€ ai, j β€ 105).
Output
Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case.
Examples
Input
3
1 1 2
2 3 1
6 4 1
Output
Yes
Input
3
1 5 2
1 1 1
1 2 3
Output
No
Note
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | instruction | 0 | 29,654 | 15 | 59,308 |
Tags: implementation
Correct Solution:
```
n = int(input())
lab = []
for i in range(n):
line = list(map(int, input().split()))
lab.append([])
for j in range(n):
lab[i].append(line[j])
flag = True
for i in range(n):
for j in range(n):
x = lab[i][j]
if x != 1:
local_flag = False
for a in [lab[i][y] for y in range(n)]:
for b in [lab[y][j] for y in range(n)]:
if x == a + b:
local_flag = True
if not local_flag:
flag = False
print('Yes') if flag else print('No')
``` | output | 1 | 29,654 | 15 | 59,309 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a square that contains N.
Constraints
* 2 \leq N \leq 10^{12}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum number of moves needed to reach a square that contains the integer N.
Examples
Input
10
Output
5
Input
50
Output
13
Input
10000000019
Output
10000000018 | instruction | 0 | 29,775 | 15 | 59,550 |
"Correct Solution:
```
n=int(input())
s=n+2
for i in range(1,int(n**0.5)+1):
if n%i==0:
x=n//i+i
s=min(s,x)
print(s-2)
``` | output | 1 | 29,775 | 15 | 59,551 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a square that contains N.
Constraints
* 2 \leq N \leq 10^{12}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum number of moves needed to reach a square that contains the integer N.
Examples
Input
10
Output
5
Input
50
Output
13
Input
10000000019
Output
10000000018 | instruction | 0 | 29,776 | 15 | 59,552 |
"Correct Solution:
```
n=int(input())
for i in range(1,int(n**0.5)+1):
if n%i==0:
ans = i+n//i
print(ans-2)
``` | output | 1 | 29,776 | 15 | 59,553 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a square that contains N.
Constraints
* 2 \leq N \leq 10^{12}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum number of moves needed to reach a square that contains the integer N.
Examples
Input
10
Output
5
Input
50
Output
13
Input
10000000019
Output
10000000018 | instruction | 0 | 29,777 | 15 | 59,554 |
"Correct Solution:
```
n = int(input())
a = n+1
for i in range(1,int(n**0.5)+3):
if n%i == 0:
a = min(a, i+n//i)
print(a-2)
``` | output | 1 | 29,777 | 15 | 59,555 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a square that contains N.
Constraints
* 2 \leq N \leq 10^{12}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum number of moves needed to reach a square that contains the integer N.
Examples
Input
10
Output
5
Input
50
Output
13
Input
10000000019
Output
10000000018 | instruction | 0 | 29,778 | 15 | 59,556 |
"Correct Solution:
```
N=int(input())
a=int((N)**(1/2))
b=1
for i in range(1,a+1):
if N%i==0:
b=max(b,i)
j=int(N//b)
print(b+j-2)
``` | output | 1 | 29,778 | 15 | 59,557 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a square that contains N.
Constraints
* 2 \leq N \leq 10^{12}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum number of moves needed to reach a square that contains the integer N.
Examples
Input
10
Output
5
Input
50
Output
13
Input
10000000019
Output
10000000018 | instruction | 0 | 29,779 | 15 | 59,558 |
"Correct Solution:
```
import math
n=int(input())
t=int(math.sqrt(n))
while n%t!=0:
t-=1
a=t
b=n//a
print(str(a+b-2))
``` | output | 1 | 29,779 | 15 | 59,559 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a square that contains N.
Constraints
* 2 \leq N \leq 10^{12}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum number of moves needed to reach a square that contains the integer N.
Examples
Input
10
Output
5
Input
50
Output
13
Input
10000000019
Output
10000000018 | instruction | 0 | 29,780 | 15 | 59,560 |
"Correct Solution:
```
n = int(input())
for i in range(int(n**0.5)+1,0,-1):
if n % i == 0:
print(i + n//i -2)
break
``` | output | 1 | 29,780 | 15 | 59,561 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a square that contains N.
Constraints
* 2 \leq N \leq 10^{12}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum number of moves needed to reach a square that contains the integer N.
Examples
Input
10
Output
5
Input
50
Output
13
Input
10000000019
Output
10000000018 | instruction | 0 | 29,781 | 15 | 59,562 |
"Correct Solution:
```
n=int(input())
a=1
for i in range(2,int(n**.5)+1):
if n%i==0:
a=i
print(a-1+(n//a)-1)
``` | output | 1 | 29,781 | 15 | 59,563 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a square that contains N.
Constraints
* 2 \leq N \leq 10^{12}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum number of moves needed to reach a square that contains the integer N.
Examples
Input
10
Output
5
Input
50
Output
13
Input
10000000019
Output
10000000018 | instruction | 0 | 29,782 | 15 | 59,564 |
"Correct Solution:
```
n = int(input())
f = 1
for i in range(1, int(n**.5)+1):
if n % i == 0:
f = i
print(f-1+n//f-1)
``` | output | 1 | 29,782 | 15 | 59,565 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.