message stringlengths 2 39.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 450 109k | cluster float64 2 2 | __index_level_0__ int64 900 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.
Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.
It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell.
<image>
Input
The only line of the input contains one integer n (0 ≤ n ≤ 109).
Output
Output one integer — the number of hexagons situated not farther than n cells away from a given cell.
Examples
Input
2
Output
19
Submitted Solution:
```
t = int(input())
z = t*(t+1)
z = z//2
z *= 6
z += 1
print(z)
``` | instruction | 0 | 39,128 | 2 | 78,256 |
Yes | output | 1 | 39,128 | 2 | 78,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.
Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.
It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell.
<image>
Input
The only line of the input contains one integer n (0 ≤ n ≤ 109).
Output
Output one integer — the number of hexagons situated not farther than n cells away from a given cell.
Examples
Input
2
Output
19
Submitted Solution:
```
n=int(input())
print(6*n+n*(n-1)*3+1)
``` | instruction | 0 | 39,129 | 2 | 78,258 |
Yes | output | 1 | 39,129 | 2 | 78,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.
Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.
It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell.
<image>
Input
The only line of the input contains one integer n (0 ≤ n ≤ 109).
Output
Output one integer — the number of hexagons situated not farther than n cells away from a given cell.
Examples
Input
2
Output
19
Submitted Solution:
```
n=int(input())
t=3*n
y=n+1
a=t*y
print(1+a)
``` | instruction | 0 | 39,130 | 2 | 78,260 |
Yes | output | 1 | 39,130 | 2 | 78,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.
Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.
It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell.
<image>
Input
The only line of the input contains one integer n (0 ≤ n ≤ 109).
Output
Output one integer — the number of hexagons situated not farther than n cells away from a given cell.
Examples
Input
2
Output
19
Submitted Solution:
```
n=int(input())
if n==0:
print(1)
else:
sum=0
i=6
while n>0:
sum+=i
i*=2
n-=1
print(sum+1)
``` | instruction | 0 | 39,131 | 2 | 78,262 |
No | output | 1 | 39,131 | 2 | 78,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.
Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.
It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell.
<image>
Input
The only line of the input contains one integer n (0 ≤ n ≤ 109).
Output
Output one integer — the number of hexagons situated not farther than n cells away from a given cell.
Examples
Input
2
Output
19
Submitted Solution:
```
n = int(input())
print(int((pow(n, 3) * 2 + n) / 3))
``` | instruction | 0 | 39,132 | 2 | 78,264 |
No | output | 1 | 39,132 | 2 | 78,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.
Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.
It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell.
<image>
Input
The only line of the input contains one integer n (0 ≤ n ≤ 109).
Output
Output one integer — the number of hexagons situated not farther than n cells away from a given cell.
Examples
Input
2
Output
19
Submitted Solution:
```
n=int(input())
print(((6)*((2**n)-1))+1)
``` | instruction | 0 | 39,133 | 2 | 78,266 |
No | output | 1 | 39,133 | 2 | 78,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.
Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.
It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell.
<image>
Input
The only line of the input contains one integer n (0 ≤ n ≤ 109).
Output
Output one integer — the number of hexagons situated not farther than n cells away from a given cell.
Examples
Input
2
Output
19
Submitted Solution:
```
n = int(input())
print(n*(n-1)*3+1)
``` | instruction | 0 | 39,134 | 2 | 78,268 |
No | output | 1 | 39,134 | 2 | 78,269 |
Provide a correct Python 3 solution for this coding contest problem.
problem
Dimension exists from $ 1 $ dimension to $ N $ dimension. AOR Ika can move between dimensions. Specifically, when AOR Ika is in the $ i $ dimension, she can move freely to the $ j $ dimension ($ i> j $), but in the $ k $ dimension ($ i <k $). You need to use magic to move to.
AOR Ika can use $ M $ types of magic and is numbered $ 1, 2, \ dots, M $. The $ i $ th magic allows you to move from the $ a_i $ dimension to the $ b_i $ dimension. This can only be used when you are in the $ a_i $ dimension.
When you go to the $ i $ dimension, you will receive a dimension-specific "curse of dimensionality". AOR Ika takes $ d_i $ damage due to the "curse of dimensionality" of the $ i $ dimension. If you receive the "curse of dimensionality" of the $ i $ dimension, you will not receive the "curse of dimensionality" of the $ j $ dimension ($ i> j $) forever.
Find the minimum total damage inflicted when AOR Ika moves from the $ s $ dimension to the $ t $ dimension. However, AOR Ika-chan has already received the "curse of dimensionality" of the $ s $ dimension, and we will not consider the damage. Also, AOR Ika is guaranteed to be able to go to the $ t $ dimension.
output
Output the minimum value of the total damage received by AOR Ika-chan. Also, output a line break at the end.
Example
Input
3 1 2 3
1 2 3
1 3
Output
3 | instruction | 0 | 39,567 | 2 | 79,134 |
"Correct Solution:
```
# AOJ 2834 Dimension travel
# Python3 2018.7.12 bal4u
INF = 0x7fffffff
import heapq
def dijkstra(V, to, start, goal):
dist = [INF]*V
Q = []
dist[start] = 0
heapq.heappush(Q, (0, start))
while Q:
t, s = heapq.heappop(Q)
if s == goal: break
if dist[s] < t: continue
for e in to[s]:
nt = t
if e > s: nt += d[e]
if dist[e] > nt:
dist[e] = nt
heapq.heappush(Q, (nt, e))
return t
import sys
N, M, s, t = map(int, input().split())
s, t = s-1, t-1
if s >= t: print(0); sys.exit(0)
d = list(map(int, input().split()))
to = [[i-1] if i > 0 else [] for i in range(N)]
for i in range(M):
a, b = map(int, input().split())
to[a-1].append(b-1)
print(dijkstra(N, to, s, t))
``` | output | 1 | 39,567 | 2 | 79,135 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6 | instruction | 0 | 40,559 | 2 | 81,118 |
Tags: brute force, constructive algorithms, greedy, math
Correct Solution:
```
import os,io
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
cases = int(input())
for t in range(cases):
n = int(input())
ab = []
for i in range(n):
a,b = list(map(int,input().split()))
ab.append([a,b])
md = ab[1][0]-max(0,ab[1][0]-ab[0][1])
st = 1
for i in range(1,n):
if md > ab[(i+1)%n][0]-max(0,ab[(i+1)%n][0]-ab[i][1]):
st = (i+1)%n
md = ab[(i+1)%n][0]-max(0,ab[(i+1)%n][0]-ab[i][1])
out = 0
for i in range(n):
p = (st+i)%n
out += ab[p][0]
ab[(p+1)%n][0] = max(0,ab[(p+1)%n][0]-ab[p][1])
print(out)
``` | output | 1 | 40,559 | 2 | 81,119 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6 | instruction | 0 | 40,560 | 2 | 81,120 |
Tags: brute force, constructive algorithms, greedy, math
Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
from itertools import accumulate
def input(): return sys.stdin.buffer.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
out = []
for _ in range(INT()):
N = INT()
AB = []
for i in range(N):
a, b = MAP()
AB.append((a, b))
need = [0] * N
for i in range(N-1, -1, -1):
a, b = AB[i]
na, nb = AB[(i+1)%N]
if b < na:
need[(i+1)%N] = na - b
sm = sum(need)
ans = INF
for i, (a, b) in enumerate(AB):
ans = min(ans, a + sm - need[i])
out.append(str(ans))
print('\n'.join(out))
``` | output | 1 | 40,560 | 2 | 81,121 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6 | instruction | 0 | 40,561 | 2 | 81,122 |
Tags: brute force, constructive algorithms, greedy, math
Correct Solution:
```
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
for _ in range(int(input())):
n=int(input())
a=[]
b=[]
for i in range(n):
aa,bb=map(int,input().split())
a.append(aa)
b.append(bb)
for i in range(n):
if b[i-1]>=a[i]:
b[i-1]=a[i]
print(sum(a)-sum(b)+min(b))
``` | output | 1 | 40,561 | 2 | 81,123 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6 | instruction | 0 | 40,562 | 2 | 81,124 |
Tags: brute force, constructive algorithms, greedy, math
Correct Solution:
```
from math import *
from sys import *
from heapq import *
from collections import defaultdict
from itertools import permutations
import os, sys
from io import IOBase, BytesIO
#input =sys.stdin.buffer.readline
M=10**9+7
def graph_as_tree_1_root():
n=int(input())
dict=defaultdict(list)
for _ in range(n-1):
x,y=list(map(int,input().split()))
dict[x].append(y)
dict[y].append(x)
tree=[[1]]
temp=[1]
vall=0
seen=set([i for i in range(2,n+1)])
lol=[]
while len(seen)>0:
#print(seen)
lol=list(seen)
tt=[]
for x in temp:
aa=[]
for val in dict[x]:
if val in seen:
aa+=[val]
seen.remove(val)
dict[x]=aa
tt+=aa
tree.append(tt)
temp=tt
vall+=1
for x in lol:
dict[x]=[]
print(tree,dict)
def pow(a,b):
res=1
while b>0:
if b&1:
res*=a
a*=a
b>>=1
return res
def powmod(a,b,m):
res=1
while b>0:
if b&1:
res=((res*a)%m)
a=(a*a)%m
b//=2
return res
def inv(a,m):
return powmod(a,m-2,m)
def alldivisors(n) :
list = []
arr=[]
for i in range(1, int(sqrt(n) + 1)) :
if (n % i == 0) :
if (n / i == i) :
arr+=[i]
else :
arr+=[i]
list.append(n//i)
arr+=list[::-1]
return arr
def primefactorisation(n):
potentional_p = 3
itog_list = defaultdict(int)
if n % 2 == 0:
itog_list[2] = 0
while n % 2 == 0:
n = n // 2
itog_list[2] += 1
while n - 1:
if potentional_p > (n**0.5):
itog_list[n] += 1
return itog_list
while n % potentional_p == 0:
n = n // potentional_p
itog_list[potentional_p] += 1
potentional_p += 2
return itog_list
def main():
t=int(input())
for _ in range(t):
n=int(input())
a=[]
b=[]
for __ in range(n):
x,y=list(map(int,input().split()))
a.append(x)
b.append(y)
sm=max(0,a[0]-b[-1])
a[0]-=max(0,a[0]-b[-1])
for i in range(1,n):
sm+=max(0,a[i]-b[i-1])
a[i]-=max(0,a[i]-b[i-1])
#print(a)
stdout.write(str(sm+min(a))+'\n')
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
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()
#threading.Thread(target=main).start()
``` | output | 1 | 40,562 | 2 | 81,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6 | instruction | 0 | 40,563 | 2 | 81,126 |
Tags: brute force, constructive algorithms, greedy, math
Correct Solution:
```
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
#sys.stdin.buffer.readline
for _ in range(int(input())):
n=int(input())
arr=[]
brr=[]
for _ in range(n):
a,b = map(int,input().split())
a=int(a)
b=int(b)
arr.append(a)
brr.append(b)
total=[]
for i in range(n):
total.append(max(0,arr[i]-brr[i-1]))
#print(total)
T = sum(total)
minn = float("inf")
#print(minn)
for j in range(n):
minn= min(minn,T+arr[j]-total[j])
#print(minn
sys.stdout.write(str(minn)+"\n")
``` | output | 1 | 40,563 | 2 | 81,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6 | instruction | 0 | 40,564 | 2 | 81,128 |
Tags: brute force, constructive algorithms, greedy, math
Correct Solution:
```
# ------------------- 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")
# ------------------- fast io --------------------
testcases=int(input())
for j in range(testcases):
n=int(input())
hel=[]
exp=[]
for b in range(n):
vals=list(map(int,input().split()))
hel.append(vals[0])
exp.append(vals[1])
#perhaps go for the 1s that are easiest to kill
#find health after explosions
afex=[]
for k in range(n):
if k==0:
afex.append(hel[0]-exp[-1])
else:
afex.append(hel[k]-exp[k-1])
#an easy lowerbound is to find the sum of the positive afex
lowerb=0
for s in range(len(afex)):
if afex[s]>0:
lowerb+=abs(afex[s])
hel[s]+=-abs(afex[s])
print(lowerb+min(min(exp),min(hel)))
``` | output | 1 | 40,564 | 2 | 81,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6 | instruction | 0 | 40,565 | 2 | 81,130 |
Tags: brute force, constructive algorithms, greedy, math
Correct Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
T=int(input())
for tests in range(T):
n=int(input())
M=[list(map(int,input().split())) for i in range(n)]
ANS=0
for i in range(n):
if M[i][0]>M[i-1][1]:
ANS+=M[i][0]-M[i-1][1]
M[i][0]=M[i-1][1]
MIN=M[0][0]
for i in range(n):
MIN=min(MIN,M[i][0])
print(ANS+MIN)
``` | output | 1 | 40,565 | 2 | 81,131 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6 | instruction | 0 | 40,566 | 2 | 81,132 |
Tags: brute force, constructive algorithms, greedy, math
Correct Solution:
```
import sys
readline = sys.stdin.buffer.readline
T = int(readline())
Ans = [None]*T
inf = 1<<60
for qu in range(T):
N = int(readline())
AB = [tuple(map(int, readline().split())) for _ in range(N)]
A, B = map(list, zip(*AB))
CA = [0]*N
CA[0] = max(0, A[0]-B[N-1])
for i in range(N):
CA[i] = max(0, A[i]-B[i-1])
Sc = sum(CA)
ans = inf
for i in range(N):
ans = min(ans, Sc-CA[i]+A[i])
Ans[qu] = ans
print('\n'.join(map(str, Ans)))
``` | output | 1 | 40,566 | 2 | 81,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6
Submitted Solution:
```
import sys
input=sys.stdin.buffer.readline
t=int(input())
while t:
t-=1
n=int(input())
x=[]
y=[]
for i in range(n):
a,b=map(int,input().split())
x.append(a)
y.append(b)
count=0
for i in range(n):
if x[i]-y[i-1]>0:
count+=(x[i]-y[i-1])
x[i]=y[i-1]
ans=min(x)
print(count+ans)
``` | instruction | 0 | 40,567 | 2 | 81,134 |
Yes | output | 1 | 40,567 | 2 | 81,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
t = int(input())
for qw in range(t):
n = int(input())
cc = 0
pepe = 0
iff = 0
els = 0
counts = 0
for i in range(n):
a,b = map(int,input().split())
if i!=0:
count = bb-a
if count<0:
if cc == 0:
crow = bb
cc = 1
else:
if crow>bb:
crow = bb
iff = 1
counts+=a-bb
else:
if pepe == 0:
hoe = a
pepe = 1
else:
if hoe>a:
hoe = a
els = 1
if i == 0:
swag = a
ver = b
bb = b
aa = a
count = b - swag
if count < 0:
if cc == 0:
crow = b
cc = 1
else:
if crow>bb:
crow = b
iff = 1
counts+=swag - b
else:
if pepe == 0:
hoe = swag
pepe = 1
else:
if hoe>swag:
hoe = swag
els = 1
if iff != 0 and els!=0:
if crow<hoe:
ans = counts + crow
else:
ans = hoe + counts
else:
if iff == 0:
ans = hoe
if els == 0:
ww =crow
ans = ww + counts
print(ans)
``` | instruction | 0 | 40,568 | 2 | 81,136 |
Yes | output | 1 | 40,568 | 2 | 81,137 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
n = int(input())
a, b = [], []
for i in range(n):
x, y = map(int, input().split())
a.append(x)
b.append(y)
c = [0 for _ in range(n)]
c_sum = 0
for i in range(n):
if i == 0:
x = max(0, a[0] - b[n-1])
else:
x = max(0, a[i] - b[i-1])
c[i] = x
c_sum += x
ans = 10 ** 20
for i in range(n):
ans = min(ans, c_sum - c[i] + a[i])
print(ans)
``` | instruction | 0 | 40,569 | 2 | 81,138 |
Yes | output | 1 | 40,569 | 2 | 81,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6
Submitted Solution:
```
def main_function():
import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys
for _ in range(int(input())):
n = int(input())
memo = [list(map(int, input().split())) for i in range(n)]
total_damage_with_explosion = 0
for i in range(n):
if memo[i][0] > memo[i - 1][1]:
total_damage_with_explosion += memo[i][0] - memo[i - 1][1]
ans = float("inf")
for i in range(n):
if memo[i][0] > memo[i - 1][1]:
ans = min(ans, total_damage_with_explosion + memo[i - 1][1])
else:
ans = min(ans, total_damage_with_explosion + memo[i][0])
sys.stdout.write(str(ans) + "\n")
if __name__ == '__main__':
main_function()
``` | instruction | 0 | 40,570 | 2 | 81,140 |
Yes | output | 1 | 40,570 | 2 | 81,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6
Submitted Solution:
```
import sys
I=sys.stdin.buffer.readline
input=lambda:stdin.readline()
for _ in range(int(I())):
n=int(I())
ans,mn=0,1000000
ai,bi=map(int,I().split())
for i in range(n-1):
na,nb=map(int,I().split())
if na>bi:
ans+=na-bi
mn=min(mn,bi,na)
bi=nb
if ai>bi:
ans+=ai-bi
mn=min(mn,bi,ai)
print(ans+mn)
``` | instruction | 0 | 40,571 | 2 | 81,142 |
No | output | 1 | 40,571 | 2 | 81,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
arr = []
for i in range(n):
arr.append(list(map(int,input().split())))
for i in range(n):
ans = 0
ans += arr[i][0]
carry = arr[i][1]
for j in range(i+1,n+1+i):
health = arr[j%n][0] - carry
if health > 0:
ans += health
carry = arr[j%n][1]
if i == 0:
mn = ans
else:
mn = min(ans,mn)
``` | instruction | 0 | 40,572 | 2 | 81,144 |
No | output | 1 | 40,572 | 2 | 81,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
count=0
a=[0]*n
b=[0]*n
minimum=10**12+1
for l in range(n):
a[l],b[l]=map(int,input().split())
minimum=min(minimum,a[l])
for i in range(n):
if a[i]-b[i-1]>0:
count+=a[i]-b[i-1]
count+=minimum
print(count)
``` | instruction | 0 | 40,573 | 2 | 81,146 |
No | output | 1 | 40,573 | 2 | 81,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.
You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.
You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.
Input
The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.
Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.
It is guaranteed that the total number of monsters in all test cases does not exceed 300000.
Output
For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.
Example
Input
1
3
7 15
2 14
5 3
Output
6
Submitted Solution:
```
import sys
input=sys.stdin.readline
from collections import *
from math import *
t=int(input())
while(t):
t-=1
n=int(input())
d=[]
for i in range(n):
a,b=map(int,input().split())
d.append([a,b])
r=[]
if(n==1):
print(d[0][0])
continue
for i in range(n):
if(i==0):
r.append(d[0][0]-d[-1][1])
if(i==n-1):
r.append(d[-1][0]-d[0][1])
elif(i!=0 and i!=n-1):
r.append(d[i][0]-d[i+1][1])
s=0
cc=0
for i in r:
if(i>0):
s+=i
d[cc][0]-=i
cc+=1
x=1000000000000000000000000000000
for i in d:
x=min(x,i[0])
print(s+x)
``` | instruction | 0 | 40,574 | 2 | 81,148 |
No | output | 1 | 40,574 | 2 | 81,149 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | instruction | 0 | 40,607 | 2 | 81,214 |
Tags: dp, greedy, implementation
Correct Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n,r1,r2,r3,d = map(int,input().split())
a = list(map(int,input().split()))
dp = [0,float("inf")]
for i in range(1,n+1):
na = a[i-1]
ndp = [float("inf"),float("inf")]
c1 = r1*na+r3
c2 = min(r1*(na+2) , r2+r1)
if i != n:
ndp[0] = min( dp[0]+c1 , dp[1]+min(c1,c2) )
ndp[1] = dp[0] + c2 + 2*d
if i == n:
print (min(dp[0]+c1,dp[0]+c2+2*d,dp[1]+c2,dp[1]+c1-d) + d*(n-1))
dp = ndp
``` | output | 1 | 40,607 | 2 | 81,215 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | instruction | 0 | 40,608 | 2 | 81,216 |
Tags: dp, greedy, implementation
Correct Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
n, r1, r2, r3, d = rint()
a = list(rint())
printd(n, r1, r2, r3, d)
printd(a)
dp = [[10**20, 10**20] for i in range(n)]
dp[0][0] = r1*a[0] + r3
dp[0][1] = min(r1*a[0] + r1, r2)
for i in range(1, n):
# 0 -> 0
dp[i][0] = min(dp[i][0], dp[i-1][0] + d + r1*a[i] + r3)
# 1 -> 0
dp[i][0] = min(dp[i][0], dp[i-1][1] + 3*d + r1*(a[i]+1) + r3)
dp[i][0] = min(dp[i][0], dp[i-1][1] + 3*d + r1*(a[i]+3))
dp[i][0] = min(dp[i][0], dp[i-1][1] + 3*d + 2*r1 + r2)
if i == n - 1:
dp[i][0] = min(dp[i][0], dp[i-1][1] + 2*d + r1*(a[i]+1) + r3)
# 0 -> 1
dp[i][1] = min(dp[i][1], dp[i-1][0] + d + r1*(a[i]+1))
dp[i][1] = min(dp[i][1], dp[i-1][0] + d + r2)
# 1 -> 1
dp[i][1] = min(dp[i][1], dp[i-1][1] + 3*d + r1*(a[i]+2))
dp[i][1] = min(dp[i][1], dp[i-1][1] + 3*d + r1 + r2)
printd(dp)
print(dp[n-1][0])
``` | output | 1 | 40,608 | 2 | 81,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | instruction | 0 | 40,609 | 2 | 81,218 |
Tags: dp, greedy, implementation
Correct Solution:
```
import sys
import math
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline
N, r1, r2, r3, d = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
# killed
X = [None for i in range(N)]
# injured
Y = [None for i in range(N)]
X[0] = A[0] * r1 + r3
Y[0] = min(r2, A[0] * r1 + r1)
for i in range(1, N):
# kill monsters with pistol and boss with AWP
x = A[i] * r1 + r3
# kill everyone with laser, injure boss
y = r2
# kill monsters with pistol, injure boss with pistol
z = A[i] * r1 + r1
X[i] = min(
X[i-1] + d + x,
Y[i-1] + d + x + d + r1 + (d if i != N - 1 else 0),
Y[i-1] + d + min(y, z) + d + r1 + d + r1
)
Y[i] = min(
X[i - 1] + d + min(y, z),
Y[i - 1] + d + min(y, z) + d + r1 + d
)
print(X[N-1])
``` | output | 1 | 40,609 | 2 | 81,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | instruction | 0 | 40,610 | 2 | 81,220 |
Tags: dp, greedy, implementation
Correct Solution:
```
import pprint
n, r1, r2, r3, d = map(int, input().split())
*arr, = map(int, input().split())
dp = [[0, 0] for _ in range(n + 1)]
dp[0][0] = -d
dp[0][1] = 2 * n * r2 + 2 * n * d
for i in range(n):
fast_kill = arr[i] * r1 + r3
slow_kill = min((arr[i] + 2) * r1, r2 + r1)
# print(i, arr[i], fast_kill, slow_kill)
extra = -d if i == n - 1 else 0
dp[i + 1][0] = min(dp[i][0] + fast_kill, dp[i][1] + fast_kill + extra, dp[i][1] + slow_kill) + d
dp[i + 1][1] = dp[i][0] + slow_kill + 3 * d
# pprint.pprint(dp)
print(min(dp[i + 1]))
``` | output | 1 | 40,610 | 2 | 81,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | instruction | 0 | 40,611 | 2 | 81,222 |
Tags: dp, greedy, implementation
Correct Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
n, r1, r2, r3, d = rint()
a = list(rint())
printd(n, r1, r2, r3, d)
printd(a)
dp = [[10**100, 10**100] for i in range(n)]
dp[0][0] = r1*a[0] + r3
dp[0][1] = min(r1*a[0] + r1, r2)
for i in range(1, n):
# 0 -> 0
dp[i][0] = min(dp[i][0], dp[i-1][0] + d + r1*a[i] + r3)
# 1 -> 0
dp[i][0] = min(dp[i][0], dp[i-1][1] + d + r1*a[i] + r3 + d + r1 + d)
dp[i][0] = min(dp[i][0], dp[i-1][1] + d + r1*a[i] + r1 + d + r1 + d + r1)
dp[i][0] = min(dp[i][0], dp[i-1][1] + d + r2 + d + r1 + d + r1)
# 0 -> 1
dp[i][1] = min(dp[i][1], dp[i-1][0] + d + r1*a[i] + r1)
dp[i][1] = min(dp[i][1], dp[i-1][0] + d + r2)
# 1 -> 1
dp[i][1] = min(dp[i][1], dp[i-1][1] + d + r1*a[i] + r1 + d + r1 + d)
dp[i][1] = min(dp[i][1], dp[i-1][1] + d + r2 + d + r1 + d)
if i == n - 1:
dp[i][0] = min(dp[i][0], dp[i-1][1] + d + r1*a[i] + r3 + d + r1)
printd(dp)
print(dp[n-1][0])
``` | output | 1 | 40,611 | 2 | 81,223 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | instruction | 0 | 40,612 | 2 | 81,224 |
Tags: dp, greedy, implementation
Correct Solution:
```
import pprint
n, r1, r2, r3, d = map(int, input().split())
*arr, = map(int, input().split())
dp = [[0] * (n + 1) for _ in range(2)]
dp[0][0] = -d
dp[1][0] = 2 * n * r2 + 2 * n * d
for i in range(n):
fast_kill = arr[i] * r1 + r3
slow_kill = min((arr[i] + 2) * r1, r2 + r1)
# print(i, arr[i], fast_kill, slow_kill)
extra = -d if i == n - 1 else 0
dp[0][i + 1] = min(dp[0][i] + fast_kill, dp[1][i] + fast_kill + extra, dp[1][i] + slow_kill) + d
dp[1][i + 1] = dp[0][i] + slow_kill + 3 * d
# pprint.pprint(dp)
print(min(dp[0][-1], dp[1][-1]))
``` | output | 1 | 40,612 | 2 | 81,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | instruction | 0 | 40,613 | 2 | 81,226 |
Tags: dp, greedy, implementation
Correct Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n,r1,r2,r3,d = map(int,input().split())
a = list(map(int,input().split()))
dp = [0,float("inf")]
for i in range(1,n+1):
na = a[i-1]
ndp = [float("inf"),float("inf")]
c1 = r1*na+r3
c2 = min(r1*(na+2) , r2+r1)
if i != n:
ndp[0] = min( dp[0]+c1 , dp[1]+min(c1,c2) )
ndp[1] = dp[0] + c2 + 2*d
if i == n:
print (min(dp[0]+c1,dp[0]+c2+2*d,dp[1]+c2,dp[1]+c1-d) + d*(n-1))
dp = ndp
#print(resArray,pipa)
#1 3 2 4
#1 12 8 12
#4
# 11 10 9 8
# 0 6 5 5
# 6 5 5
# 6 1 1
#
``` | output | 1 | 40,613 | 2 | 81,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed. | instruction | 0 | 40,614 | 2 | 81,228 |
Tags: dp, greedy, implementation
Correct Solution:
```
import pprint
n, r1, r2, r3, d = map(int, input().split())
*arr, = map(int, input().split())
dp = [[0] * (n + 1) for _ in range(2)]
dp[0][0] = -d
dp[1][0] = 2 * n * r2 + 2 * n * d
for i in range(n):
fast_kill = arr[i] * r1 + r3
slow_kill = min((arr[i] + 2) * r1, r2 + r1)
# print(i, arr[i], fast_kill, slow_kill)
extra = -d * (i == n - 1)
dp[0][i + 1] = min(dp[0][i] + fast_kill, dp[1][i] + fast_kill + extra, dp[1][i] + slow_kill) + d
dp[1][i + 1] = dp[0][i] + slow_kill + 3 * d
# pprint.pprint(dp)
print(min(dp[0][-1], dp[1][-1]))
``` | output | 1 | 40,614 | 2 | 81,229 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def snipe(i):
global r1,r2,r3,arr
return r1*arr[i]+r3
def laser(i):
global r1,r2,r3,arr
return min(r1*(arr[i]+1),r2)
n,r1,r2,r3,d = list(map(int,input().split()))
arr = list(map(int,input().split()))
dp = [[sys.maxsize for j in range(2)] for i in range(n)]
dp[0][0]=snipe(0)+d
dp[0][1]=laser(0)+d
for i in range(1,n-1):
dp[i][0]=min(dp[i-1][0]+snipe(i)+d, dp[i-1][1]+laser(i)+2*r1+3*d)
dp[i][1]=dp[i-1][0]+laser(i)+d
i = n-1
ans = min(dp[i-1][0]+snipe(i), dp[i-1][1]+laser(i)+2*r1+2*d, dp[i-1][1]+snipe(i)+r1+d, dp[i-1][0]+laser(i)+2*d+r1)
print(ans)
``` | instruction | 0 | 40,615 | 2 | 81,230 |
Yes | output | 1 | 40,615 | 2 | 81,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed.
Submitted Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
n, r1, r2, r3, d = rint()
a = list(rint())
printd(n, r1, r2, r3, d)
printd(a)
dp = [[10**20, 10**20] for i in range(n)]
dp[0][0] = r1*a[0] + r3
dp[0][1] = min(r1*a[0] + r1, r2)
for i in range(1, n):
dp[i][0] = min(dp[i-1][0] + d + r1*a[i] + r3\
,dp[i-1][1] + 3*d + r1*(a[i]+1) + r3\
,dp[i-1][1] + 3*d + r1*(a[i]+3)\
,dp[i-1][1] + 3*d + 2*r1 + r2)
dp[i][1] = min(dp[i-1][0] + d + r1*(a[i]+1)\
,dp[i-1][0] + d + r2\
,dp[i-1][1] + 3*d + r1*(a[i]+2)\
,dp[i-1][1] + 3*d + r1 + r2)
i = n-1
dp[i][0] = min(dp[i][0], dp[i - 1][1] + 2 * d + r1 * (a[i] + 1) + r3)
printd(dp)
print(dp[n-1][0])
``` | instruction | 0 | 40,616 | 2 | 81,232 |
Yes | output | 1 | 40,616 | 2 | 81,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed.
Submitted Solution:
```
N, R1, R2, R3, D = map(int, input().split())
A = list(map(int, input().split()))
# print(N, R1, R2, R3, D, A)
dp = [[0, 0] for _ in A]
dp[0][0] = R1 * A[0] + R3
dp[0][1] = min(R2, R1 * A[0] + R1)
for i, a in enumerate(A[1:-1], 1):
dp[i][0] = min(dp[i - 1][0] + D + R1 * A[i] + R3, dp[i - 1][1] + D
+ min(R1 * A[i] + R3 + 2 * D + R1, R2 + 2 * D + 2 * R1, R1 * A[i] + 2 * R1 + 2 * D + R1))
dp[i][1] = min(dp[i - 1][0] + D + min(R1 * A[i] + R1, R2), dp[i - 1][1] + D
+ min(R1 * A[i] + 2 * D + 2 * R1, R2 + 2 * D + R1))
dp[-1][0] = min(dp[-2][0] + D + min(R1 * A[-1] + R3, R2 + 2 * D + R1, R1 * A[-1] + 2 * R1 + 2 * D),
dp[-2][1] + D + min(R1 * A[-1] + R3 + D + R1, R2 + 2 * D + 2 * R1, R1 * A[-1] + R1 + 2 * D + 2 * R1))
print(dp[-1][0])
``` | instruction | 0 | 40,617 | 2 | 81,234 |
Yes | output | 1 | 40,617 | 2 | 81,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed.
Submitted Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
n, r1, r2, r3, d = rint()
a = list(rint())
printd(n, r1, r2, r3, d)
printd(a)
dp = [[10**20, 10**20] for i in range(n)]
dp[0][0] = r1*a[0] + r3
dp[0][1] = min(r1*a[0] + r1, r2)
for i in range(1, n):
dp[i][0] = min(dp[i-1][0] + d + r1*a[i] + r3\
,dp[i-1][1] + 3*d + r1*(a[i]+1) + r3\
,dp[i-1][1] + 3*d + r1*(a[i]+3)\
,dp[i-1][1] + 3*d + 2*r1 + r2)
if i == n - 1:
dp[i][0] = min(dp[i][0], dp[i-1][1] + 2*d + r1*(a[i]+1) + r3)
dp[i][1] = min(dp[i-1][0] + d + r1*(a[i]+1)\
,dp[i-1][0] + d + r2\
,dp[i-1][1] + 3*d + r1*(a[i]+2)\
,dp[i-1][1] + 3*d + r1 + r2)
printd(dp)
print(dp[n-1][0])
``` | instruction | 0 | 40,618 | 2 | 81,236 |
Yes | output | 1 | 40,618 | 2 | 81,237 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def snipe(i):
global r1,r2,r3,arr
return r1*arr[i]+r3
def laser(i):
global r1,r2,r3,arr
return min(r1*(arr[i]+1),r2)
n,r1,r2,r3,d = list(map(int,input().split()))
arr = list(map(int,input().split()))
dp = [[sys.maxsize for j in range(2)] for i in range(n)]
dp[0][0]=snipe(0)+d
dp[0][1]=laser(0)+d
for i in range(1,n-1):
dp[i][0]=min(dp[i-1][0]+snipe(i)+d, dp[i-1][1]+laser(i)+2*r1+3*d)
dp[i][1]=min(dp[i-1][0]+laser(i)+d,dp[i-1][1]+snipe(i)+2*d+r1)
i = n-1
ans = min(dp[i-1][0]+snipe(i), dp[i-1][1]+laser(i)+2*r1+2*d, dp[i-1][1]+snipe(i)+r1+d)
print(ans)
``` | instruction | 0 | 40,619 | 2 | 81,238 |
No | output | 1 | 40,619 | 2 | 81,239 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed.
Submitted Solution:
```
import sys
import math
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline
N, r1, r2, r3, d = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
# killed
X = [None for i in range(N)]
# injured
Y = [None for i in range(N)]
X[0] = A[0] * r1 + r3
Y[0] = min(r2, A[0] * r1 + r1)
for i in range(1, N):
# kill monsters with pistol and boss with AWP
x = A[i] * r1 + r3
# kill everyone with laser, injure boss
y = r2
# kill monsters with pistol, injure boss with pistol
z = A[i] * r1 + r1
X[i] = min(
X[i-1] + x + d,
Y[i-1] + x + 2*d + r1,
Y[i-1] + min(y, z) + 3*d + 2 * r1
)
Y[i] = min(
X[i - 1] + d + min(y, z),
Y[i - 1] + 3*d + min(y, z) + r1
)
print(X[N-1])
``` | instruction | 0 | 40,620 | 2 | 81,240 |
No | output | 1 | 40,620 | 2 | 81,241 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed.
Submitted Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
n, r1, r2, r3, d = rint()
a = list(rint())
printd(n, r1, r2, r3, d)
printd(a)
dp = [[10**20, 10**20] for i in range(n)]
dp[0][0] = r1*a[0] + r3
dp[0][1] = min(r1*a[0] + r1, r2)
for i in range(1, n):
# 0 -> 0
dp[i][0] = min(dp[i][0], dp[i-1][0] + d + r1*a[i] + r3)
# 1 -> 0
dp[i][0] = min(dp[i][0], dp[i-1][1] + 3*d + r1*(a[i]+1) + r3)
dp[i][0] = min(dp[i][0], dp[i-1][1] + 3*d + r1*(a[i]+3))
dp[i][0] = min(dp[i][0], dp[i-1][1] + 3*d + 2*r1 + r2)
if i == n - 1:
dp[i][0] = min(dp[i][0], dp[i-1][1] + 2*d + r1*(a[i]+1) + r3)
# 0 -> 1
dp[i][1] = min(dp[i][1], dp[i-1][0] + d + r1*(a[i]+1))
dp[i][1] = min(dp[i][1], dp[i-1][0] + d + r2)
# 1 -> 1
dp[i][1] = min(dp[i][1], dp[i-1][1] + 3*d + r1*(a[i]+2))
dp[i][1] = min(dp[i][1], dp[i-1][1] + 2*d + r1 + r2)
printd(dp)
print(dp[n-1][0])
``` | instruction | 0 | 40,621 | 2 | 81,242 |
No | output | 1 | 40,621 | 2 | 81,243 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ziota found a video game called "Monster Invaders".
Similar to every other shooting RPG game, "Monster Invaders" involves killing monsters and bosses with guns.
For the sake of simplicity, we only consider two different types of monsters and three different types of guns.
Namely, the two types of monsters are:
* a normal monster with 1 hp.
* a boss with 2 hp.
And the three types of guns are:
* Pistol, deals 1 hp in damage to one monster, r_1 reloading time
* Laser gun, deals 1 hp in damage to all the monsters in the current level (including the boss), r_2 reloading time
* AWP, instantly kills any monster, r_3 reloading time
The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.
The levels of the game can be considered as an array a_1, a_2, …, a_n, in which the i-th stage has a_i normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the a_i normal monsters.
If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level i (1 < i < n) are levels i - 1 and i + 1, the only adjacent level of level 1 is level 2, the only adjacent level of level n is level n - 1). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with d teleportation time.
In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation.
Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
Input
The first line of the input contains five integers separated by single spaces: n (2 ≤ n ≤ 10^6) — the number of stages, r_1, r_2, r_3 (1 ≤ r_1 ≤ r_2 ≤ r_3 ≤ 10^9) — the reload time of the three guns respectively, d (1 ≤ d ≤ 10^9) — the time of moving between adjacent levels.
The second line of the input contains n integers separated by single spaces a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6, 1 ≤ i ≤ n).
Output
Print one integer, the minimum time to finish the game.
Examples
Input
4 1 3 4 3
3 2 5 1
Output
34
Input
4 2 4 4 1
4 5 1 2
Output
31
Note
In the first test case, the optimal strategy is:
* Use the pistol to kill three normal monsters and AWP to kill the boss (Total time 1⋅3+4=7)
* Move to stage two (Total time 7+3=10)
* Use the pistol twice and AWP to kill the boss (Total time 10+1⋅2+4=16)
* Move to stage three (Total time 16+3=19)
* Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time 19+3+3=25)
* Use the pistol once, use AWP to kill the boss (Total time 25+1⋅1+4=30)
* Move back to stage three (Total time 30+3=33)
* Kill the boss at stage three with the pistol (Total time 33+1=34)
Note that here, we do not finish at level n, but when all the bosses are killed.
Submitted Solution:
```
#!/usr/bin/env python3
import io
import os
import sys
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def printd(*args, **kwargs):
#print(*args, **kwargs, file=sys.stderr)
#print(*args, **kwargs)
pass
def get_str():
return input().decode().strip()
def rint():
return map(int, input().split())
def oint():
return int(input())
n, r1, r2, r3, d = rint()
a = list(rint())
print(n, r1, r2, r3, d)
print(a)
dp = [[0, 0] for i in range(n)]
dp[0][0] = r1*a[0] + r3
dp[0][1] = min(r1*(a[0]+1), r2)
for i in range(1, n):
from0 = dp[i-1][0] + r1*a[i] + r3 + d
from1 = dp[i-1][1] + r1 + d*3 +\
min(r1*a[i] + r3, r1*(a[i]+1)+r1, r2+r1)
dp[i][0] = min(from0, from1)
from0 = dp[i-1][0] + min(r1*(a[i]+1), r2) + d
dp[i][1] = from0
print(dp)
if n == 1:
ans = dp[0][0]
else:
ans = min(dp[n-1][0], dp[n-2][1] + r1+ 2*d + r1*a[i]+r3 )
print(ans)
``` | instruction | 0 | 40,622 | 2 | 81,244 |
No | output | 1 | 40,622 | 2 | 81,245 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters. | instruction | 0 | 40,910 | 2 | 81,820 |
Tags: dfs and similar, graphs, greedy
Correct Solution:
```
import sys
n, m = map(int, input().split())
cost = [int(x) for x in input().split()]
adj = [[] for _ in range(n)]
for i in range(m):
u, v = map(int, input().split())
adj[u-1].append(v-1)
adj[v-1].append(u-1)
visited = [0 for x in range(n)]
minCost = -1 - sys.maxsize
def dfs(v):
l = [v]
minCost = cost[v]
while l:
top = l.pop()
visited[top] = 1
minCost = min(minCost, cost[top])
for u in adj[top]:
if visited[u] == 0:
l.append(u)
return minCost
res = 0
for vertex in range(n):
if visited[vertex] == 0:
res += dfs(vertex)
print(res)
``` | output | 1 | 40,910 | 2 | 81,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters. | instruction | 0 | 40,911 | 2 | 81,822 |
Tags: dfs and similar, graphs, greedy
Correct Solution:
```
n,q=[int(i) for i in input().split()]
l=[int(i) for i in input().split()]
frie=[i for i in range(n)]
def find(a):
while frie[a]!=a:
frie[a]=frie[frie[a]]
a=frie[a]
return a
for i in range(q):
s=[int(i) for i in input().split()]
x,y=s[0]-1,s[1]-1
x,y=find(x),find(y)
if l[x]>l[y]:
x,y=y,x
frie[y]=x
visited=[False for i in range(n)]
for i in range(n-1,-1,-1):
if frie[i]!=i:
j=i
mi=l[j]
visited[j]=True
while frie[j]!=j and not visited[j]:
frie[j]=frie[frie[j]]
mi=min(mi,l[j])
j=frie[j]
visited[j]=True
l[j]=min(mi,l[j])
# print(frie)
print(sum(l[frie[j]] for j in range(n) if frie[j] == j))
``` | output | 1 | 40,911 | 2 | 81,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters. | instruction | 0 | 40,912 | 2 | 81,824 |
Tags: dfs and similar, graphs, greedy
Correct Solution:
```
n,m = map(int,input().strip().split())
arr = list(map(int,input().strip().split()))
gr = [[] for i in range(n)]
def dfs():
stack = []
par = {}
ans = []
for i in range(n):
if i+1 not in par:
par[i+1] = 0
stack.append(i+1)
temp = []
while stack:
#print(stack)
u = stack.pop()
temp.append(arr[u-1])
#print(u,end=" ")
for v in gr[u-1]:
if v not in par:
stack.append(v)
par[v] = u
ans.append(min(temp))
#print(ans)
print(sum(ans))
for i in range(m):
x,y = map(int,input().strip().split())
gr[x-1].append(y)
gr[y-1].append(x)
dfs()
``` | output | 1 | 40,912 | 2 | 81,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters. | instruction | 0 | 40,913 | 2 | 81,826 |
Tags: dfs and similar, graphs, greedy
Correct Solution:
```
class Person:
relations: [int]
def __init__(self, price, index):
self.index = index
self.price = price
self.relations = []
def dfs(persons):
queue = persons[:]
visited = set()
cost = 0
while queue:
s: Person = queue.pop(0)
if s.index not in visited:
stack = s.relations
visited.add(s.index)
min_price = s.price
while stack:
element = stack.pop()
if element not in visited:
visited.add(element)
p: Person = persons[element]
min_price = min(min_price, p.price)
stack += p.relations
cost += min_price
print(cost)
m, n = [int(x) for x in input().split()]
prices = [int(x) for x in input().split()]
pairs = []
for i in range(n):
p1, p2 = [int(x) for x in input().split()]
pairs.append((p1, p2))
# Process inputs
persons = []
for i in range(m):
persons.append(Person(prices[i], i))
for p1, p2 in pairs:
persons[p1 - 1].relations.append(p2 - 1)
persons[p2 - 1].relations.append(p1 - 1)
dfs(persons)
``` | output | 1 | 40,913 | 2 | 81,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters. | instruction | 0 | 40,914 | 2 | 81,828 |
Tags: dfs and similar, graphs, greedy
Correct Solution:
```
def answer(n,m,A,g):
cost=0
vis=[0]*(n+1)
for i in range(1,n+1):
if vis[i]==0:
mini=A[i-1]
s=[i]
vis[i]=1
while s:
item=s.pop()
mini=min(mini,A[item-1])
for node in g[item]:
if vis[node]==0:
s.append(node)
vis[node]=1
cost+=mini
return cost
n,m=map(int,input().split())
A=list(map(int,input().split()))
g=[[] for j in range(n+1)]
for i in range(m):
u,v=map(int,input().split())
g[u].append(v)
g[v].append(u)
print(answer(n,m,A,g))
``` | output | 1 | 40,914 | 2 | 81,829 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters. | instruction | 0 | 40,915 | 2 | 81,830 |
Tags: dfs and similar, graphs, greedy
Correct Solution:
```
from collections import defaultdict
I = lambda: map(int, input().split())
n, m = I()
C = list(I())
G = defaultdict(set)
for _ in range(m):
x, y = I()
G[x].add(y)
G[y].add(x)
visited, groups = set(), []
for u in range(1, n+1):
if u not in visited:
stack, group = [u], []
while stack:
u = stack.pop()
visited.add(u)
group.append(u)
for v in G[u]:
if v not in visited:
stack.append(v)
groups.append(group)
print(sum(min(C[u-1] for u in group) for group in groups))
``` | output | 1 | 40,915 | 2 | 81,831 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters. | instruction | 0 | 40,916 | 2 | 81,832 |
Tags: dfs and similar, graphs, greedy
Correct Solution:
```
class UnionFind():
def __init__(self, n ,cost):
self.table = [i for i in range(n)]
self.rank = [1 for _ in range(n)]
self.cost = cost[:]
self.cost.insert(0,0)
def unite(self, x, y):
# x,yの根を格納する
x = self.find(x)
y = self.find(y)
#同一グラフ内だった場合
if x == y:
return False
if self.rank[x] > self.rank[y]:
self.table[y] = x
else:
self.table[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
if self.cost[x] > self.cost[y]:
self.cost[x] = self.cost[y]
else:
self.cost[y] = self.cost[x]
def find(self, x):
""" xの根の値を返すメソッド """
#xが根である時
if self.table[x] == x:
return x
#xが根でない ==> 根へたどる&根へつけかえる
self.table[x] = self.find(self.table[x])
#根の値を返す
return self.table[x]
def SameGragh(self,x,y):
"""二つの値が同一グラフ内だった場合:True,違う場合:False"""
if self.find(x) == self.find(y):
return True
else:
return False
def sumCost(self):
sumval = 0
for i in range(1,n+1):
if self.table[i] == i:
sumval += self.cost[i]
return sumval
if __name__ == "__main__":
n,m = map(int,input().split(" "))
cost = list(map(int,input().split(" ")))
group = UnionFind(n+1,cost)
used = [0] * (n+1)
for i in range(m):
a,b = map(int,input().split(" "))
if not group.SameGragh(a,b):
group.unite(a,b)
print(group.sumCost())
``` | output | 1 | 40,916 | 2 | 81,833 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters. | instruction | 0 | 40,917 | 2 | 81,834 |
Tags: dfs and similar, graphs, greedy
Correct Solution:
```
from collections import defaultdict as dd
g=dd(list)
def addE(u,v):
g[u].append(v)
g[v].append(u)
def dfs(v):
stck=[]
stck.append(v)
cost1=cost[v-1]
visited[v]=True
while len(stck)!=0:
cur=stck.pop()
for ch in g[cur]:
if visited[ch]:
continue
cost1=min(cost1,cost[ch-1])
stck.append(ch)
visited[ch]=True
return cost1
n,m=map(int,input().split())
cost=[int(x) for x in input().split()]
visited=[False]*(n+1)
for i in range(m):
u,v=map(int,input().split())
addE(u,v)
ans=0
for i in range(1,n+1):
if not visited[i]:
ans+=dfs(i)
for i in range(1,n+1):
if not visited[i]:
ans+=cost[i-1]
print(ans)
``` | output | 1 | 40,917 | 2 | 81,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.
Submitted Solution:
```
def dfs(graph, start):
visited, stack = set(), [start]
while stack:
vertex = stack.pop()
if vertex not in visited:
visited.add(vertex)
stack.extend(graph[vertex] - visited)
return visited, min([prices[v] for v in visited])
n, m = map(int, input().split()); ans = 0
prices = [None] + list(map(int, input().split()))
friendship = {x: set() for x in range(1, n+1)}
for _ in range(m):
x, y = map(int, input().split())
friendship[x].add(y)
friendship[y].add(x)
citizens = set(range(1, n+1))
while citizens:
v = citizens.pop()
visited, p = dfs(friendship, v)
citizens.difference_update(visited)
ans += p
print(ans)
``` | instruction | 0 | 40,918 | 2 | 81,836 |
Yes | output | 1 | 40,918 | 2 | 81,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.
In the second example Vova has to bribe everyone.
In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.
Submitted Solution:
```
n, m = map(int, input().split())
arr = list(map(int, input().split()))
# graph = {i: set() for i in range(1, n+1)}
graph = [[] for i in range(n+1)]
for i in range(m):
x, y = map(int, input().split())
graph[x].append(y)
graph[y].append(x)
# if(x>y):
# graph[y] = graph[y].union(set([x]))
# else:
# graph[x] = graph[x].union(set([y]))
# graph[min(x,y)] = graph[min(x,y)].union(set([max(x,y)]))
# graph[max(x,y)] = graph[max(x,y)].union(set([min(x,y)]))
# print(graph)
globalVisited = [0 for _ in range(n+1)]
temp = 10000000000
def dfs(node):
global temp, globalVisited
stack = [node]
temp = arr[node-1]
while stack:
vertex = stack.pop()
if globalVisited[vertex] == 0:
globalVisited[vertex] = 1
temp = min(temp, arr[vertex-1])
for neighbors in graph[vertex]:
if globalVisited[neighbors] == 0:
stack.append(neighbors)
return temp
ans = 0
for i in range(1, n+1):
if globalVisited[i] == 0:
ans += dfs(i)
print(ans)
``` | instruction | 0 | 40,919 | 2 | 81,838 |
Yes | output | 1 | 40,919 | 2 | 81,839 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.