message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.
Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages.
Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.
The numbers in the lines are separated by single spaces.
Output
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
Examples
Input
5 5
1 2
2 2 3
2 3 4
2 4 5
1 5
Output
0
Input
8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1
Output
2
Input
2 2
1 2
0
Output
1
Note
In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.
In the third sample employee 2 must learn language 2. | instruction | 0 | 33,667 | 14 | 67,334 |
Tags: dfs and similar, dsu
Correct Solution:
```
n,m=map(int,input().split())
languages=[ [list(map(int,input().split()))[1:]]+[False] for i in range(n) ]
c=0
w=0
for i in range(len(languages)):
g=languages[i-w][0]
if not g:
c+=1
languages.pop(i-w)
w+=1
def connected(languages,lang):
lang[1]=True
for x in range(len(languages)):
i=languages[x]
if i[1]:
continue
p=False
for y in i[0]:
if y in lang[0]:
p=True;break
# print(f"In function:{i}")
if not i[1] and p:
languages[x][1]=True
lang[0].extend(i[0])
lang[0]=list(dict.fromkeys(lang[0]))
connected(languages,lang)
# print(languages)
# connected(languages,languages[0])
# print(languages)
if languages:
c-=1
for x in languages:
if not x[1]:
c+=1
# print(languages)
connected(languages,x)
print(c)
else:
print(c)
# print(languages)
``` | output | 1 | 33,667 | 14 | 67,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.
Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages.
Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.
The numbers in the lines are separated by single spaces.
Output
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
Examples
Input
5 5
1 2
2 2 3
2 3 4
2 4 5
1 5
Output
0
Input
8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1
Output
2
Input
2 2
1 2
0
Output
1
Note
In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.
In the third sample employee 2 must learn language 2. | instruction | 0 | 33,668 | 14 | 67,336 |
Tags: dfs and similar, dsu
Correct Solution:
```
n, m = map(int, input().split())
languages = []
for i in range(n):
languages.append(list(map(int, input().split()))[1:])
languages.insert(0, [])
# print(languages)
neighbors = [[] for i in range( 1 + n)]
# Function to check if employees speak same language
def SpeakSame(e1, e2):
for one_language in languages[e1]:
for another_language in languages[e2]:
if one_language == another_language:
return True
return False
# For each employee
for i in range(1, 1 + n):
for j in range( 1 + i, 1 + n):
if SpeakSame(i, j):
neighbors[i].append(j)
neighbors[j].append(i)
# 2 edges and 3 vertices
# V is the number of vertices
# E is the number of edges
# Adjacency list
# initialize
# E = int(input())
# V = 1 + E
# neighbors = [[] for i in range(1 + V)]
# for i in range(E):
# n, m = map(int, input().split())
# neighbors[n].append(m)
# neighbors[m].append(n)
result = [[], [2, 3], [1], [1]]
# assert neighbors==result
# print(neighbors)
class Queue:
def __init__(self):
self.list = []
def push(self, value):
self.list.append(value)
def pop(self):
return self.list.pop(0)
def top(self):
return self.list[0]
def empty(self):
return len(self.list) == 0
visited = [False for i in range(1+n)]
independentCycles = 0
def BFS(starting):
connectedComponent = []
global independentCycles
BFSq = Queue()
BFSq.push(starting)
visited[starting] = True
while not BFSq.empty():
m = BFSq.pop() # we rely on the fact that pop() also return the popped element
connectedComponent.append(m)
for neighbor in neighbors[m]:
if not visited[neighbor]:
# the m -> neighbor edged is the subtree
visited[neighbor] = True
BFSq.push(neighbor)
else:
# the m -> neighbor edged is closing an independent loop
independentCycles += 1
return connectedComponent
count_isolated =0
ConnectedSets = []
for idx, visit in enumerate(visited):
if visit == False:
connectedComponent = BFS(idx)
if len(connectedComponent)==1:
count_isolated +=1
ConnectedSets.append(connectedComponent)
doNotSpeak = 0
for language in languages:
if len(language)==0:
doNotSpeak +=1
# print(doNotSpeak)
if doNotSpeak == len(languages):
print(len(ConnectedSets)-1)
else:
print(len(ConnectedSets)-2)
``` | output | 1 | 33,668 | 14 | 67,337 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.
Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages.
Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.
The numbers in the lines are separated by single spaces.
Output
Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).
Examples
Input
5 5
1 2
2 2 3
2 3 4
2 4 5
1 5
Output
0
Input
8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1
Output
2
Input
2 2
1 2
0
Output
1
Note
In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.
In the third sample employee 2 must learn language 2. | instruction | 0 | 33,669 | 14 | 67,338 |
Tags: dfs and similar, dsu
Correct Solution:
```
n,m = map(int,input().split())
lang = [set(list(map(int,input().split()))[1:]) for _ in range(n)]
g = {}
count = 0
for i,e in enumerate(lang):
if len(e)!=0:
g[i] = []
for j,o in enumerate(lang):
if i!=j and len(o)!=0:
if len(e&o)!=0:
g[i].append(j)
else:
count+=1
if len(g)!=0:
ma = []
for i in range(n):
if i in g:
stack = [i]
visited = [i]
while len(stack)!=0:
r = stack.pop()
for j in g[r]:
if j not in visited:
visited.append(j)
stack.append(j)
visited = sorted(visited)
if visited not in ma:
ma.append(visited)
count += len(ma)-1
print(count)
``` | output | 1 | 33,669 | 14 | 67,339 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old.
They will have dinner around some round tables. You want to distribute foxes such that:
1. Each fox is sitting at some table.
2. Each table has at least 3 foxes sitting around it.
3. The sum of ages of any two adjacent foxes around each table should be a prime number.
If k foxes f1, f2, ..., fk are sitting around table in clockwise order, then for 1 ≤ i ≤ k - 1: fi and fi + 1 are adjacent, and f1 and fk are also adjacent.
If it is possible to distribute the foxes in the desired manner, find out a way to do that.
Input
The first line contains single integer n (3 ≤ n ≤ 200): the number of foxes in this party.
The second line contains n integers ai (2 ≤ ai ≤ 104).
Output
If it is impossible to do this, output "Impossible".
Otherwise, in the first line output an integer m (<image>): the number of tables.
Then output m lines, each line should start with an integer k -=– the number of foxes around that table, and then k numbers — indices of fox sitting around that table in clockwise order.
If there are several possible arrangements, output any of them.
Examples
Input
4
3 4 8 9
Output
1
4 1 2 4 3
Input
5
2 2 2 2 2
Output
Impossible
Input
12
2 3 4 5 6 7 8 9 10 11 12 13
Output
1
12 1 2 3 6 5 12 9 8 7 10 11 4
Input
24
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Output
3
6 1 2 3 6 5 4
10 7 8 9 12 15 14 13 16 11 10
8 17 18 23 22 19 20 21 24
Note
In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.
In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number. | instruction | 0 | 33,781 | 14 | 67,562 |
Tags: flows
Correct Solution:
```
#E
def main():
sieve = [False, True] * 10001
for i in range(3, 140, 2):
if sieve[i]:
j, k = i * 2, i * i
le = (20001 - k) // j + 1
sieve[k::j] = [False] * le
n = int(input())
aa = list(map(int, input().split()))
pp = [-1] * n
def dget(v):
if dsu[v]!=v:
dsu[v] = dget(dsu[v])
return dsu[v]
def dfs(v):
if free[v]:
free[v], a, pv = False, aa[v], pp[v]
for i, p in enumerate(pp):
if sieve[a + aa[i]] and pv != i and (p == -1 or dfs(p)):
pp[i] = v
return True
return False
for i in range(n):
free = [True] * n
if not dfs(i):
print('Impossible')
return
dsu = list(range(n))
for i, p in enumerate(pp):
i, p = dget(i), dget(p)
dsu[p] = i
print(sum(dget(i)==i for i in range(n)))
for i in range(n):
if dget(i) == i:
row = [sum(dget(j) == i for j in range(n)), i + 1]
j = pp[i]
while j != i:
row.append(j + 1)
j = pp[j]
print(*row)
main()
``` | output | 1 | 33,781 | 14 | 67,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old.
They will have dinner around some round tables. You want to distribute foxes such that:
1. Each fox is sitting at some table.
2. Each table has at least 3 foxes sitting around it.
3. The sum of ages of any two adjacent foxes around each table should be a prime number.
If k foxes f1, f2, ..., fk are sitting around table in clockwise order, then for 1 ≤ i ≤ k - 1: fi and fi + 1 are adjacent, and f1 and fk are also adjacent.
If it is possible to distribute the foxes in the desired manner, find out a way to do that.
Input
The first line contains single integer n (3 ≤ n ≤ 200): the number of foxes in this party.
The second line contains n integers ai (2 ≤ ai ≤ 104).
Output
If it is impossible to do this, output "Impossible".
Otherwise, in the first line output an integer m (<image>): the number of tables.
Then output m lines, each line should start with an integer k -=– the number of foxes around that table, and then k numbers — indices of fox sitting around that table in clockwise order.
If there are several possible arrangements, output any of them.
Examples
Input
4
3 4 8 9
Output
1
4 1 2 4 3
Input
5
2 2 2 2 2
Output
Impossible
Input
12
2 3 4 5 6 7 8 9 10 11 12 13
Output
1
12 1 2 3 6 5 12 9 8 7 10 11 4
Input
24
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Output
3
6 1 2 3 6 5 4
10 7 8 9 12 15 14 13 16 11 10
8 17 18 23 22 19 20 21 24
Note
In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.
In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number. | instruction | 0 | 33,782 | 14 | 67,564 |
Tags: flows
Correct Solution:
```
from collections import deque
class Dinic():
def __init__(self, listEdge, s, t):
self.s = s
self.t = t
self.graph = {}
self.maxCap = 1000000
# dict các node lân cận
# e[0]: from, e[1]: to, e[2]: dung luong
for e in listEdge:
if e[0] not in self.graph:
self.graph[e[0]] = []
if e[1] not in self.graph:
self.graph[e[1]] = []
#to #cap #reveser edge
self.graph[e[0]].append([e[1], e[2], len(self.graph[e[1]])])
self.graph[e[1]].append([e[0], 0, len(self.graph[e[0]])-1])
self.N = len(self.graph.keys())
def bfs(self):
self.dist = {}
self.dist[self.s] = 0
self.curIter = {node:[] for node in self.graph}
Q = deque([self.s])
while(len(Q) > 0):
cur = Q.popleft()
for index,e in enumerate(self.graph[cur]):
# Chỉ add vào các node kế tiếp nếu dung lượng cạnh > 0 và chưa được visit trước đấy
if e[1] > 0 and e[0] not in self.dist:
self.dist[e[0]] = self.dist[cur] + 1
# add vào danh sách node kế tiếp của node hiện tại
self.curIter[cur].append(index)
Q.append(e[0])
def findPath(self, cur, f):
if cur == self.t:
return f
while len(self.curIter[cur]) > 0:
indexEdge = self.curIter[cur][-1]
nextNode = self.graph[cur][indexEdge][0]
remainCap = self.graph[cur][indexEdge][1]
indexPreEdge = self.graph[cur][indexEdge][2]
if remainCap > 0 and self.dist[nextNode] > self.dist[cur]:
#self.next[cur] = indexEdge
flow = self.findPath(nextNode, min(f, remainCap))
if flow > 0:
self.path.append(cur)
self.graph[cur][indexEdge][1] -= flow
self.graph[nextNode][indexPreEdge][1] += flow
#if cur == self.s:
# print(self.path, flow)
return flow
#else:
#self.path.pop()
self.curIter[cur].pop()
return 0
def maxFlow(self):
maxflow = 0
flow = []
while(True):
self.bfs()
if self.t not in self.dist:
break
while(True):
self.path = []
f = self.findPath(self.s, self.maxCap)
#print('iter', self.curIter)
if f == 0:
break
flow.append(f)
maxflow += f
return maxflow
# Tìm tập node thuộc S và T
# sau khi đã tìm được max flow
def residualBfs(self):
Q = deque([self.s])
side = {self.s:'s'}
while(len(Q) > 0):
cur = Q.popleft()
for index,e in enumerate(self.graph[cur]):
if e[1] > 0 and e[0] not in side:
Q.append(e[0])
side[e[0]] = 's'
S = []
T = []
for x in self.graph:
if x in side:
S.append(x)
else:
T.append(x)
return set(S), set(T)
def push(G, u, v):
if u not in G:
G[u]=[]
if v not in G:
G[v]=[]
G[u].append(v)
G[v].append(u)
def bfs(u, used, G):
l, r = G[u][0], G[u][1]
used[u]=1
used[l]=1
used[r]=1
S=[l,u,r]
i=2
while i < len(S):
cur=S[i]
if cur in G:
for v in G[cur]:
if used[v]==0:
used[v]=1
S.append(v)
i+=1
return S
max_ = 20001
prime = [1] * max_
for i in range(2, max_):
if prime[i] == 1:
for j in range(2*i, max_, i):
prime[j] = 0
n = int(input())
a = list(map(int, input().split()))
diff = 0
for x in a:
if x%2==0:
diff+=1
else:
diff-=1
flg=True
if diff != 0:
flg=False
else:
edge = []
for i in range(n):
if a[i]%2==1:
edge.append([i,'t', 2])
continue
edge.append(['s', i, 2])
for j in range(n):
if a[j]%2==1 and prime[a[i]+a[j]] == 1:
edge.append([i, j, 1])
g = Dinic(edge, 's', 't')
F = g.maxFlow()
if F != n:
flg=False
else:
G = {}
for u, es in g.graph.items():
if u in ['s', 't'] or a[u] % 2 == 1:
continue
for e in es:
v, remain = e[0], e[1]
if isinstance(v, int) == True and remain == 0:
push(G, u, v)
used = [0]*n
comp = []
for i in range(n):
if a[i]%2==0 and used[i]==0:
comp.append(bfs(i, used, G))
if flg==False:
print('Impossible')
else:
print(len(comp))
for nodes in comp:
for i in range(len(nodes)):
nodes[i] += 1
arr = [len(nodes)] + nodes
print(' '.join([str(x) for x in arr]))
``` | output | 1 | 33,782 | 14 | 67,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old.
They will have dinner around some round tables. You want to distribute foxes such that:
1. Each fox is sitting at some table.
2. Each table has at least 3 foxes sitting around it.
3. The sum of ages of any two adjacent foxes around each table should be a prime number.
If k foxes f1, f2, ..., fk are sitting around table in clockwise order, then for 1 ≤ i ≤ k - 1: fi and fi + 1 are adjacent, and f1 and fk are also adjacent.
If it is possible to distribute the foxes in the desired manner, find out a way to do that.
Input
The first line contains single integer n (3 ≤ n ≤ 200): the number of foxes in this party.
The second line contains n integers ai (2 ≤ ai ≤ 104).
Output
If it is impossible to do this, output "Impossible".
Otherwise, in the first line output an integer m (<image>): the number of tables.
Then output m lines, each line should start with an integer k -=– the number of foxes around that table, and then k numbers — indices of fox sitting around that table in clockwise order.
If there are several possible arrangements, output any of them.
Examples
Input
4
3 4 8 9
Output
1
4 1 2 4 3
Input
5
2 2 2 2 2
Output
Impossible
Input
12
2 3 4 5 6 7 8 9 10 11 12 13
Output
1
12 1 2 3 6 5 12 9 8 7 10 11 4
Input
24
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Output
3
6 1 2 3 6 5 4
10 7 8 9 12 15 14 13 16 11 10
8 17 18 23 22 19 20 21 24
Note
In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.
In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number. | instruction | 0 | 33,783 | 14 | 67,566 |
Tags: flows
Correct Solution:
```
def main():
sieve = [False, True] * 10001
for i in range(3, 140, 2):
if sieve[i]:
j, k = i * 2, i * i
le = (20001 - k) // j + 1
sieve[k::j] = [False] * le
n = int(input())
aa = list(map(int, input().split()))
pp = [-1] * n
def dsu_get(v):
if dsu[v] != v:
dsu[v] = dsu_get(dsu[v])
return dsu[v]
def dfs(v):
if free[v]:
free[v], a, pv = False, aa[v], pp[v]
for i, p in enumerate(pp):
if sieve[a + aa[i]] and pv != i and (p == -1 or dfs(p)):
pp[i] = v
return True
return False
for i in range(n):
free = [True] * n
if not dfs(i):
print('Impossible')
return
dsu = list(range(n))
for i, p in enumerate(pp):
i, p = dsu_get(i), dsu_get(p)
dsu[p] = i
print(sum(dsu_get(i) == i for i in range(n)))
for i in range(n):
if dsu_get(i) == i:
row = [sum(dsu_get(j) == i for j in range(n)), i + 1]
j = pp[i]
while j != i:
row.append(j + 1)
j = pp[j]
print(*row)
if __name__ == '__main__':
main()
``` | output | 1 | 33,783 | 14 | 67,567 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old.
They will have dinner around some round tables. You want to distribute foxes such that:
1. Each fox is sitting at some table.
2. Each table has at least 3 foxes sitting around it.
3. The sum of ages of any two adjacent foxes around each table should be a prime number.
If k foxes f1, f2, ..., fk are sitting around table in clockwise order, then for 1 ≤ i ≤ k - 1: fi and fi + 1 are adjacent, and f1 and fk are also adjacent.
If it is possible to distribute the foxes in the desired manner, find out a way to do that.
Input
The first line contains single integer n (3 ≤ n ≤ 200): the number of foxes in this party.
The second line contains n integers ai (2 ≤ ai ≤ 104).
Output
If it is impossible to do this, output "Impossible".
Otherwise, in the first line output an integer m (<image>): the number of tables.
Then output m lines, each line should start with an integer k -=– the number of foxes around that table, and then k numbers — indices of fox sitting around that table in clockwise order.
If there are several possible arrangements, output any of them.
Examples
Input
4
3 4 8 9
Output
1
4 1 2 4 3
Input
5
2 2 2 2 2
Output
Impossible
Input
12
2 3 4 5 6 7 8 9 10 11 12 13
Output
1
12 1 2 3 6 5 12 9 8 7 10 11 4
Input
24
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Output
3
6 1 2 3 6 5 4
10 7 8 9 12 15 14 13 16 11 10
8 17 18 23 22 19 20 21 24
Note
In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.
In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number.
Submitted Solution:
```
from collections import deque
class Dinic():
def __init__(self, listEdge, s, t):
self.s = s
self.t = t
self.graph = {}
self.maxCap = 1000000
# dict các node lân cận
# e[0]: from, e[1]: to, e[2]: dung luong
for e in listEdge:
if e[0] not in self.graph:
self.graph[e[0]] = []
if e[1] not in self.graph:
self.graph[e[1]] = []
#to #cap #reveser edge
self.graph[e[0]].append([e[1], e[2], len(self.graph[e[1]])])
self.graph[e[1]].append([e[0], 0, len(self.graph[e[0]])-1])
self.N = len(self.graph.keys())
def bfs(self):
self.dist = {}
self.dist[self.s] = 0
self.curIter = {node:[] for node in self.graph}
Q = deque([self.s])
while(len(Q) > 0):
cur = Q.popleft()
for index,e in enumerate(self.graph[cur]):
# Chỉ add vào các node kế tiếp nếu dung lượng cạnh > 0 và chưa được visit trước đấy
if e[1] > 0 and e[0] not in self.dist:
self.dist[e[0]] = self.dist[cur] + 1
# add vào danh sách node kế tiếp của node hiện tại
self.curIter[cur].append(index)
Q.append(e[0])
def findPath(self, cur, f):
if cur == self.t:
return f
while len(self.curIter[cur]) > 0:
indexEdge = self.curIter[cur][-1]
nextNode = self.graph[cur][indexEdge][0]
remainCap = self.graph[cur][indexEdge][1]
indexPreEdge = self.graph[cur][indexEdge][2]
if remainCap > 0 and self.dist[nextNode] > self.dist[cur]:
#self.next[cur] = indexEdge
flow = self.findPath(nextNode, min(f, remainCap))
if flow > 0:
self.path.append(cur)
self.graph[cur][indexEdge][1] -= flow
self.graph[nextNode][indexPreEdge][1] += flow
#if cur == self.s:
# print(self.path, flow)
return flow
#else:
#self.path.pop()
self.curIter[cur].pop()
return 0
def maxFlow(self):
maxflow = 0
flow = []
while(True):
self.bfs()
if self.t not in self.dist:
break
while(True):
self.path = []
f = self.findPath(self.s, self.maxCap)
#print('iter', self.curIter)
if f == 0:
break
flow.append(f)
maxflow += f
return maxflow
# Tìm tập node thuộc S và T
# sau khi đã tìm được max flow
def residualBfs(self):
Q = deque([self.s])
side = {self.s:'s'}
while(len(Q) > 0):
cur = Q.popleft()
for index,e in enumerate(self.graph[cur]):
if e[1] > 0 and e[0] not in side:
Q.append(e[0])
side[e[0]] = 's'
S = []
T = []
for x in self.graph:
if x in side:
S.append(x)
else:
T.append(x)
return set(S), set(T)
def push(G, u, v):
if u not in G:
G[u]=[]
if v not in G:
G[v]=[]
G[u].append(v)
G[v].append(u)
def bfs(u, used, G):
l, r = G[u][0], G[u][1]
used[u]=1
used[l]=1
used[r]=1
S=[l,u,r]
i=2
while i < len(S):
cur=S[i]
if cur in G:
for v in G[cur]:
if used[v]==0:
used[v]=1
S.append(v)
i+=1
return S
max_ = 20001
prime = [1] * max_
for i in range(2, max_):
if prime[i] == 1:
for j in range(2*i, max_, i):
prime[j] = 0
n = int(input())
a = list(map(int, input().split()))
diff = 0
for x in a:
if x%2==0:
diff+=1
else:
diff-=1
flg=True
if diff != 0:
flg=False
else:
edge = []
for i in range(n):
if a[i]%2==1:
edge.append([i,'t', 2])
continue
edge.append(['s', i, 2])
for j in range(n):
if a[j]%2==1 and prime[a[i]+a[j]] == 1:
edge.append([i, j, 1])
g = Dinic(edge, 's', 't')
F = g.maxFlow()
if F != n:
flg=False
else:
G = {}
for u, es in g.graph.items():
if u in ['s', 't'] or a[u] % 2 == 1:
continue
for e in es:
v, remain = e[0], e[1]
if isinstance(v, int) == True and remain == 0:
push(G, u, v)
used = [0]*n
comp = []
for i in range(0, n, 2):
if a[i]%2==0 and used[i]==0:
comp.append(bfs(i, used, G))
if flg==False:
print('Impossible')
else:
print(len(comp))
for nodes in comp:
for i in range(len(nodes)):
nodes[i] += 1
arr = [len(nodes)] + nodes
print(' '.join([str(x) for x in arr]))
``` | instruction | 0 | 33,784 | 14 | 67,568 |
No | output | 1 | 33,784 | 14 | 67,569 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old.
They will have dinner around some round tables. You want to distribute foxes such that:
1. Each fox is sitting at some table.
2. Each table has at least 3 foxes sitting around it.
3. The sum of ages of any two adjacent foxes around each table should be a prime number.
If k foxes f1, f2, ..., fk are sitting around table in clockwise order, then for 1 ≤ i ≤ k - 1: fi and fi + 1 are adjacent, and f1 and fk are also adjacent.
If it is possible to distribute the foxes in the desired manner, find out a way to do that.
Input
The first line contains single integer n (3 ≤ n ≤ 200): the number of foxes in this party.
The second line contains n integers ai (2 ≤ ai ≤ 104).
Output
If it is impossible to do this, output "Impossible".
Otherwise, in the first line output an integer m (<image>): the number of tables.
Then output m lines, each line should start with an integer k -=– the number of foxes around that table, and then k numbers — indices of fox sitting around that table in clockwise order.
If there are several possible arrangements, output any of them.
Examples
Input
4
3 4 8 9
Output
1
4 1 2 4 3
Input
5
2 2 2 2 2
Output
Impossible
Input
12
2 3 4 5 6 7 8 9 10 11 12 13
Output
1
12 1 2 3 6 5 12 9 8 7 10 11 4
Input
24
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Output
3
6 1 2 3 6 5 4
10 7 8 9 12 15 14 13 16 11 10
8 17 18 23 22 19 20 21 24
Note
In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.
In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number.
Submitted Solution:
```
from collections import deque
class Dinic():
def __init__(self, listEdge, s, t):
self.s = s
self.t = t
self.graph = {}
self.maxCap = 1000000
# dict các node lân cận
# e[0]: from, e[1]: to, e[2]: dung luong
for e in listEdge:
if e[0] not in self.graph:
self.graph[e[0]] = []
if e[1] not in self.graph:
self.graph[e[1]] = []
#to #cap #reveser edge
self.graph[e[0]].append([e[1], e[2], len(self.graph[e[1]])])
self.graph[e[1]].append([e[0], 0, len(self.graph[e[0]])-1])
self.N = len(self.graph.keys())
def bfs(self):
self.dist = {}
self.dist[self.s] = 0
self.curIter = {node:[] for node in self.graph}
Q = deque([self.s])
while(len(Q) > 0):
cur = Q.popleft()
for index,e in enumerate(self.graph[cur]):
# Chỉ add vào các node kế tiếp nếu dung lượng cạnh > 0 và chưa được visit trước đấy
if e[1] > 0 and e[0] not in self.dist:
self.dist[e[0]] = self.dist[cur] + 1
# add vào danh sách node kế tiếp của node hiện tại
self.curIter[cur].append(index)
Q.append(e[0])
def findPath(self, cur, f):
if cur == self.t:
return f
while len(self.curIter[cur]) > 0:
indexEdge = self.curIter[cur][-1]
nextNode = self.graph[cur][indexEdge][0]
remainCap = self.graph[cur][indexEdge][1]
indexPreEdge = self.graph[cur][indexEdge][2]
if remainCap > 0 and self.dist[nextNode] > self.dist[cur]:
#self.next[cur] = indexEdge
flow = self.findPath(nextNode, min(f, remainCap))
if flow > 0:
self.path.append(cur)
self.graph[cur][indexEdge][1] -= flow
self.graph[nextNode][indexPreEdge][1] += flow
#if cur == self.s:
# print(self.path, flow)
return flow
#else:
#self.path.pop()
self.curIter[cur].pop()
return 0
def maxFlow(self):
maxflow = 0
flow = []
while(True):
self.bfs()
if self.t not in self.dist:
break
while(True):
self.path = []
f = self.findPath(self.s, self.maxCap)
#print('iter', self.curIter)
if f == 0:
break
flow.append(f)
maxflow += f
return maxflow
# Tìm tập node thuộc S và T
# sau khi đã tìm được max flow
def residualBfs(self):
Q = deque([self.s])
side = {self.s:'s'}
while(len(Q) > 0):
cur = Q.popleft()
for index,e in enumerate(self.graph[cur]):
if e[1] > 0 and e[0] not in side:
Q.append(e[0])
side[e[0]] = 's'
S = []
T = []
for x in self.graph:
if x in side:
S.append(x)
else:
T.append(x)
return set(S), set(T)
def push(G, u, v):
if u not in G:
G[u]=[]
if v not in G:
G[v]=[]
G[u].append(v)
G[v].append(u)
def bfs(u, used, G):
used[u]=1
S=[u]
i=0
while i < len(S):
cur=S[i]
if cur in G:
for v in G[cur]:
if used[v]==0:
used[v]=1
S.append(v)
i+=1
return S
max_ = 20001
prime = [1] * max_
for i in range(2, max_):
if prime[i] == 1:
for j in range(2*i, max_, i):
prime[j] = 0
n = int(input())
a = list(map(int, input().split()))
diff = 0
for x in a:
if x%2==0:
diff+=1
else:
diff-=1
flg=True
if diff != 0:
flg=False
else:
edge = []
for i in range(n):
if a[i]%2==1:
edge.append([i,'t', 2])
continue
edge.append(['s', i, 2])
for j in range(n):
if a[j]%2==1 and prime[a[i]+a[j]] == 1:
edge.append([i, j, 1])
g = Dinic(edge, 's', 't')
F = g.maxFlow()
if F != n:
flg=False
else:
G = {}
for u, es in g.graph.items():
if u in ['s', 't'] or a[u] % 2 == 1:
continue
for e in es:
v, remain = e[0], e[1]
if isinstance(v, int) == True and remain == 0:
push(G, u, v)
used = [0]*n
comp = []
for i in range(0, n, 2):
if a[i]%2==0 and used[i]==0:
comp.append(bfs(i, used, G))
if flg==False:
print('Impossible')
else:
print(len(comp))
for nodes in comp:
for i in range(len(nodes)):
nodes[i] += 1
arr = [len(nodes)] + nodes
print(' '.join([str(x) for x in arr]))
``` | instruction | 0 | 33,785 | 14 | 67,570 |
No | output | 1 | 33,785 | 14 | 67,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
Input
The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.
Output
If it is impossible to drink n cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them.
Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO | instruction | 0 | 33,891 | 14 | 67,782 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from math import ceil
n, k, g, b = map(int, input().split())
if (min(g, b) + 1) * k < max(g, b):
print("NO")
exit()
if g > b:
m = ceil(g / k)
left = b
for i in range(m - 1):
print("G" * k, end="")
if left - k >= (m - i - 1):
print("B" * k, end="")
left -= k
else:
print("B", end="")
left -= 1
print("G" * (g - k * (m - 1)), end="")
print("B" * left)
else:
m = ceil(b / k)
left = g
for i in range(m - 1):
print("B" * k, end="")
if left - k >= (m - i - 1):
print("G" * k, end="")
left -= k
else:
print("G", end="")
left -= 1
print("B" * (b - k * (m - 1)), end="")
print("G" * left)
``` | output | 1 | 33,891 | 14 | 67,783 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
Input
The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.
Output
If it is impossible to drink n cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them.
Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO | instruction | 0 | 33,892 | 14 | 67,784 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n,k,a,b = map(int,input().split())
s = 'GB'*min(a,b)
a-=len(s)//2
b-=len(s)//2
if(a!=0):
s+='G'
a-=1
elif b!=0:
s='B'+s
b-=1
cur = 0
m = 1
while(cur!= len(s)):
if(m!=k):
if(s[cur]=='G' and a!=0):
s = s[:cur+1]+'G'+s[cur+1:]
a-=1
m+=1
elif(s[cur]=='B' and b!=0):
s = s[:cur+1]+'B'+s[cur+1:]
b-=1
m+=1
else:
m = 1
cur+=1
if(a==0 and b ==0):
print(s)
else:
print('NO')
``` | output | 1 | 33,892 | 14 | 67,785 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
Input
The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.
Output
If it is impossible to drink n cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them.
Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO | instruction | 0 | 33,893 | 14 | 67,786 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
#PY
n,k,a,b = map(int,input().split())
m1=max(a,b)
m2=min(a,b)
if m1 - (k * m2) > k:
print("NO")
elif a == b:
while a != 0:
print("G",end='')
print("B",end='')
a-=1
elif a > b:
while b!=0 or a!=0:
for i in range(k):
if a==0:
break
print("G",end='')
a-=1
while a==b and a!=0:
print("B",end='')
print("G",end='')
a-=1
b-=1
if b!=0:
print("B",end='')
b-=1
while a==b and a!=0:
print("G",end='')
print("B",end='')
a-=1
b-=1
elif a < b:
while b!=0 or a!=0:
for i in range(k):
if b==0:
break
print("B",end='')
b-=1
while a==b and a!=0:
print("G",end='')
print("B",end='')
a-=1
b-=1
if a!=0:
print("G",end='')
a-=1
while a==b and a!=0:
print("B",end='')
print("G",end='')
a-=1
b-=1
``` | output | 1 | 33,893 | 14 | 67,787 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
Input
The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.
Output
If it is impossible to drink n cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them.
Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO | instruction | 0 | 33,894 | 14 | 67,788 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n, k, a, b = [int(x) for x in input().split()]
if (int(n / (k+1)) > min(a, b)):
print("NO")
else:
actual = (a >= b)
s = ""
letter = ""
m = 0
while(n > 0):
v = int(n / (k+1))
if (v < min(a, b)):
if actual:
m = min(k, a - v)
letter = "G"
a -= m
else:
m = min(k, b - v)
letter = "B"
b -= m
elif (v == min(a, b)):
if (a == b):
m = a
if actual:
letter = "GB"
else:
letter = "BG"
else:
if (actual):
m = max(min(k, a - v), 1)
letter = "G"
a -= m
else:
m = max(min(k, b - v), 1)
letter = "B"
b -= m
actual = not actual
s += letter * m
n -= m * len(letter)
print(s)
``` | output | 1 | 33,894 | 14 | 67,789 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
Input
The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.
Output
If it is impossible to drink n cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them.
Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO | instruction | 0 | 33,895 | 14 | 67,790 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n, k, a, b = [int(x) for x in input().split()]
s = ""
if (int(n / (k+1)) > min(a, b)):
s = "NO"
else:
actual = (a >= b)
m = 0
while(n > 0):
v = int(n / (k+1))
if (a != b):
if (actual):
m = max(min(k, a - v), 1)
s += "G" * m
a -= m
else:
m = max(min(k, b - v), 1)
s += "B" * m
b -= m
n -= m
else:
n = 0
if actual:
s += "GB" * a
else:
s += "BG" * a
actual = not actual
print(s)
``` | output | 1 | 33,895 | 14 | 67,791 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
Input
The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.
Output
If it is impossible to drink n cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them.
Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO | instruction | 0 | 33,896 | 14 | 67,792 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n, k, a, b = [int(v) for v in input().split()]
# n, k, a, b = 5,1,3,2
# n, k, a, b = 7,2,2,5
# n, k, a, b = 4,3,4,0
G = 'G'
B = 'B'
if a > b:
a, b = b, a
G, B = B, G
ans = []
while b - a > 1 and a >= 1:
take_b = min(k, b - a)
ans.append(B * take_b)
ans.append(G)
b -= take_b
a -= 1
assert(a <= b)
ans.append((B + G) * a)
b -= a
a = 0
if b > k:
print('NO')
else:
ans.append(B * b)
s = ''.join(ans)
assert(len(s) == n)
print(s)
# print(a, b)
``` | output | 1 | 33,896 | 14 | 67,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
Input
The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.
Output
If it is impossible to drink n cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them.
Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO | instruction | 0 | 33,897 | 14 | 67,794 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from collections import *
d = defaultdict()
n,k,a,b = map(int,input().split())
ch = 1
if(a == b):
ch = 2
mx,mn = max(a,b),min(a,b)
if(a>b):
d[mx] = 'G'
d[mn] = 'B'
else:
d[mx] = 'B'
d[mn] = 'G'
o = -((-mx)//(mn+1))
if(o>k):
ch = 0
s = ""
t = mn
y = mx
o = mx//(mn+1)
y = mx - (mn+1)*o
a = []
if(ch == 2):
s = ""
for i in range(n//2):
s += 'GB'
print(s)
elif(ch == 1):
for i in range((mn+1)):
a.append(d[mx]*(o))
for i in range(y):
a[i] += d[mx]
s = d[mn].join(a)
print(s)
else:
print('NO')
``` | output | 1 | 33,897 | 14 | 67,795 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
Input
The first line contains four integers n, k, a and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that a + b = n.
Output
If it is impossible to drink n cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.
If there are multiple answers, print any of them.
Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO | instruction | 0 | 33,898 | 14 | 67,796 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n,k,a,b=map(int,input().split())
acol='G'
bcol='B'
if a>b:
a,b=b,a
acol,bcol=bcol,acol
if b>(a+1)*k:
print("NO")
else:
s=""
num=0
for i in range(n):
if b>a and num<k:
print(bcol,end="")
b-=1
num+=1
else:
print(acol,end="")
a-=1
num=0
``` | output | 1 | 33,898 | 14 | 67,797 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order.
For each action several separate windows are singled out: k1 separate windows for the first action (the first type windows), k2 windows for the second one (the second type windows), and k3 for the third one (the third type windows). The service time for one person in any of the first type window equals to t1. Similarly, it takes t2 time to serve a person in any of the second type windows. And it takes t3 to serve one person in any of the third type windows. Thus, the service time depends only on the window type and is independent from the person who is applying for visa.
At some moment n people come to the embassy, the i-th person comes at the moment of time ci. The person is registered under some number. After that he sits in the hall and waits for his number to be shown on a special board. Besides the person's number the board shows the number of the window where one should go and the person goes there immediately. Let's consider that the time needed to approach the window is negligible. The table can show information for no more than one person at a time. The electronic queue works so as to immediately start working with the person who has approached the window, as there are no other people in front of the window.
The Client Service Quality inspectors noticed that several people spend too much time in the embassy (this is particularly tiresome as the embassy has no mobile phone reception and 3G). It was decided to organise the system so that the largest time a person spends in the embassy were minimum. Help the inspectors organise the queue. Consider that all actions except for being served in at the window, happen instantly.
Input
The first line contains three space-separated integers k1, k2, k3 (1 ≤ ki ≤ 109), they are the number of windows of the first, second and third type correspondingly.
The second line contains three space-separated integers t1, t2, t3 (1 ≤ ti ≤ 105), they are the periods of time needed to serve one person in the window of the first, second and third type correspondingly.
The third line contains an integer n (1 ≤ n ≤ 105), it is the number of people.
The fourth line contains n space-separated integers ci (1 ≤ ci ≤ 109) in the non-decreasing order; ci is the time when the person number i comes to the embassy.
Output
Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).
Examples
Input
1 1 1
1 1 1
5
1 1 1 1 1
Output
7
Input
2 1 1
5 1 1
5
1 2 3 3 5
Output
13
Note
In the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last person who comes to the first window waits (4 units of time).
Windows in the second test work like this:
The first window of the first type: [1, 6) — the first person, [6, 11) — third person, [11, 16) — fifth person
The second window of the first type: [2, 7) — the second person, [7, 12) — the fourth person
The only second type window: [6, 7) — first, [7, 8) — second, [11, 12) — third, [12, 13) — fourth, [16, 17) — fifth
The only third type window: [7, 8) — first, [8, 9) — second, [12, 13) — third, [13, 14) — fourth, [17, 18) — fifth
We can see that it takes most time to serve the fifth person. | instruction | 0 | 33,950 | 14 | 67,900 |
Tags: data structures, greedy
Correct Solution:
```
from heapq import heappush,heappop
k=list(map(int,input().split()))
t=list(map(int,input().split()))
ans=0
n=int(input())
l=sorted(list(map(int,input().split())))
X=list(l)
for i in range(3):
for j in range(n):
if j>=k[i] and X[j-k[i]]>X[j]:
X[j]+=X[j-k[i]]-X[j]
X[j]+=t[i]
for i in range(n):
ans=max(ans,X[i]-l[i])
print(ans)
``` | output | 1 | 33,950 | 14 | 67,901 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order.
For each action several separate windows are singled out: k1 separate windows for the first action (the first type windows), k2 windows for the second one (the second type windows), and k3 for the third one (the third type windows). The service time for one person in any of the first type window equals to t1. Similarly, it takes t2 time to serve a person in any of the second type windows. And it takes t3 to serve one person in any of the third type windows. Thus, the service time depends only on the window type and is independent from the person who is applying for visa.
At some moment n people come to the embassy, the i-th person comes at the moment of time ci. The person is registered under some number. After that he sits in the hall and waits for his number to be shown on a special board. Besides the person's number the board shows the number of the window where one should go and the person goes there immediately. Let's consider that the time needed to approach the window is negligible. The table can show information for no more than one person at a time. The electronic queue works so as to immediately start working with the person who has approached the window, as there are no other people in front of the window.
The Client Service Quality inspectors noticed that several people spend too much time in the embassy (this is particularly tiresome as the embassy has no mobile phone reception and 3G). It was decided to organise the system so that the largest time a person spends in the embassy were minimum. Help the inspectors organise the queue. Consider that all actions except for being served in at the window, happen instantly.
Input
The first line contains three space-separated integers k1, k2, k3 (1 ≤ ki ≤ 109), they are the number of windows of the first, second and third type correspondingly.
The second line contains three space-separated integers t1, t2, t3 (1 ≤ ti ≤ 105), they are the periods of time needed to serve one person in the window of the first, second and third type correspondingly.
The third line contains an integer n (1 ≤ n ≤ 105), it is the number of people.
The fourth line contains n space-separated integers ci (1 ≤ ci ≤ 109) in the non-decreasing order; ci is the time when the person number i comes to the embassy.
Output
Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).
Examples
Input
1 1 1
1 1 1
5
1 1 1 1 1
Output
7
Input
2 1 1
5 1 1
5
1 2 3 3 5
Output
13
Note
In the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last person who comes to the first window waits (4 units of time).
Windows in the second test work like this:
The first window of the first type: [1, 6) — the first person, [6, 11) — third person, [11, 16) — fifth person
The second window of the first type: [2, 7) — the second person, [7, 12) — the fourth person
The only second type window: [6, 7) — first, [7, 8) — second, [11, 12) — third, [12, 13) — fourth, [16, 17) — fifth
The only third type window: [7, 8) — first, [8, 9) — second, [12, 13) — third, [13, 14) — fourth, [17, 18) — fifth
We can see that it takes most time to serve the fifth person. | instruction | 0 | 33,951 | 14 | 67,902 |
Tags: data structures, greedy
Correct Solution:
```
q = zip(map(int, input().split()), map(int, input().split()))
n = int(input())
c = list(map(int, input().split()))
d = c[:]
for k, t in q:
for i in range(min(k, n)): c[i] += t
for i in range(k, n): c[i] = max(c[i], c[i - k]) + t
print(max(c[i] - d[i] for i in range(n)))
``` | output | 1 | 33,951 | 14 | 67,903 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO | instruction | 0 | 34,335 | 14 | 68,670 |
Tags: brute force, math, number theory
Correct Solution:
```
ans = int(input())
flag = 0
if ans > 1000000000000:
print("NO")
else:
if ans > 3:
x = 1
y = (ans - 3) // 2
if (x ** 2 + 2 * x * y + x + 1) == ans:
print(str(x)+' '+str(y))
flag = 1
if flag == 0:
print("NO")
``` | output | 1 | 34,335 | 14 | 68,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO | instruction | 0 | 34,336 | 14 | 68,672 |
Tags: brute force, math, number theory
Correct Solution:
```
r=int(input())
if r&1==0 or r<5:
print ("NO")
else:
print(1,(r-3)//2)
``` | output | 1 | 34,336 | 14 | 68,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO | instruction | 0 | 34,337 | 14 | 68,674 |
Tags: brute force, math, number theory
Correct Solution:
```
from math import *
r = int(input())
r -= 1
e = False
xx = 0
yy = 0
for i in range(1, int(sqrt(r)) + 1):
i1 = i
r1 = r
if r1 % i1 == 0:
r1 = r1/i1
if ((r1 - i1 - 1) > 0) and ((r1 - i1 - 1) % 2 == 0):
xx = i1
yy = (r1 - i1 - 1) // 2
e = True
break
if not e:
print("NO")
else:
print(xx, int(yy))
``` | output | 1 | 34,337 | 14 | 68,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO | instruction | 0 | 34,338 | 14 | 68,676 |
Tags: brute force, math, number theory
Correct Solution:
```
n = int(input())
if(n % 2 == 1):
if(n <= 3):
print("NO")
else:
print("1 " + str((n - 3) // 2))
else:
print("NO")
``` | output | 1 | 34,338 | 14 | 68,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO | instruction | 0 | 34,339 | 14 | 68,678 |
Tags: brute force, math, number theory
Correct Solution:
```
r=int(input())
y=0
x=1
cnt=0
while True:
y=(r-1-x-x*x)/(2*x)
s=int(y)
if y<=0:
break
if y==s:
cnt=1
break
else:
x+=1
if cnt==1:
print(x,s)
else :
print("NO")
``` | output | 1 | 34,339 | 14 | 68,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO | instruction | 0 | 34,340 | 14 | 68,680 |
Tags: brute force, math, number theory
Correct Solution:
```
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return factors
def factors(n):
super=[1]
for i in prime_factors(n):
super.extend([j*i for j in super])
return list(sorted(set(super)))
n = int(input())
n -= 1
OK = False;
for i in factors(n):
if((n // i - i) > 0 and (n // i - i) % 2 == 1 and i > 0 and (n // i - i)// 2 > 0):
print("{} {}".format(i, (n // i - i)// 2))
OK = True
break
if(not OK):
print("NO")
``` | output | 1 | 34,340 | 14 | 68,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO | instruction | 0 | 34,341 | 14 | 68,682 |
Tags: brute force, math, number theory
Correct Solution:
```
from math import sqrt
def get_int():
from sys import stdin
return int(stdin.readline().replace('\n', ''))
def is_even(n):
return n%2 == 0
def heidi_hash(r):
k = r-1
rt = int(sqrt(k))
for x in range(1, rt+2):
if k % x == 0:
temp = k/x -x -1
if temp > 0 and is_even(temp):
return x,int(temp/2)
return 'NO'
r = get_int()
res = heidi_hash(r)
print(res[0],res[1]) if type(res) != type('') else print(res)
``` | output | 1 | 34,341 | 14 | 68,683 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO
Submitted Solution:
```
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from itertools import (
accumulate, combinations, combinations_with_replacement, groupby,
permutations, product)
from math import (acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot,
log2, pi, radians, sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits
def inp():
return(int(input()))
def inlist():
return(list(map(int, input().split())))
def instr():
s = input()
return(list(s[:len(s)]))
def invr():
return(map(Decimal, input().split()))
r = inp()
x = 1
while x * (x+1) <= r:
if (r - x*(x+1) - 1) % (2*x) == 0:
y = (r - x*(x+1) - 1) // (2*x)
if x <= y:
print(x, y)
sys.exit()
x += 1
print("NO")
``` | instruction | 0 | 34,342 | 14 | 68,684 |
Yes | output | 1 | 34,342 | 14 | 68,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO
Submitted Solution:
```
import math
n = int(input())
x = math.floor(math.sqrt(n))
p = 0
for i in range(1, x):
if (n-1-i**2-i)%(2*i) == 0 :
print(i, (n-1-i**2-i)//(2*i))
p = 1
break
if not p:
print("NO")
``` | instruction | 0 | 34,343 | 14 | 68,686 |
Yes | output | 1 | 34,343 | 14 | 68,687 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO
Submitted Solution:
```
from math import sqrt
n=int(input())
if n%2==0:
print("NO")
exit()
mi=10000000000
n-=1
b=0
for i in range(1,int(sqrt(n))):
if n%i==0:
y=(n-i-i**2)/(2*i)
if y==int(y):
if i<mi:
mi=i
b=int(y)
if mi==10000000000:
print("NO")
exit()
print(mi,b)
``` | instruction | 0 | 34,344 | 14 | 68,688 |
Yes | output | 1 | 34,344 | 14 | 68,689 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO
Submitted Solution:
```
n=int(input())
if(n%2==0):
print("NO")
elif(n==1 or n==3):
print('NO')
else:
print(1,end=" ")
print((n-3)//2)
``` | instruction | 0 | 34,345 | 14 | 68,690 |
Yes | output | 1 | 34,345 | 14 | 68,691 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO
Submitted Solution:
```
r=int(input())-1
ans=0
for i in range(1,(r//2)+1):
if r%i==0:
x=min(i,r//i)
y=max(i,r//i)-x-1
if y%2==0:
ans=1
y=y//2
print(x,end=" ")
print(y)
break
if ans==0:
print("NO")
``` | instruction | 0 | 34,346 | 14 | 68,692 |
No | output | 1 | 34,346 | 14 | 68,693 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO
Submitted Solution:
```
import math
def funcao(r):
if r % 2 == 0 or r == 1:
return [-1, -1]
limite = (math.sqrt(4 * r - 3) + 1) // 2
x = 1
while x < limite:
y = (r - (x ** 2 + x + 1)) // 2 # Se x for um número par o y não da um inteiro
return [x, y]
x += 1
return [-1,-1]
r = int(input())
res = funcao(r)
if res[0] == -1:
print("NO")
else:
print(*res)
``` | instruction | 0 | 34,347 | 14 | 68,694 |
No | output | 1 | 34,347 | 14 | 68,695 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO
Submitted Solution:
```
r = int(input())
print((1, (r - 3) // 2) if r & 1 else "NO")
``` | instruction | 0 | 34,348 | 14 | 68,696 |
No | output | 1 | 34,348 | 14 | 68,697 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.
Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.
The first hashing function she designed is as follows.
Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.
Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?
If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO".
Input
The first and only line contains an integer r (1 ≤ r ≤ 10^{12}).
Output
Output integers x, y such that H(x,y) = r and x is smallest possible, or "NO" if no such pair exists.
Examples
Input
19
Output
1 8
Input
16
Output
NO
Submitted Solution:
```
#rOkY
#FuCk
################################ kOpAl ############################################
def ans(t):
print(1,(t%10)-1)
t=int(input())
if(t==1):
print(1,1)
elif(t%2==0):
print("NO")
else:
ans(t)
``` | instruction | 0 | 34,349 | 14 | 68,698 |
No | output | 1 | 34,349 | 14 | 68,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home. | instruction | 0 | 34,716 | 14 | 69,432 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
count = 0
i = 0
while i < n and a[i] == 0:
i += 1
while i < n:
if a[i] == 1:
i += 1
count += 1
else:
tmp = 0
while i < n and a[i] == 0:
tmp += 1
i += 1
if i != n and tmp < 2:
count += tmp
print(count)
``` | output | 1 | 34,716 | 14 | 69,433 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home. | instruction | 0 | 34,717 | 14 | 69,434 |
Tags: implementation
Correct Solution:
```
import re
input()
print(len("".join(re.split('00+', input().replace(' ', '').strip('0')))))
``` | output | 1 | 34,717 | 14 | 69,435 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home. | instruction | 0 | 34,718 | 14 | 69,436 |
Tags: implementation
Correct Solution:
```
import sys
def solve(v, n):
start = n
for i in range(n):
if(v[i] == 1):
start = i
break
end = -1
for i in range(n-1, -1, -1):
if(v[i] == 1):
end = i+1
break
resp = end-start
# print(start, end, resp)
if(resp > 1):
zeros = 0
maximum = 0
for i in range(start, end):
if(v[i] == 0):
zeros += 1
else:
if(zeros >= 2):
resp -= zeros
zeros = 0
return max(resp, 0)
n = int(input())
line = input()
v = [int(x) for x in line.split()]
print(solve(v, n))
``` | output | 1 | 34,718 | 14 | 69,437 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home. | instruction | 0 | 34,719 | 14 | 69,438 |
Tags: implementation
Correct Solution:
```
import sys
n = int(input())
v = list(map(int, input().split()))
for i in range(2, n):
if v[i] + v[i - 2] == 2: v[i - 1] = 1
print(sum(v))
``` | output | 1 | 34,719 | 14 | 69,439 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home. | instruction | 0 | 34,720 | 14 | 69,440 |
Tags: implementation
Correct Solution:
```
# ===================================
# (c) MidAndFeed aka ASilentVoice
# ===================================
# import math, fractions, collections
# ===================================
n = int(input())
q = [int(x) for x in input().split()]
go = 0
ans = 0
for i in range(n):
if not(go):
if q[i] == 1:
go = 1
else:
continue
t = q[i]
if t == 0:
if (i+1 < n and t == q[i+1]) or (i == n-1):
go = 0
continue
ans += 1
print(ans)
``` | output | 1 | 34,720 | 14 | 69,441 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home. | instruction | 0 | 34,721 | 14 | 69,442 |
Tags: implementation
Correct Solution:
```
def main():
n = int(input())
a = list(map(int, input().split()))
cnt = 0
ans = 0
i = 0
while i < n:
if a[i] == 1:
break
i += 1
while i < n:
cnt = 0
if a[i] == 0:
while i < n and a[i] == 0:
cnt += 1
i += 1
i -= 1
else:
ans += 1
if cnt < 2 and i != n - 1:
ans += cnt
i += 1
print(ans)
main()
``` | output | 1 | 34,721 | 14 | 69,443 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home. | instruction | 0 | 34,722 | 14 | 69,444 |
Tags: implementation
Correct Solution:
```
#!/usr/bin/python3.5
n=int(input())
s=[int(x) for x in input().split()]
p,o,=0,0
for i in range(n):
if s[i]:
p+=1
o=1
elif not(o):
continue
elif i<n-1:
if s[i+1] and s[i-1]:
p+=1
print(p)
``` | output | 1 | 34,722 | 14 | 69,445 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.
One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).
The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).
The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the n pairs she knows if there will be a class at that time or not.
Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.
Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.
Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.
The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn't have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, ..., an are separated by spaces.
Output
Print a single number — the number of pairs during which Alena stays at the university.
Examples
Input
5
0 1 0 1 1
Output
4
Input
7
1 0 1 0 0 1 0
Output
4
Input
1
0
Output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.
In the last sample Alena doesn't have a single pair, so she spends all the time at home. | instruction | 0 | 34,723 | 14 | 69,446 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
time = 0
try:
start = a.index(1)
except:
print(0)
exit()
current = a[start]
zero_in_row = 0
f = lambda x: 1 if x == 1 else 0
for i in range(start, n):
if a[i] == 1:
time += f(zero_in_row)
zero_in_row = 0
time += 1
else:
zero_in_row += 1
print(time)
``` | output | 1 | 34,723 | 14 | 69,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left. | instruction | 0 | 34,748 | 14 | 69,496 |
Tags: greedy, math
Correct Solution:
```
al, ar = map(int, input().split())
bl, br = map(int, input().split())
if al-1<=br<=2*al+2 or ar-1<=bl<=2*ar+2:
print("YES")
else:
print("NO")
``` | output | 1 | 34,748 | 14 | 69,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left. | instruction | 0 | 34,749 | 14 | 69,498 |
Tags: greedy, math
Correct Solution:
```
girl = list(map(int, input().split()))
boy = list(map(int, input().split()))
def compatible(b, g):
return (g - 1) <= b <= (g + 1) * 2
if compatible(boy[0], girl[1]) or compatible(boy[1], girl[0]):
print('YES')
else:
print('NO')
``` | output | 1 | 34,749 | 14 | 69,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left. | instruction | 0 | 34,750 | 14 | 69,500 |
Tags: greedy, math
Correct Solution:
```
f=lambda:map(int,input().split())
dl,dr=f()
ml,mr=f()
def chk(d,m):
return d-2<m and m<2*d+3
if chk(dl,mr) or chk(dr,ml):
print("YES")
else:
print("NO")
``` | output | 1 | 34,750 | 14 | 69,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left. | instruction | 0 | 34,751 | 14 | 69,502 |
Tags: greedy, math
Correct Solution:
```
def fun(x,y):
if y>=x-1:
s=1
else:
s=0
if y%2==0:
t=(y/2)-1
else:
t=int(y/2)
if x>=t:
w=1
else:
w=0
if w*s==1:
return 1
else:
return 0
a,b=map(int,input().split())
c,d=map(int,input().split())
if fun(a,d) or fun(b,c):
print('YES')
else:
print('NO')
``` | output | 1 | 34,751 | 14 | 69,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left. | instruction | 0 | 34,752 | 14 | 69,504 |
Tags: greedy, math
Correct Solution:
```
import sys
import math
import collections
import heapq
import decimal
input=sys.stdin.readline
a,b=(int(i) for i in input().split())
c,d=(int(i) for i in input().split())
def check(i,j):
if((j>=i-1 and j<=(i+1)*2) or (i>=(j-1)//2 and i<=j+1)):
return True
else:
return False
if(check(a,d) or check(b,c)):
print("YES")
else:
print("NO")
``` | output | 1 | 34,752 | 14 | 69,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left. | instruction | 0 | 34,753 | 14 | 69,506 |
Tags: greedy, math
Correct Solution:
```
r=lambda g,b: g-1<=b<=2*(g+1)
a,b=map(int,input().split())
c,d=map(int,input().split())
if r(a,d) or r(b,c):print("YES")
else:print("NO")
``` | output | 1 | 34,753 | 14 | 69,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left. | instruction | 0 | 34,754 | 14 | 69,508 |
Tags: greedy, math
Correct Solution:
```
l_g, r_g = [int(x) for x in input().split()]
l_b, r_b = [int(x) for x in input().split()]
def is_okay(g, b):
if g >= (b + 2):
return False
if b >= (((g + 1) * 2) + 1):
return False
return True
if any([is_okay(l_g, r_b), is_okay(r_g, l_b)]):
print('YES')
else: print('NO')
``` | output | 1 | 34,754 | 14 | 69,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left. | instruction | 0 | 34,755 | 14 | 69,510 |
Tags: greedy, math
Correct Solution:
```
gl, gr = map(int, input().split())
bl, br = map(int, input().split())
if (br >= gl-1 and br <= (gl+1)*2) or (bl >= gr-1 and bl <= (gr+1)*2):
print('YES')
else:
print('NO')
``` | output | 1 | 34,755 | 14 | 69,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has al fingers on her left hand and ar fingers on the right one. The boy correspondingly has bl and br fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa.
Input
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Output
Print YES or NO, that is, the answer to Petr Palych's question.
Examples
Input
5 1
10 5
Output
YES
Input
4 5
3 3
Output
YES
Input
1 2
11 6
Output
NO
Note
The boy and the girl don't really care who goes to the left.
Submitted Solution:
```
gl, gr = [int(i) for i in input().split()]
bl, br = [int(i) for i in input().split()]
if ((gl * 2 + 2) >= br and (abs(gl - br) <= 1)) or ((gr * 2 + 2) >= bl and (abs(gr - bl) <= 1)):
print("YES")
else:
print("NO")
``` | instruction | 0 | 34,761 | 14 | 69,522 |
No | output | 1 | 34,761 | 14 | 69,523 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.