text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide a correct Python 3 solution for this coding contest problem.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
"Correct Solution:
```
# Undirected Graph
class Edge:
__slots__ = ('v', 'w')
def __init__(self, v, w):
self.v = v
self.w = w
def either(self):
return self.v
def other(self, v):
if v == self.v:
return self.w
else:
return self.v
class Graph:
def __init__(self, v):
self.v = v
self._edges = [[] for _ in range(v)]
def add(self, e):
self._edges[e.v].append(e)
self._edges[e.w].append(e)
def adj(self, v):
return self._edges[v]
def edges(self):
for es in self._edges:
for e in es:
yield e
def bridges(graph):
def visit(v, e):
nonlocal n
w = e.other(v)
if not visited[w]:
parent[w] = v
n += 1
visited[w] = n
low[w] = n
return True
elif w != parent[v]:
low[v] = min(low[v], visited[w])
return False
def leave(p, e):
c = e.other(p)
if p == parent[c] and low[c] > visited[p]:
es.append(e)
low[p] = min(low[p], low[c])
return False
visited = [0] * graph.v
low = [0] * graph.v
parent = [-1] * graph.v
es = []
s = 0
n = 1
visited[s] = n
low[s] = n
stack = [(s, e, visit) for e in graph.adj(s)]
while stack:
v, e, func = stack.pop()
# print(v, e.v, e.w, func.__name__, visited, low)
if func(v, e):
stack.append((v, e, leave))
w = e.other(v)
for ne in graph.adj(w):
stack.append((w, ne, visit))
return es
def run():
v, e = [int(i) for i in input().split()]
g = Graph(v)
for _ in range(e):
s, t = [int(i) for i in input().split()]
g.add(Edge(s, t))
edges = [(e.v, e.w) if e.v < e.w else (e.w, e.v)
for e in bridges(g)]
for v, w in sorted(edges):
print(v, w)
if __name__ == '__main__':
run()
```
| 92,600 |
Provide a correct Python 3 solution for this coding contest problem.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
5 4
0 1
1 2
2 3
3 4
output:
0 1
1 2
2 3
3 4
"""
import sys
sys.setrecursionlimit(int(1e5))
def generate_adj_table(_v_info):
for v_detail in _v_info:
v_from, v_to = map(int, v_detail)
init_adj_table[v_from].append(v_to)
# undirected graph
init_adj_table[v_to].append(v_from)
return init_adj_table
def graph_dfs(u, visited, parent, low, disc):
global Time
# Count of children in current node
children = 0
# Mark the current node as visited and print it
visited[u] = True
# Initialize discovery time and low value
disc[u] = Time
low[u] = Time
Time += 1
# Recur for all the vertices adjacent to this vertex
for v in adj_table[u]:
# If v is not visited yet, then make it a child of u
# in DFS tree and recur for it
if not visited[v]:
parent[v] = u
children += 1
graph_dfs(v, visited, parent, low, disc)
# Check if the subtree rooted with v has a connection to
# one of the ancestors of u
low[u] = min(low[u], low[v])
''' If the lowest vertex reachable from subtree
under v is below u in DFS tree, then u-v is
a bridge'''
if low[v] > disc[u]:
ans.append(sorted([u, v]))
elif v != parent[u]: # Update low value of u for parent function calls.
low[u] = min(low[u], disc[v])
return None
def bridge():
visited = [False] * vertices
disc = [float("Inf")] * vertices
low = [float("Inf")] * vertices
parent = [-1] * vertices
for v in range(vertices):
if not visited[v]:
graph_dfs(v, visited, parent, low, disc)
return ans
if __name__ == '__main__':
_input = sys.stdin.readlines()
vertices, edges = map(int, _input[0].split())
v_info = map(lambda x: x.split(), _input[1:])
Time = 0
ans = []
init_adj_table = tuple([] for _ in range(vertices))
adj_table = generate_adj_table(v_info)
res = bridge()
res.sort()
for ele in res:
print(*ele)
```
| 92,601 |
Provide a correct Python 3 solution for this coding contest problem.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
"Correct Solution:
```
#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys,collections
sys.setrecursionlimit(100000)
INF = float("inf")
V,E = map(int,sys.stdin.readline().split())
st = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(E)) # multi line with multi param
G = [[] for _ in range(V)]
for s,t in st:
G[s].append(t)
G[t].append(s) #not directed
visited = set()
prenum = [0]*V
parent = [0]*V
lowest = [0]*V
timer = 0
def dfs(current,prev):
global timer
parent[current]=prev
prenum[current]=lowest[current]=timer
timer += 1
visited.add(current)
for nex in G[current]:
if not nex in visited:
dfs(nex,current)
lowest[current]=min(lowest[current],lowest[nex])
elif nex != prev:
lowest[current]=min(lowest[current],prenum[nex])
dfs(0,-1)
ret = []
for i in range(1,V):
p = parent[i]
if prenum[p] < lowest[i]:
if p < i:
ret.append([p,i])
else:
ret.append([i,p])
ret = sorted(list(ret))
for s,t in ret:
print(s,t)
```
| 92,602 |
Provide a correct Python 3 solution for this coding contest problem.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
"Correct Solution:
```
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
# return: articulation points, bridges
# The graph must be connected.
def lowlink(adj, root=1):
N = len(adj) - 1
order = [N + 1] * (N + 1)
low = [N + 1] * (N + 1)
AP = []
bridge = []
st = [root]
cnt = 1
par = [0] * (N + 1)
seq = []
while st:
v = st.pop()
if order[v] != N+1:
continue
order[v] = cnt
seq.append(v)
low[v] = cnt
cnt += 1
for u in adj[v]:
if order[u] < cnt:
if par[v] != u:
low[v] = min(low[v], order[u])
continue
else:
par[u] = v
st.append(u)
child = [[] for _ in range(N + 1)]
for v in range(1, N + 1):
child[par[v]].append(v)
seq.reverse()
for v in seq:
for u in child[v]:
low[v] = min(low[v], low[u])
# bridge
for p in range(1, N+1):
for c in child[p]:
if order[p] < low[c]:
bridge.append((p, c))
# articulation point
for v in range(1, N + 1):
if v == root:
if len(child[v]) > 1:
AP.append(v)
else:
for c in child[v]:
if order[v] <= low[c]:
AP.append(v)
break
return AP, bridge
N, M = map(int, input().split())
adj = [[] for _ in range(N+1)]
for _ in range(M):
a, b = map(int, input().split())
a += 1
b += 1
adj[a].append(b)
adj[b].append(a)
AP, bridge = lowlink(adj)
ans = []
for u, v in bridge:
u -= 1
v -= 1
if u > v:
u, v = v, u
ans.append((u, v))
ans.sort(key=lambda x: x[1])
ans.sort(key=lambda x: x[0])
for u, v in ans:
print(u, v)
if __name__ == '__main__':
main()
```
| 92,603 |
Provide a correct Python 3 solution for this coding contest problem.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
"Correct Solution:
```
from math import inf
import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
def dfs(v,par):
global cur
vis[v]=1 ; disc[v]=cur ;low[v]=cur
cur+=1
for i in g[v]:
if vis[i]==0:
dfs(i,v)
low[v]=min(low[i],low[v])
if low[i]>disc[v]:
apts.add((min(v,i),max(v,i)))
elif i!=par: low[v]=min(disc[i],low[v])
def articulation_pts():
global apts,disc,low,vis
disc=[0]*n ; low=[inf]*n ; vis=[0]*n ; apts=set()
for i in range(n):
if vis[i]==0:
dfs(i,-1)
n,e=map(int,input().split())
cur=0
g=[[] for i in range(n)]
for i in range(e):
x,y=map(int,input().split())
g[x].append(y)
g[y].append(x)
articulation_pts()
apts=sorted(apts)
for i in apts:
print(*i)
```
| 92,604 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 8)
def lowLink(N: int, Adj: list):
articulation = []
bridge = []
order = [None] * N
lowest = [1 << 100] * N
def _dfs(cur, pre, k):
order[cur] = lowest[cur] = k
is_articulation = False
cnt = 0
for nxt in Adj[cur]:
if order[nxt] is None:
cnt += 1
_dfs(nxt, cur, k + 1)
if lowest[cur] > lowest[nxt]:
lowest[cur] = lowest[nxt]
is_articulation |= pre >= 0 and lowest[nxt] >= order[cur]
if order[cur] < lowest[nxt]:
if cur < nxt:
bridge.append((cur, nxt))
else:
bridge.append((nxt, cur))
elif nxt != pre and lowest[cur] > order[nxt]:
lowest[cur] = order[nxt]
is_articulation |= pre < 0 and cnt > 1
if is_articulation:
articulation.append(cur)
_dfs(0, -1, 0)
return articulation, bridge
def main():
n, m, *L = map(int, open(0).read().split())
adj = [[] for _ in range(n)]
for s, t in zip(*[iter(L)] * 2):
adj[s] += t,
adj[t] += s,
_, bridge = lowLink(n, adj)
for x, y in sorted(bridge):
print(x, y)
if __name__ == '__main__':
main()
```
Yes
| 92,605 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
Submitted Solution:
```
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**7)
class LowLinks:
def __init__(self, edges, edges_num:int):
"""edges[u]: all vertexes connected with vertex 'u'
edges_num: number of edges of graph
the root of DFS-tree is vertex 0
"""
self.edges = edges
self.V = len(edges)
self.order = [-1]*V
self.low = [float('inf')]*V
self.bridges = []
# if degreee(root) > 1 and graph is tree: root is articulation
self.articulations = []
if len(edges[0]) > 1 and edges_num == self.V-1:
self.articulations.append(0)
self.k = 0
def build(self):
self.dfs(0, 0)
def get_bridges(self)->tuple:
return self.bridges
def get_articulations(self)->tuple:
return self.articulations
def dfs(self, v:int, prev:int):
self.order[v] = self.k
self.low[v] = self.k
self.k += 1
is_articulation = False
for to in self.edges[v]:
if self.order[to] < 0: # not visited
self.dfs(to, v)
self.low[v] = min(self.low[v], self.low[to])
if self.order[v] < self.low[to]:
self.bridges.append((v, to) if v < to else (to, v))
is_articulation |= self.order[v] <= self.low[to]
elif to != prev: # back edge
self.low[v] = min(self.low[v], self.order[to])
if v>0 and is_articulation:
self.articulations.append(v)
if __name__ == "__main__":
V,E = map(int, readline().split())
edges = [[] for _ in range(V)]
for _ in range(E):
s,t = map(int, readline().split())
edges[s].append(t)
edges[t].append(s)
lowlinks = LowLinks(edges, E)
lowlinks.build()
bridges = lowlinks.get_bridges()
bridges.sort()
if bridges:
for s,t in bridges:
print(s, t)
```
Yes
| 92,606 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
Submitted Solution:
```
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
INF=float('inf')
N,M=MAP()
nodes=[[] for i in range(N)]
for i in range(M):
u,v=MAP()
nodes[u].append(v)
nodes[v].append(u)
visited=[False]*N
timer=1
prenum=[0]*(N+1)
lowest=[0]*(N+1)
ans=[]
def rec(cur, prev):
global timer
# curを訪問した直後の処理
prenum[cur]=lowest[cur]=timer
timer+=1
visited[cur]=True
for nxt in nodes[cur]:
# 未訪問なら再帰探索する
if not visited[nxt]:
rec(nxt, cur)
# nxtの探索が終了した直後の処理
lowest[cur]=min(lowest[cur], lowest[nxt])
# より近い経路を含まないなら橋とする
if lowest[nxt]==prenum[nxt]:
# 番号の小さい方から入れる
ans.append((min(cur, nxt), max(cur, nxt)))
# 訪問済の場合、親への経路は無視して、他は近い経路となるか確認を取る
elif nxt!=prev:
lowest[cur]=min(lowest[cur], lowest[nxt])
rec(0, -1)
ans.sort()
for edge in ans:
print(*edge)
```
Yes
| 92,607 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
Submitted Solution:
```
# 橋検出
from collections import defaultdict
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()
# 二つの配列ord,miniを持つ。
V, E = map(int, input().split())
edge = []
graph = [[] for i in range(V)]
for i in range(E):
x, y = map(int, input().split())
graph[x].append(y)
graph[y].append(x)
edge.append((min(x, y), max(x, y)))
order = [i for i in range(V)]
mini = [V+1 for i in range(V)]
visited = [False for i in range(V)]
used = defaultdict(bool)
for node in range(V):
for point in graph[node]:
used[(node, point)] = False
cnt = 0
def dfs(node):
global cnt
visited[node] = True
cnt += 1
order[node] = cnt
mini[node] = cnt
for point in graph[node]:
if visited[point] == False:
used[(node, point)] = True
dfs(point)
mini[node] = min(mini[node], mini[point])
elif not used[(point, node)]:
mini[node] = min(mini[node], order[point])
return
dfs(0)
answer = []
for x, y in edge:
if order[x] < order[y]:
if order[x] < mini[y]:
answer.append((x, y))
elif order[x] > order[y]:
if order[y] < mini[x]:
answer.append((x, y))
answer.sort()
for x, y in answer:
print(x, y)
```
Yes
| 92,608 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
Submitted Solution:
```
import sys
f_i = sys.stdin
V, E = map(int, f_i.readline().split())
adj = [[] for i in range(V)]
for l_i in f_i:
s, t = map(int, l_i.split())
adj[s].append(t)
adj[t].append(s)
# dfs traverse
prenum = [1] + [None] * (V - 1)
parent = [0] * V
lowest = [1] + [V] * (V - 1)
import collections
path = collections.deque()
path.append(0)
cnt = 1
while path:
u = path[-1]
adj_v = adj[u]
for v in adj_v:
if not prenum[v]:
parent[v] = u
path.append(v)
cnt += 1
prenum[v] = cnt
lowest[v] = cnt
adj[u].remove(v)
adj[v].remove(u)
break
if u == path[-1]:
lowest[u] = min(lowest[u], prenum[u])
for v in adj_v:
l = min(lowest[u], lowest[v])
lowest[u] = l
lowest[v] = l
p = parent[u]
lowest[p] = min(lowest[u], lowest[p])
path.pop()
# output
bridge = []
for u in range(1, V):
p = parent[u]
if lowest[p] != lowest[u]:
b = [p, u]
b.sort()
bridge.append(b)
bridge.sort()
for b in bridge:
print(*b)
```
No
| 92,609 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
Submitted Solution:
```
from sys import stdin
readline = stdin.readline
from sys import stdin
readline = stdin.readline
def main():
v, e = map(int, readline().split())
from collections import defaultdict
g = defaultdict(list)
for _ in range(e):
s, t = map(int, readline().split())
g[s].append(t)
g[t].append(s)
visited = set()
lowest = [None] * v
parent = [None] * v
prenum = [None] * v
child = defaultdict(list)
root = 0
# dfs??§tree?????????
stack = [(root, None)]
while stack:
u, prev = stack.pop()
if u not in visited:
parent[u] = prev
if prev is not None:
child[prev].append(u)
visited |= {u}
prenum[u] = lowest[u] = len(visited)
stack.extend([(v, u) for v in g[u] if v not in visited])
# lowest????¨????
from collections import Counter
leaf = [i for i in range(v) if not child[i]]
unfinished = Counter()
for li in leaf:
while li is not None:
candidate = [prenum[li]] + [prenum[i] for i in g[li] if i != parent[li]] + [lowest[i] for i in child[li]]
lowest[li] = min(candidate)
li = parent[li]
if li is not None and 1 < len(child[li]):
unfinished[li] += 1
if unfinished[li] < len(child[li]):
break
# ????????????
bridge = []
for i in range(v):
if child[i]:
for j in child[i]:
if prenum[i] < lowest[j]:
if i > j:
i, j = j, i
bridge.append((i, j))
bridge.sort()
for bi in bridge:
print(*bi)
main()
```
No
| 92,610 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
Submitted Solution:
```
# Acceptance of input
import sys
f_i = sys.stdin
V, E = map(int, f_i.readline().split())
adj = [[] for i in range(V)]
for l_i in f_i:
s, t = map(int, l_i.split())
adj[s].append(t)
adj[t].append(s)
# dfs traverse
prenum = [1] + [None] * (V - 1)
parent = [0] * V
lowest = [1] + [V] * (V - 1)
import collections
path = collections.deque()
path.append(0)
cnt = 1
while path:
u = path[-1]
adj_v = adj[u]
for v in adj_v:
if not prenum[v]:
parent[v] = u
path.append(v)
cnt += 1
prenum[v] = cnt
adj[u].remove(v)
adj[v].remove(u)
break
if u == path[-1]:
lowest[u] = min(lowest[u], prenum[u])
for v in adj_v:
l = min(lowest[u], lowest[v])
lowest[u] = l
lowest[v] = l
p = parent[u]
lowest[p] = min(lowest[u], lowest[p])
path.pop()
# output
bridge = []
for u in range(1, V):
p = parent[u]
if lowest[p] != lowest[u]:
b = [p, u]
b.sort()
bridge.append(b)
bridge.sort()
for b in bridge:
print(*b)
```
No
| 92,611 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Find bridges of an undirected graph G(V, E).
A bridge (also known as a cut-edge) is an edge whose deletion increase the number of connected components.
Constraints
* 1 ≤ |V| ≤ 100,000
* 0 ≤ |E| ≤ 100,000
* The graph is connected
* There are no parallel edges
* There are no self-loops
Input
|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
, where |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target nodes of i-th edge (undirected).
Output
A list of bridges of the graph ordered by name. For each bridge, names of its end-ponints, source and target (source < target), should be printed separated by a space. The sources should be printed ascending order, then the target should also be printed ascending order for the same source.
Examples
Input
4 4
0 1
0 2
1 2
2 3
Output
2 3
Input
5 4
0 1
1 2
2 3
3 4
Output
0 1
1 2
2 3
3 4
Submitted Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
5 4
0 1
1 2
2 3
3 4
output:
0 1
1 2
2 3
3 4
"""
import sys
sys.setrecursionlimit(int(1e10))
def generate_adj_table(_v_info):
for v_detail in _v_info:
v_from, v_to = map(int, v_detail)
init_adj_table[v_from].append(v_to)
# undirected graph
init_adj_table[v_to].append(v_from)
return init_adj_table
def graph_dfs(u, visited, parent, low, disc):
global Time
# Count of children in current node
children = 0
# Mark the current node as visited and print it
visited[u] = True
# Initialize discovery time and low value
disc[u] = Time
low[u] = Time
Time += 1
# Recur for all the vertices adjacent to this vertex
for v in adj_table[u]:
# If v is not visited yet, then make it a child of u
# in DFS tree and recur for it
if not visited[v]:
parent[v] = u
children += 1
graph_dfs(v, visited, parent, low, disc)
# Check if the subtree rooted with v has a connection to
# one of the ancestors of u
low[u] = min(low[u], low[v])
''' If the lowest vertex reachable from subtree
under v is below u in DFS tree, then u-v is
a bridge'''
if low[v] > disc[u]:
ans.append(sorted([u, v]))
elif v != parent[u]: # Update low value of u for parent function calls.
low[u] = min(low[u], disc[v])
return None
def bridge():
visited = [False] * vertices
disc = [float("Inf")] * vertices
low = [float("Inf")] * vertices
parent = [-1] * vertices
for v in range(vertices):
if not visited[v]:
graph_dfs(v, visited, parent, low, disc)
return ans
if __name__ == '__main__':
_input = sys.stdin.readlines()
vertices, edges = map(int, _input[0].split())
v_info = map(lambda x: x.split(), _input[1:])
Time = 0
ans = []
init_adj_table = tuple([] for _ in range(vertices))
adj_table = generate_adj_table(v_info)
res = bridge()
res.sort()
for ele in res:
print(*ele)
```
No
| 92,612 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Tags: implementation, sortings
Correct Solution:
```
n=int(input())
s=input()
print("Yes" if n==1or n>26or len(set(s))!=n else "No")
```
| 92,613 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Tags: implementation, sortings
Correct Solution:
```
n=int(input())
s=input().strip()
arr=[]
for i in range(26):
arr.append(0)
for i in s:
arr[ord(i)-97]+=1
flag=1
if (n==1):
print ("Yes")
else:
for i in range(26):
if (arr[i]!=0):
if (arr[i]!=1):
print ("Yes")
flag=0
break
if (flag==1):
print ("No")
```
| 92,614 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Tags: implementation, sortings
Correct Solution:
```
n = int(input())
s = input()
arr = [0] * 26
for i in s:
arr[ord(i)-97] += 1
if n == 1:
print('YES')
exit()
else:
for i in arr:
if i >= 2:
print('YES')
exit()
print('NO')
```
| 92,615 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Tags: implementation, sortings
Correct Solution:
```
import sys
#sys.stdin = open("py_in.txt","r")
freq = [0] * 27
n = int(input())
p = input()
if(n==1):
print("Yes")
else:
flag = False
for i in range(n):
id = ord(p[i]) - ord('a')
freq[id] += 1
if(freq[id]>1):
flag = True
break
if(flag==True):
print("Yes")
else:
print("No")
```
| 92,616 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Tags: implementation, sortings
Correct Solution:
```
#-------------Program--------------
#----Kuzlyaev-Nikita-Codeforces----
#-------------Training-------------
#----------------------------------
n=int(input())
s=list(str(input()))
if n>26:print("Yes")
else:
l=list(set(s))
if len(l)==len(s) and n!=1:
print("No")
else:
print("Yes")
```
| 92,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Tags: implementation, sortings
Correct Solution:
```
import functools
x = input()
sequence = input()
sequence = ''.join(sorted(sequence))
previous_char = None
is_possible = False
if len(sequence) == 1:
print("YES")
else:
for char in sequence:
if char == previous_char:
is_possible = True
break
previous_char = char
if is_possible:
print("YES")
else:
print("NO")
```
| 92,618 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Tags: implementation, sortings
Correct Solution:
```
#!/usr/bin/python
import sys
a = input()
a = input()
count = [0] * 26
for i in a:
count[ord(i) - ord('a')] += 1
if (max(count) >= 2) or (len(a) == 1):
print('Yes')
else:
print('No')
```
| 92,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Tags: implementation, sortings
Correct Solution:
```
T=int(input())
text=input()
L=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for i in range(T):
L[ord(text[i])-ord('a')]+=1
check=True
for item in L:
if item>=2:
check=False
break
if T==1:
print('Yes')
elif check:
print('No')
else:
print('Yes')
```
| 92,620 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Submitted Solution:
```
n = int(input())
s = input()
if len(s) == 1:
print("Yes")
exit()
m = {}
for i in s:
if i in m:
print("Yes")
exit()
else:
m[i] = 1
print("No")
```
Yes
| 92,621 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Submitted Solution:
```
#codeforces_1025A_live
gi = lambda: list(map(int,input().split()))
n = gi()[0]
s = input()
if n > 26 or n==1:
print("Yes")
exit();
if len(set(list(s))) == n:
print("No")
else:
print("Yes")
```
Yes
| 92,622 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Submitted Solution:
```
n = int(input())
s = list(input())
g = set(s)
print("Yes" if len(g) != n or n == 1 else "No")
```
Yes
| 92,623 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Submitted Solution:
```
n = int(input())
s = input()
a = 0
if len(s) == 1:
print('Yes')
else:
boolean = False
for i in set(s):
if s.count(i) != 1:
boolean = True
break
if boolean:
print('Yes')
else:
print('No')
```
Yes
| 92,624 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Submitted Solution:
```
x=int(input())
str=input()
t=0
d={ i : [0] for i in "abcdefhijklmnopqrstuvwxyz"}
for i in d :
#print(i)
d[i]=str.count(i)
if d[i]>1 :
t=1
break
if (1==t) :
print("YES")
else :
print("NO")
```
No
| 92,625 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Submitted Solution:
```
if __name__ == "__main__":
num_of_puppies = int(input().strip())
colors = input().strip().lower()
puppies = [0] * 26 # 26 : is the English letters.
for i in colors:
puppies[ord(i) - ord('a')] += 1
standard = False
for i in puppies:
if i >= 2:
standard = True
break
if standard:
print("YES")
else:
print("NO")
```
No
| 92,626 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Submitted Solution:
```
def find_pair(string, len_string):
for index in range(0, len_string - 1):
char = string[index]
for chars in string[index + 1:]:
if char == chars:
return "Yes"
return "No"
numberPuppies = int(input())
string = input()
print(find_pair(string, numberPuppies))
```
No
| 92,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive.
The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color.
Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color x such that there are currently at least two puppies of color x and recolor all puppies of the color x into some arbitrary color y. Luckily, this operation can be applied multiple times (including zero).
For example, if the number of puppies is 7 and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose x='c' right now, because currently only one puppy has the color 'c'.
Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color.
Input
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of puppies.
The second line contains a string s of length n consisting of lowercase Latin letters, where the i-th symbol denotes the i-th puppy's color.
Output
If it's possible to recolor all puppies into one color, print "Yes".
Otherwise print "No".
Output the answer without quotation signs.
Examples
Input
6
aabddc
Output
Yes
Input
3
abc
Output
No
Input
3
jjj
Output
Yes
Note
In the first example Slava can perform the following steps:
1. take all puppies of color 'a' (a total of two) and recolor them into 'b';
2. take all puppies of color 'd' (a total of two) and recolor them into 'c';
3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'.
In the second example it's impossible to recolor any of the puppies.
In the third example all the puppies' colors are the same; thus there's no need to recolor anything.
Submitted Solution:
```
import collections
n = int(input())
s = input()
d = collections.Counter()
for i in s:
d[i]+=1
if (max(d.values()) >= 2):
print ("YES")
else:
print ("NO")
```
No
| 92,628 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Tags: constructive algorithms, graphs
Correct Solution:
```
n,m=map(int,input().split())
arr=[]
for i in range(m):
x,y=map(int,input().split())
arr.append([x,y])
dict1={}
k=1
for i in range(m):
a=arr[i][0]
b=arr[i][1]
try:
dict1[a].append((k,a))
except:
dict1[a]=[(k,a)]
try:
dict1[b].append((k,b))
except:
KeyError
dict1[b]=[(k,b)]
k+=1
#print(dict1)
for i in range(1,n+1):
try:
z=len(dict1[i])
except:
KeyError
dict1[i]=[(k,i)]
k+=1
for i in range(1,n+1):
print(len(dict1[i]))
for j in dict1[i]:
print(j[0],j[1])
```
| 92,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Tags: constructive algorithms, graphs
Correct Solution:
```
if __name__ == '__main__':
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for edge_id, i in enumerate(range(m)):
a, b = map(int, input().split())
edges[a-1].append((b-1, edge_id))
edges[b-1].append((a-1, edge_id))
cnt = 0
ans = [[] for _ in range(n)]
for i in range(n):
for j, (node, edge_id) in enumerate(edges[i]):
cnt += 1
ans[i].append((cnt, i + 1))
ans[i].append((cnt, n + 1 + edge_id))
if len(edges[i]) == 0:
cnt += 1
ans[i].append((cnt, i + 1))
print('\n'.join('%d\n' % len(color) + '\n'.join('%d %d' % (x, y) for (x, y) in color) for color in ans))
```
| 92,630 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Tags: constructive algorithms, graphs
Correct Solution:
```
n,m = map(int, input().split())
d = {}
for i in range(1,n+1):
d[i] = set()
for i in range(m):
x,y = map(int, input().split())
d[x].add(x*n+y)
d[y].add(x*n+y)
for i in range(1,n+1):
print(len(d[i])+1)
print(i,i)
for k in d[i]:
print(i,k)
```
| 92,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Tags: constructive algorithms, graphs
Correct Solution:
```
from collections import defaultdict
def compute(n, col_pairs):
pos = defaultdict(list)
for i in range(1, n+1):
pos[i].append((i, i))
for i, (u, v) in enumerate(col_pairs, 1):
pos[u].append((u, n + i))
pos[v].append((v, n + i))
for i in range(1, n+1):
print(len(pos[i]))
for p1, p2 in pos[i]:
print(p1, p2)
def run():
# n = number of colors [1,100]
# m = number of color pairs that harmonize with each other [0, min(1000, n*(n+1)/2)]
n, m = [int(c) for c in input().split()]
col_pairs = []
for _ in range(m):
col1, col2 = [int(c) for c in input().split()]
col_pairs.append((col1, col2))
compute(n, col_pairs)
if __name__ == '__main__':
run()
```
| 92,632 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Tags: constructive algorithms, graphs
Correct Solution:
```
n,m=[int(x) for x in input().split()]
ms=[]
con=[set()for i in range(n)]
loc=[0]*n
for i in range(m):
a,b=[int(x) for x in input().split()]
a-=1
b-=1
con[a].add(b)
con[b].add(a)
ms.append((a,b))
ans=[[]for i in range(n)]
if len(con[0])==0:
con[0].add(n+10)
l=len(con[0])
ans[0]=[i for i in range(l)]
for i in range(1,n):
loc[i]=l
if len(con[i])==0:
con[i].add(n+10)
for s in con[i]:
if s<i:
ans[i].append(loc[s])
loc[s]+=1
else:
ans[i].append(l)
l+=1
for i in range(n):
print(len(ans[i]))
for s in ans[i]:
print(i+1,s+1)
```
| 92,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Tags: constructive algorithms, graphs
Correct Solution:
```
# -*- coding:utf-8 -*-
"""
created by shuangquan.huang at 11/3/18
"""
N, M = map(int, input().split())
ans = [(0, 0)] + [[(i+1, i+10000)] for i in range(N)]
y = 9999
for mi in range(M):
u, v = map(int, input().split())
ans[u].append((u, y))
ans[v].append((v, y))
y -= 1
outs = []
for i in range(1, N+1):
outs.append(str(len(ans[i])))
for x, y in ans[i]:
outs.append('{} {}'.format(x, y))
print('\n'.join(outs))
```
| 92,634 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Tags: constructive algorithms, graphs
Correct Solution:
```
# Author: Divesh Uttamchandani
# Institution: BITS Pilani
n,m = list(map(int,input().strip().split()))
graph = [set({}) for i in range(n+1)]
for i in range(m):
a,b = list(map(int,input().strip().split()))
graph[min(a,b)].add((max(a,b)))
points = [(set({})) for i in range(n+1)]
points_start = [-1 for i in range(n+2)]
s=1
for j in range(1, n+1):
i=graph[j]
points_start[j]=s
s+=len(i)+1
points_start[-1]=s
for i in range(1,n+1):
for d in range(points_start[i+1]-points_start[i]):
points[i].add((points_start[i]+d, i))
#print(points)
for coord, edges in enumerate(graph):
if(coord!=0):
for i,edge in enumerate(edges):
#coord, edge is and edge
points[edge].add((points_start[coord]+i, edge))
for coord, p in enumerate(points):
if(coord!=0):
print(len(p))
for j in p:
print(j[0], j[1])
# <> with <3 using Termicoder:
# https://termicoder.github.io
```
| 92,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Tags: constructive algorithms, graphs
Correct Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 1 12:14:13 2020
@author: shailesh
"""
n,m = [int(i) for i in input().split()]
harmonies = []
for i in range(m):
harmonies.append([int(i) for i in input().split()])
#stage 1: diagonal
rooks_by_colour = [[] for i in range(n+1)]
for i in range(1,n+1):
rooks_by_colour[i].append((i,i))
next_row = n+1
for harmony in harmonies:
colour1,colour2 = harmony
rooks_by_colour[colour1].append((next_row,colour1))
rooks_by_colour[colour2].append((next_row,colour2))
next_row+=1
for i in range(1,n+1):
print(len(rooks_by_colour[i]))
for i in rooks_by_colour[i]:
print(i[0],i[1])
```
| 92,636 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Submitted Solution:
```
n, m = map(int, input().split())
res = [[(i+1, 5001+i)] for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
res[a-1].append((a, i+1))
res[b-1].append((b, i+1))
for i in range(n):
print(len(res[i]))
for p in res[i]:
print(*p)
```
Yes
| 92,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Submitted Solution:
```
n, m = map(int, input().split())
clr = [[] for i in range(n)]
for i in range(m):
a, b = sorted(map(int, input().split()))
a -= 1
b -= 1
clr[a].append(b)
ys = [-1 for i in range(n)]
result = [[] for i in range(n)]
global y, x
x = 1
y = 1
def visit(cur):
global y, x
y += 1
result[cur].append((x, y,))
x += 1
ys[cur] = y
for adj in clr[cur]:
if ys[adj] == -1:
visit(adj)
result[cur].append((x, ys[cur],))
result[adj].append((x, ys[adj],))
x += 1
for i in range(n):
if ys[i] == -1:
visit(i)
for i in range(n):
print(len(result[i]))
for j in result[i]:
print(*j)
```
Yes
| 92,638 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Submitted Solution:
```
# -*- coding:utf-8 -*-
"""
created by shuangquan.huang at 11/3/18
"""
N, M = map(int, input().split())
ans = [(0, 0)] + [[(i+1, i+10000)] for i in range(N)]
y = 9999
for mi in range(M):
u, v = map(int, input().split())
ans[u].append((u, y))
ans[v].append((v, y))
y -= 1
for i in range(1, N+1):
print(len(ans[i]))
for x, y in ans[i]:
print('{} {}'.format(x, y))
```
Yes
| 92,639 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Submitted Solution:
```
# use this as the main template for python problems
from collections import Counter
if __name__ == "__main__":
# single variables
n, m = [int(val) for val in input().split()]
harmony = [set([]) for i in range(n)]
for i in range(m):
# get the pair, divide larger and smaller
a, b = [int(val) for val in input().split()]
a -= 1
b -= 1
harmony[a].add(b)
harmony[b].add(a)
pos = [[] for i in range(n)]
# each color gets a unique column
# all intra harmony will be within a column
# all inter harmony will be within rows
v = 1
vertical_positions = {}
for i in range(n):
s = harmony[i]
count = len(s)
vertical_positions[i] = set([])
if(count == 0):
vertical_positions[i].add(v)
pos[i].append((i+1, v))
v += 1
else:
for color in s:
if(color < i):
val = vertical_positions[color].pop()
pos[i].append((i+1, val))
else:
vertical_positions[i].add(v)
pos[i].append((i+1, v))
v += 1
for i in pos:
print(len(i))
for j in i:
print(j[0], j[1])
```
Yes
| 92,640 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Submitted Solution:
```
n, m = map(int, input().split())
j = 0
lis = [[] for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
lis[a - 1].append(b)
lis[b - 1].append(a)
for pos in range(n):
print(len(lis[pos]))
for i in lis[pos]:
print(str(pos + 1), str(i))
```
No
| 92,641 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# ------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def print_list(l):
print(' '.join(map(str,l)))
# sys.setrecursionlimit(300000)
# from heapq import *
# from collections import deque as dq
# from math import ceil,floor,sqrt,pow
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
n,m = RL()
dic = [[i] for i in range(n+1)]
for _ in range(m):
a,b = RL()
dic[a].append(b)
for i in range(1,n+1):
print(len(dic[i]))
for j in dic[i]:
print(i,j)
```
No
| 92,642 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Submitted Solution:
```
n, m = map(int, input().split())
a = []
for i in range(n):
a.append([i * n, i * n])
for i in range(m):
x, y = map(int, input().split())
x -= 1
y -= 1
if x > y:
x, y = y, x
a[x].append([x*n, x * n + y])
a[y].append([y*n, x * n + y])
for i in range(n):
print(len(a[i]))
for j in a:
print(j[0]+1, j[1] + 1)
```
No
| 92,643 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Ivan is a novice painter. He has n dyes of different colors. He also knows exactly m pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has 5000 rooks. He wants to take k rooks, paint each of them in one of n colors and then place this k rooks on a chessboard of size 10^{9} × 10^{9}.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
* For any color there is a rook of this color on a board;
* For any color the set of rooks of this color is connected;
* For any two different colors a b union of set of rooks of color a and set of rooks of color b is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
Input
The first line of input contains 2 integers n, m (1 ≤ n ≤ 100, 0 ≤ m ≤ min(1000, (n(n-1))/(2))) — number of colors and number of pairs of colors which harmonize with each other.
In next m lines pairs of colors which harmonize with each other are listed. Colors are numbered from 1 to n. It is guaranteed that no pair occurs twice in this list.
Output
Print n blocks, i-th of them describes rooks of i-th color.
In the first line of block print one number a_{i} (1 ≤ a_{i} ≤ 5000) — number of rooks of color i. In each of next a_{i} lines print two integers x and y (1 ≤ x, y ≤ 10^{9}) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed 5000.
It is guaranteed that the solution exists.
Examples
Input
3 2
1 2
2 3
Output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
Input
3 3
1 2
2 3
3 1
Output
1
1 1
1
1 2
1
1 3
Input
3 1
1 3
Output
1
1 1
1
2 2
1
3 1
Note
Rooks arrangements for all three examples (red is color 1, green is color 2 and blue is color 3).
<image>
<image>
<image>
Submitted Solution:
```
#import sys
#sys.stdin = open("123.data")
n, m = map(int, input().split())
g = [[(i, i)] for i in range(n)]
k = n
for i in range(m):
a1, b1 = map(int, input().split())
g[a1 - 1].append((b1 - 1, k))
g[b1 - 1].append((a1 - 1, k))
#print(k, 11111111)
k += 1
#print(g)
for i in range(n):
print(len(g[i]))
for x in g[i]:
print(x[0] + 1, x[1] + 1)
```
No
| 92,644 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Tags: greedy
Correct Solution:
```
def func(inpstr):
return inpstr[0]
N = int(input())
arr = []
money = 0
for i in range(N):
a = [int(i) for i in input().split()]
arr.append([max(a[1::])] + [a[0]])
arr.sort(key = func)
while(len(arr) != 1):
p = arr[-2][1]
m = arr[-1][0] - arr[-2][0]
money += m * p
arr[-1][1] += p
arr.pop(-2)
print(money)
```
| 92,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Tags: greedy
Correct Solution:
```
n=int(input())
a=[]
b=[]
for i in range (n):
s=list (map (int, input().strip().split()))
a.append(s.pop(0))
b.append(max(s))
c=max(b)
sum = 0
for i in range(n):
sum+=(c-b[i])*a[i]
print(sum)
```
| 92,646 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Tags: greedy
Correct Solution:
```
n = int(input())
CompS = []
S = 0
for i in range(n):
m, *H = list(map(int, input().split()))
Max = max(H)
CompS.append((Max, m))
CompS.sort(reverse=True)
for i in range(1, len(CompS)):
S += (CompS[0][0] - CompS[i][0]) * CompS[i][1]
print(S)
```
| 92,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Tags: greedy
Correct Solution:
```
n = int(input())
b = [int(x) for x in input().split()]
q = b[0]
p = max(b[1:])
total = 0
for i in range(n-1):
a = [int(x) for x in input().split()]
c = a[0]
m = max(a[1:])
if p > m:
total += c * (p - m)
elif m > p:
total += q * (m - p)
p = m
q += c
print(total)
```
| 92,648 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Tags: greedy
Correct Solution:
```
from sys import stdin, stdout
input = stdin.readline
n = (int)(input())
a,s,t =[],0,[]
for i in range(n):
b = list(map(int,input().split()))
a.append(max(b[1:]))
t.append(b[0])
boro = max(a)
for i in range(n):
s+=(boro-a[i])*t[i]
stdout.write(str(s))
```
| 92,649 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Tags: greedy
Correct Solution:
```
import sys
input=sys.stdin.readline
N=int(input())
tot=[]
for i in range(N):
x=list(map(int,input().split()))
maxim=max(x[1:])
n=x[0]
tot.append((maxim,n))
tot.sort()
counter=0
summa=tot[0][1]
for i in range(1,len(tot)):
counter+=(tot[i][0]-tot[i-1][0])*summa
summa+=tot[i][1]
print(counter)
```
| 92,650 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Tags: greedy
Correct Solution:
```
n = int(input())
M = []
A = []
for i in range(n):
a = list(map(int, input().split(' ')))
M.append(a[0])
A.append(max(a[1:]))
temp = max(A)
ans = 0
for i in range(n):
ans += abs(temp-A[i])*M[i]
print(ans)
```
| 92,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Tags: greedy
Correct Solution:
```
#In the name of GOD!
n = int(input())
cmpn = []
for i in range(n):
s = list(map(int, input().split()))
s[0] = 0
cmpn.append([max(s), len(s) - 1])
cmpn.sort()
ans = 0
mx = cmpn[n - 1][0]
for i in range(n):
ans += (mx - cmpn[i][0]) * cmpn[i][1]
print(ans)
```
| 92,652 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 9 14:00:51 2018
@author: mach
"""
i = int(input())
l = []
for _ in range(i):
k = list(map(int, input().strip().split()))
l.append(k)
#//l = [[2,4,3],[2,2,1],[3,1,1,1]]
l.sort(key=lambda x : max(x[1:]),reverse = True)
k = max(l[0][1:])
sums = 0
for i in range(1,len(l)):
j = (k - max(l[i][1:])) * (len(l[i]) - 1)
sums += j
print(sums)
```
Yes
| 92,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Submitted Solution:
```
import sys
input=sys.stdin.readline
n=int(input())
m=-10
ans=0
sum_of_tedad=0
for _ in range(n):
f=list(map(int,input().split()))
a=f[0]
sum_of_tedad+=a
f[0]=-10
k=max(f)
ans-=(k*a)
m=max(m,k)
ans+=(m*sum_of_tedad)
print(ans)
```
Yes
| 92,654 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
start_time = time.time()
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
def getMat(n):
return [getInts() for _ in range(n)]
def get_ints():return map(int, sys.stdin.readline().split())
T=int(input());ar=[];maxx=0
for _ in range(T):
arr=list(get_ints());m=max(arr[1:])
ar.append([m,arr[0]])
maxx=max(m,maxx)
ans=0#;print(maxx)
for i in range(T):
ans+=(maxx-ar[i][0])*ar[i][1]
print(ans)
```
Yes
| 92,655 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Submitted Solution:
```
n = int(input())
maxsalary = 0
fulllist = []
for i in range(n):
line = input()
tokens = line.split()
m = int(tokens[0])
lst = []
for j in range(m):
lst.append(int(tokens[j+1]))
maxsalaryofthisline = max(lst)
maxsalary = max(maxsalary, maxsalaryofthisline)
fulllist.append((maxsalaryofthisline, m))
increase = 0
for lst in fulllist:
maxsalaryofthisline, numberofemployee = lst
increase += numberofemployee * (maxsalary - maxsalaryofthisline)
print(increase)
```
Yes
| 92,656 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def main():
n = int(input())
cur_max = -100
answer = 0
for i in range(n):
m, *a = map(int, input().split())
if i == 0:
cur_max = max(a)
else:
q = max(a)
answer += (abs(cur_max - q)*m)
cur_max = max(cur_max, q)
print(answer)
return
if __name__=="__main__":
main()
```
No
| 92,657 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Submitted Solution:
```
n = int(input())
allemp = []
while n != 0:
inp = list(map(int, input().split()))
m = inp[0]
emp = inp[1:]
allemp.append(emp)
n -= 1
allemp.sort(reverse=True)
maxval = max(allemp[0])
inc = 0
for val in allemp[1:]:
inc += (maxval - max(val)) * len(val)
print(inc)
```
No
| 92,658 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Submitted Solution:
```
n=int(input())
A=[]
for i in range (n):
B=list(map(int,input().split()))
ni=B[0]
B.remove(ni)
ai=max(B)
T=[ni,ai]
A.append(T)
p=0
q=0
for i in range(1,n):
p+=A[i][0]
q+=A[i][0]*A[i][1]
ans=A[0][1]*p-q
print(ans)
```
No
| 92,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left.
But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal.
To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase.
Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one.
Input
The first line contains a single integer n — the number of companies in the conglomerate (1 ≤ n ≤ 2 ⋅ 10^5). Each of the next n lines describes a company.
A company description start with an integer m_i — the number of its employees (1 ≤ m_i ≤ 2 ⋅ 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9.
The total number of employees in all companies does not exceed 2 ⋅ 10^5.
Output
Output a single integer — the minimal total increase of all employees that allows to merge all companies.
Example
Input
3
2 4 3
2 2 1
3 1 1 1
Output
13
Note
One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
Submitted Solution:
```
#n,m = map(int, input().strip().split(' '))
n=int(input())
c=0
m=0
for i in range(n):
lst = list(map(int, input().strip().split(' ')))
m1=max(lst[1:])
if m1>=m:
m=m1
else:
c+=(m-m1)*lst[0]
print(c)
```
No
| 92,660 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Tags: greedy, implementation
Correct Solution:
```
### Chocolates ###
### https://codeforces.com/contest/1139/problem/B ###
n = int(input())
a = [int(c) for c in input().split()]
i = n-1
s = a[i]
while a[i] != 0 and i >= 1:
if a[i-1] < a[i]:
s = s + a[i-1]
else:
a[i-1] = a[i] - 1
s = s + a[i-1]
i = i - 1
print(s)
```
| 92,661 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Tags: greedy, implementation
Correct Solution:
```
# cook your dish here
n=int(input())
L=[int(x) for x in input().split()]
c=0
for i in range(n-1,-1,-1):
if i==n-1:
c+=L[i]
else:
if L[i] >= L[i+1] and L[i+1]!=0:
d=(L[i]-L[i+1])+1
m=L[i]-d
c+=m
L[i]=m
elif L[i+1]==0:
L[i]=0
c+=0
else:
c+=L[i]
print(c)
```
| 92,662 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Tags: greedy, implementation
Correct Solution:
```
input()
chocos=list(map(int,input().split()))[::-1]+[0,0]
numbers=chocos[0]
for i in range(len(chocos)-2):
if chocos[i]==0:
break
elif chocos[i+1]<chocos[i]:
numbers+=chocos[i+1]
else:
numbers+=chocos[i]-1
chocos[i+1]=chocos[i]-1
print(numbers)
```
| 92,663 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Tags: greedy, implementation
Correct Solution:
```
x = int(input())
s = str(input()).split()
for i in range(x):
s[i] = int(s[i])
s = s[::-1]
sumi = s[0]
for i in range(1, x):
if s[i-1] > s[i]:
sumi += s[i]
else:
if s[i-1]-1 > 0:
sumi += s[i-1]-1
s[i] = s[i-1]-1
else:
break
print(sumi)
```
| 92,664 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
u = list(map(int, input().split()))
for i in range(n - 2, -1, -1):
if u[i] >= u[i + 1] and u[i] != 0:
u[i] = u[i + 1] - 1
if u[i + 1] == 0:
u[i] = 0
print(sum(u))
```
| 92,665 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
l=list(map(int,input().split()))
m=l[n-1]
for i in range(n-2,-1,-1):
if l[i]>=l[i+1]:
l[i]=l[i+1]-1
if l[i]==0:
break
m+=l[i]
print(m)
```
| 92,666 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Tags: greedy, implementation
Correct Solution:
```
import sys
sys.setrecursionlimit(2000)
from collections import Counter
from functools import reduce
# sys.stdin.readline()
if __name__ == "__main__":
# single variables
n = [int(val) for val in sys.stdin.readline().split()][0]
a = [int(val) for val in sys.stdin.readline().split()]
prev = a[-1]
count = prev
for i in range(n-2, -1, -1):
prev = min(a[i], prev-1)
prev = max(prev, 0)
count += prev
print(count)
```
| 92,667 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
s=list(map(int,input().split()))
k=1e20
tot=0
for i in s[::-1]:
if k<=1:
break
elif k<=i:
tot+=k-1
k-=1
elif k>i:
tot+=i
k=i
print(tot)
```
| 92,668 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
ans = a[n-1]
av= a[n-1]-1
for i in range (n-2,-1,-1):
#print (ans)
if av<=0:
break
if a[i]>(av):
ans = ans+av
av = av-1
elif a[i]<=av:
ans = ans+ a[i]
av = a[i]-1
print (ans)
```
Yes
| 92,669 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
num = arr[-1]
arr2 = [num]
arr.reverse()
arr.pop(0)
for k in arr:
num = min(num-1, k)
if num < 0:
arr2.append(0)
else:
arr2.append(num)
print(sum(arr2))
```
Yes
| 92,670 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
ans=a[-1]
for i in range(n-2,-1,-1):
if a[i+1]==0:
a[i]=0
elif a[i]<a[i+1]:
ans+=a[i]
elif a[i]==[i-1]:
a[i]=a[i]-1
ans+=a[i]
else:
a[i]=a[i+1]-1
ans+=a[i]
print(ans)
```
Yes
| 92,671 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Submitted Solution:
```
import io, os
input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
n = ii()
a = li()
ans, cur = 0, 10 ** 9 + 1
while a and cur:
cur = min(cur - 1, a.pop())
ans += cur
print(ans)
```
Yes
| 92,672 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
ans = 0
prev = a[n-1]
count = prev
for i in range(n-2, -1, -1):
if a[i] < prev:
count += a[i]
ans = max(ans, count)
prev = a[i]
else:
count += prev - 1
ans = max(ans, count)
prev = prev - 1
if a[i] == 1:
count = 0
print(ans)
```
No
| 92,673 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
#print(a)
ans = a[len(a)-1]
#print(ans)
prev = a[len(a)-1]
for i in range(len(a)-1, 0, -1):
if a[i-1] < prev:
ans += a[i-1]
prev = a[i-1]
print("prev:", i, prev, ans)
else:
if prev > 0:
prev = prev-1
ans += prev
else:
break
print("no:", i, prev, ans)
print(ans)
```
No
| 92,674 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Submitted Solution:
```
amount = int(input())
array = [int(s) for s in input().split()]
#left = 0
#right = 0
#l_idx = 0
#r_idx = 0
#max_ = array[0]
#curr_sum = array[0]
#for i in range(1, len(array)):
# if array[i] > array[i - 1]:
# right += 1
# curr_sum += array[i]
# else:
# left = i
# right = i
# curr_sum = array[i]
#
# if curr_sum >= max_:
# l_idx = left
# r_idx = right
# max_ = curr_sum
ans = array[-1]
#print(max_)
#print(l_idx)
curr = array[-1]
for i in range(len(array) - 2, max(-1,len(array) - 1 - array[-1]), -1):
if array[i] >= curr - 1:
ans += curr - 1
curr -= 1
else:
ans += array[i]
curr = array[i]
#print(ans)
print(ans)
```
No
| 92,675 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You went to the store, selling n types of chocolates. There are a_i chocolates of type i in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy x_i chocolates of type i (clearly, 0 ≤ x_i ≤ a_i), then for all 1 ≤ j < i at least one of the following must hold:
* x_j = 0 (you bought zero chocolates of type j)
* x_j < x_i (you bought less chocolates of type j than of type i)
For example, the array x = [0, 0, 1, 2, 10] satisfies the requirement above (assuming that all a_i ≥ x_i), while arrays x = [0, 1, 0], x = [5, 5] and x = [3, 2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer n (1 ≤ n ≤ 2 ⋅ 10^5), denoting the number of types of chocolate.
The next line contains n integers a_i (1 ≤ a_i ≤ 10^9), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
Note
In the first example, it is optimal to buy: 0 + 0 + 1 + 3 + 6 chocolates.
In the second example, it is optimal to buy: 1 + 2 + 3 + 4 + 10 chocolates.
In the third example, it is optimal to buy: 0 + 0 + 0 + 1 chocolates.
Submitted Solution:
```
n=int(input())
l=list(map(int,input().split()))
last=l[n-1]
ans=l[n-1]
l.reverse()
for i in l[1:]:
last-=1
if last==0:
break
while last>i:
last-=1
ans+=last
print(last)
```
No
| 92,676 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Tags: greedy
Correct Solution:
```
numero = int(input())
pos = []
contador = 0
lista = [int(x) for x in input().split()]
if len(lista )== 1:
pos.append('R')
contador +=1
print(contador)
for x in pos:
print(x,end="")
print()
quit()
if lista[0] > lista[len(lista)-1]:
pos.append('R')
menor = lista.pop(len(lista)-1)
else:
pos.append('L')
menor = lista.pop(0)
contador += 1
inicio = 0
fim = len(lista)-1
tamanho = len(lista)
while True:
if tamanho == 1:
if lista[inicio] > menor:
contador +=1
pos.append('L')
break
else:
break
if lista[inicio] > lista[fim] and lista[fim] > menor:
pos.append('R')
menor = lista[fim]
fim = fim -1
tamanho -= 1
elif lista[inicio] < lista[fim] and lista[inicio] > menor:
pos.append('L')
menor = lista[inicio]
inicio += 1
tamanho -= 1
elif lista[inicio] > lista[fim] and lista[inicio] > menor:
pos.append('L')
menor = lista[inicio]
inicio += 1
tamanho -= 1
elif lista[inicio] < lista[fim] and lista[fim] > menor:
pos.append('R')
menor = lista[fim]
fim = fim -1
tamanho -= 1
else:
break
contador+=1
print(contador)
for x in pos:
print(x,end="")
print()
```
| 92,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Tags: greedy
Correct Solution:
```
def solve():
N = int(input())
A = list(map(int,input().split()))
ans = ''
l = 0
r = N - 1
old = 0
while l <= r:
#print(l,r)
if A[l] < A[r]:
if old < A[l]:
old = A[l]
ans += 'L'
l += 1
elif old < A[r]:
old = A[r]
ans += 'R'
r -= 1
else:
break
else:
if old < A[r]:
old = A[r]
ans += 'R'
r -= 1
elif old < A[l]:
old = A[l]
ans += 'L'
l += 1
else:
break
print(len(ans))
print(ans)
solve()
```
| 92,678 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Tags: greedy
Correct Solution:
```
n = int(input())
A = input()
A = [int(x) for x in A.split()]
i = 0
j = len(A) - 1
last = float("-inf")
out = []
while i <= j:
if A[i] > last and (A[i] <= A[j] or A[j] < last):
out.append("L")
last = A[i]
i += 1
elif A[j] > last and (A[j] <= A[i] or A[i] < last):
out.append("R")
last = A[j]
j -= 1
else:
break
print(len(out))
print("".join(out))
```
| 92,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Tags: greedy
Correct Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
i = 0
j = n-1
prev = -1
ans = []
while(i<=j):
if a[i]<a[j]:
if a[i]>prev:
ans += 'L'
prev = a[i]
i += 1
elif a[j]>prev:
ans += 'R'
prev = a[j]
j -= 1
else:
break
elif a[i]>a[j]:
if a[j]>prev:
ans += 'R'
prev = a[j]
j -= 1
elif a[i]>prev:
ans += 'L'
prev = a[i]
i += 1
else:
break
elif a[i]>prev:
l = 0
for p in range(i+1, j):
if a[p]>a[p-1]:
l += 1
else:
break
r = 0
for p in range(j-1, i, -1):
if a[p]>a[p+1]:
r += 1
else:
break
if r>l:
ans += 'R'*(r+1)
prev = a[j]
j -= 1
else:
ans += 'L'*(l+1)
prev = a[i]
i += 1
break
else:
break
print(len(ans))
print(*ans, sep = "")
```
| 92,680 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Tags: greedy
Correct Solution:
```
input();a=*map(int,input().split()),;r,s=len(a)-1,'';l=e=0
while l<=r and max(a[l],a[r])>e:
L,R=a[l],a[r]
if L>e>R or R>L>e:e=L;l+=1;s+='L'
else:e=R;r-=1;s+='R'
print(len(s),s)
```
| 92,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Tags: greedy
Correct Solution:
```
def main():
n = int(input())
seq = list(map(int, input().split(' ')))
i = 0
j = len(seq) - 1
res = []
res_seq = []
while i < j:
if len(res) == 0:
if seq[i] <= seq[j]:
res.append(seq[i])
i += 1
res_seq.append('L')
else:
res.append(seq[j])
j -= 1
res_seq.append('R')
elif seq[i] > res[len(res) - 1] and seq[j] > res[len(res) - 1]:
if seq[i] < seq[j]:
res.append(seq[i])
i += 1
res_seq.append('L')
else:
res.append(seq[j])
j -= 1
res_seq.append('R')
else:
if seq[i] > res[len(res) - 1]:
res.append(seq[i])
res_seq.append('L')
i += 1
elif seq[j] > res[len(res) - 1]:
res.append(seq[j])
res_seq.append('R')
j -= 1
else:
break
if i == j and len(res) == 0:
res.append(seq[i])
res_seq.append('L')
elif i == j and seq[i] >= res[len(res) - 1]:
res.append(seq[i])
res_seq.append('L')
print(len(res))
print("".join(res_seq))
if __name__ == "__main__":
main()
```
| 92,682 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Tags: greedy
Correct Solution:
```
R = lambda: map(int, input().split())
n = int(input())
L = list(R())
mi = 0
i,j = 0,n-1
res = []
while i <= j:
if mi < L[j] and mi < L[i]:
if L[i] > L[j]:
res.append('R')
mi = L[j]
j -= 1
else:
res.append('L')
mi = L[i]
i += 1
elif mi < L[j]:
res.append('R')
mi = L[j]
j -= 1
elif mi < L[i]:
res.append('L')
mi = L[i]
i += 1
else:break
print(len(res))
print(''.join(res))
```
| 92,683 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Tags: greedy
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
fi, bi = 0, n - 1
prev = 0
res = []
while fi <= bi:
f, b = a[fi], a[bi]
if prev >= f and prev >= b:
break
elif f <= b:
if f > prev:
res.append('L')
fi += 1
prev = f
else:
res.append('R')
bi -= 1
prev = b
else:
if b > prev:
res.append('R')
bi -= 1
prev = b
else:
res.append('L')
fi += 1
prev = f
print(len(res))
for c in res:
print(c, end='')
print()
```
| 92,684 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Submitted Solution:
```
from collections import deque
n = int(input())
a = deque(int(x) for x in input().split())
ans = []
temp = 0
while a:
if a[0] > temp:
if a[-1] > temp and a[-1] < a[0]:
ans.append('R')
temp = a[-1]
a.pop()
else:
ans.append('L')
temp = a[0]
a.popleft()
else:
if a[-1] > temp:
ans.append('R')
temp = a[-1]
a.pop()
else:
break
print(len(ans))
print(''.join(ans))
```
Yes
| 92,685 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Submitted Solution:
```
n=int(input())
a=*map(int,input().split()),
r=''
i=p=0
j=n-1
while i<=j and(a[i]>p or a[j]>p):
if a[i]>p>=a[j]or a[j]>=a[i]>p:r+='L';p=a[i];i+=1
elif a[j]>p>a[i]or a[i]>a[j]>p:r+='R';p=a[j];j-=1
print(len(r),r)
```
Yes
| 92,686 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Submitted Solution:
```
n = int(input())
s = list(map(int,input().split()))
t = 0
ans = ''
l = 0
r = n-1
while l <= r:
if t < s[l] < s[r]:
ans += 'L'
t = s[l]
l += 1
elif t < s[r] < s[l]:
ans += 'R'
t = s[r]
r -= 1
elif t < s[l]:
ans += 'L'
t = s[l]
l += 1
elif t < s[r]:
ans += 'R'
t = s[r]
r -= 1
else:
break
print(len(ans))
print(ans)
```
Yes
| 92,687 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Submitted Solution:
```
n = int(input())
x = list(map(int,input().split()))
l = 0
r = n-1
ans1 = 0
ans2 = ''
now = 0
while l <= r:
if now < x[l]:
if x[l] < x[r]:
ans1 += 1
ans2 = ans2+'L'
now = x[l]
l += 1
elif now > x[r]:
ans1 += 1
ans2 = ans2+'L'
now = x[l]
l += 1
else:
ans1 += 1
ans2 = ans2+'R'
now = x[r]
r -= 1
elif now < x[r]:
ans1 += 1
ans2 = ans2+'R'
now = x[r]
r -= 1
else:
break
print(ans1)
print(ans2)
```
Yes
| 92,688 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
l = 0
r = n - 1
ans = []
res = []
pos = 1
prep = []
a = n
for k in range(a):
prep.append([1, 1])
#prep = [[1 for j in range(2)] for i in range(n)]
for i in range(1, n):
if (arr[i] < arr[i - 1]):
prep[i][1] = prep[i - 1][1] + 1
for i in range(n - 2, -1, -1):
if (arr[i] < arr[i + 1]):
prep[i][0] = prep[i + 1][0] + 1
if arr[l] < arr[r]:
ans.append(arr[l])
l += 1
res.append('L')
else:
if (prep[l][0] > prep[r][1]):
ans.append(arr[l])
l += 1
res.append('L')
elif (prep[r][1] >= prep[l][0]):
ans.append(arr[r])
r -= 1
res.append('R')
while pos:
if (l == r):
if ans[len(ans) - 1] < arr[l]:
ans.append(arr[l])
res.append('R')
pos = 0
continue
elif arr[l] < arr[r]:
if arr[l] > ans[len(ans) - 1]:
ans.append(arr[l])
l += 1
res.append('L')
elif arr[r] > ans[len(ans) - 1]:
ans.append(arr[r])
r -= 1
res.append('R')
else:
pos = 0
elif arr[l] > arr[r]:
if arr[r] > ans[len(ans) - 1]:
ans.append(arr[r])
r -= 1
res.append('R')
elif arr[l] > ans[len(ans) - 1]:
ans.append(arr[l])
l += 1
res.append('L')
else:
pos = 0
else:
if r < l:
pos = 0
continue
elif (prep[l][0] > prep[r][1]):
if arr[l] > ans[len(ans) - 1]:
ans.append(arr[l])
l += 1
res.append('L')
elif arr[r] > ans[len(ans) - 1]:
ans.append(arr[r])
r -= 1
res.append('R')
else:
pos = 0
elif (prep[r][1] >= prep[l][0]):
if arr[r] > ans[len(ans) - 1]:
ans.append(arr[r])
r -= 1
res.append('R')
elif arr[l] > ans[len(ans) - 1]:
ans.append(arr[l])
l += 1
res.append('L')
else:
pos = 0
print(len(res))
for el in res:
print(el, end = "")
```
No
| 92,689 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Submitted Solution:
```
n = int(input())
nums = list(map(int, input().split()))
moves = []
prev = float('-inf')
print(prev)
while nums:
print(nums)
if nums[0] < prev and nums[-1] < prev:
break
elif nums[0] < nums[-1] and nums[0] > prev:
moves.append("L")
prev = nums.pop(0)
continue
elif nums[-1] > prev:
moves.append("R")
prev = nums.pop(-1)
continue
else:
break
print(len(moves))
print(''.join(moves))
```
No
| 92,690 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Submitted Solution:
```
def f(first, last, lastNum):
print(first, last, lastNum)
global a
if first == last:
if a[first] > lastNum:
return ['L']
else:
return []
else:
if a[first] > lastNum:
r1 = f(first + 1, last, a[first])
r1.insert(0, 'L')
else:
r1 = []
if a[last] > lastNum:
r2 = f(first, last - 1, a[last])
r2.insert(0, 'R')
else:
r2 = []
if len(r1) > len(r2):
return r1
elif len(r2) > len(r1):
return r2
elif len(r1) > 0:
return r1
else:
return []
n = int(input())
a = list(map(int, input().split()))
res = f(0, n-1, -1)
print(len(res))
print(*res, sep = '')
```
No
| 92,691 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).
You are given a sequence a consisting of n integers. All these integers are distinct, each value from 1 to n appears in the sequence exactly once.
You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).
For example, for the sequence [2, 1, 5, 4, 3] the answer is 4 (you take 2 and the sequence becomes [1, 5, 4, 3], then you take the rightmost element 3 and the sequence becomes [1, 5, 4], then you take 4 and the sequence becomes [1, 5] and then you take 5 and the sequence becomes [1], the obtained increasing sequence is [2, 3, 4, 5]).
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n), where a_i is the i-th element of a. All these integers are pairwise distinct.
Output
In the first line of the output print k — the maximum number of elements in a strictly increasing sequence you can obtain.
In the second line print a string s of length k, where the j-th character of this string s_j should be 'L' if you take the leftmost element during the j-th move and 'R' otherwise. If there are multiple answers, you can print any.
Examples
Input
5
2 1 5 4 3
Output
4
LRRR
Input
7
1 3 5 6 7 4 2
Output
7
LRLRLLL
Input
3
1 2 3
Output
3
LLL
Input
4
1 2 4 3
Output
4
LLRL
Note
The first example is described in the problem statement.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
Lind = 0
Rind = n - 1
flag = True
ans = list()
ans.append(0)
ans2 = list()
flag5 = True
flag6 = True
while flag and Rind >= Lind and flag5 and flag6:
if a[Rind] > a[Lind]:
if a[Lind] > ans[-1]:
ans.append(a[Lind])
Lind += 1
ans2.append('L')
else:
flag5 = False
elif a[Lind] > a[Rind]:
if a[Rind] > ans[-1]:
ans.append(a[Rind])
Rind -= 1
ans2.append('R')
else:
flag6 = False
else:
flag = False
if not flag or not flag5 or not flag6:
lmax = 0
rmax = 0
for i in range(Lind, Rind):
if a[i+1] > a[i]:
lmax += 1
else:
break
for i in reversed(range(Lind+1, Rind+1)):
if a[i-1] > a[i]:
rmax += 1
else:
break
if lmax > rmax:
for i in range(Lind, Lind+lmax+1):
ans.append(a[i])
ans2.append('L')
else:
for i in reversed(range(Rind-rmax, Rind+1)):
ans.append(a[i])
ans2.append('R')
print(len(ans)-1)
print(*ans2,end='',sep='')
```
No
| 92,692 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently biologists came to a fascinating conclusion about how to find a chameleon mood. Consider chameleon body to be a rectangular table n × m, each cell of which may be green or blue and may change between these two colors. We will denote as (x, y) (1 ≤ x ≤ n, 1 ≤ y ≤ m) the cell in row x and column y.
Let us define a chameleon good mood certificate to be four cells which are corners of some subrectangle of the table, such that colors in opposite cells among these four are similar, and at the same time not all of the four cell colors are similar. Formally, it is a group of four cells (x_1, y_1), (x_1, y_2), (x_2, y_1), (x_2, y_2) for some 1 ≤ x_1 < x_2 ≤ n, 1 ≤ y_1 < y_2 ≤ m, that colors of (x_1, y_1) and (x_2, y_2) coincide and colors of (x_1, y_2) and (x_2, y_1) coincide, but not all of the four cells share the same color. It was found that whenever such four cells are present, chameleon is in good mood, and vice versa: if there are no such four cells, chameleon is in bad mood.
You are asked to help scientists write a program determining the mood of chameleon. Let us consider that initially all cells of chameleon are green. After that chameleon coloring may change several times. On one change, colors of contiguous segment of some table row are replaced with the opposite. Formally, each color change is defined by three integers a, l, r (1 ≤ a ≤ n, 1 ≤ l ≤ r ≤ m). On such change colors of all cells (a, b) such that l ≤ b ≤ r are replaced with the opposite.
Write a program that reports mood of the chameleon after each change. Additionally, if the chameleon mood is good, program should find out any four numbers x_1, y_1, x_2, y_2 such that four cells (x_1, y_1), (x_1, y_2), (x_2, y_1), (x_2, y_2) are the good mood certificate.
Input
The first line of input contains three integers n, m, q (1 ≤ n, m ≤ 2000, 1 ≤ q ≤ 500 000), the sizes of the table and the number of changes respectively.
Each of the following q lines contains 3 integers a_i, l_i, r_i (1 ≤ a_i ≤ n, 1 ≤ l_i ≤ r_i ≤ m), describing i-th coloring change.
Output
Print q lines. In the i-th line report the chameleon mood after first i color changes for all 1 ≤ i ≤ q.
If chameleon is in bad mood, print the only integer -1.
Otherwise, print four integers x_1, y_1, x_2, y_2 (1 ≤ x_1 < x_2 ≤ n, 1 ≤ y_1 < y_2 ≤ m) such that four cells (x_1, y_1), (x_1, y_2), (x_2, y_1), (x_2, y_2) are the good mood certificate. If there are several ways to choose such four integers, print any valid one.
Examples
Input
2 2 6
1 1 1
2 2 2
2 1 1
1 2 2
2 2 2
1 1 1
Output
-1
1 1 2 2
-1
-1
-1
1 1 2 2
Input
4 3 9
2 2 3
4 1 2
2 1 3
3 2 2
3 1 3
1 2 2
4 2 3
1 1 3
3 1 3
Output
-1
2 1 4 3
-1
2 1 3 2
3 2 4 3
1 1 2 2
1 1 2 2
-1
2 1 3 2
Submitted Solution:
```
x,y,q=input().split()
x=int(x)
y=int(y)
q=int(q)
m=1
lis=[[0,0]]
while(m<=x):
a=[0]
n=1
while(n<=y):
a.append(0)
n+=1
lis.append(a)
m+=1
def change(a1,b1):
if(lis[a1][b1]==0):
lis[a1][b1]=1
else:
lis[a1][b1]=0
print(lis)
while(q>0):
d,e,f=input().split()
d=int(d)
e=int(e)
f=int(f)
z=e
while(z<f+1):
change(d,z)
z+=1
print(lis)
x1=1
key=0
while(x1<x):
y1=1
while(y1<y):
x2=x1+1
while(x2<=x):
y2=y1+1
while(y2<=y):
if(lis[x1][y1]==lis[x2][y2] and lis[x1][y2]==lis[x2][y1] and lis[x1][y1]!=lis[x1][y2]):
if(key==0):
print(str(x1)+" "+str(y1)+" "+str(x2)+" "+str(y2))
key=1
break
y2+=1
x2+=1
y1+=1
x1+=1
if(key==0):
print("-1")
q-=1
print(lis)
```
No
| 92,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers x and y (it is guaranteed that x > y). You may choose any prime integer p and subtract it any number of times from x. Is it possible to make x equal to y?
Recall that a prime number is a positive integer that has exactly two positive divisors: 1 and this integer itself. The sequence of prime numbers starts with 2, 3, 5, 7, 11.
Your program should solve t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
Then t lines follow, each describing a test case. Each line contains two integers x and y (1 ≤ y < x ≤ 10^{18}).
Output
For each test case, print YES if it is possible to choose a prime number p and subtract it any number of times from x so that x becomes equal to y. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
Example
Input
4
100 98
42 32
1000000000000000000 1
41 40
Output
YES
YES
YES
NO
Note
In the first test of the example you may choose p = 2 and subtract it once.
In the second test of the example you may choose p = 5 and subtract it twice. Note that you cannot choose p = 7, subtract it, then choose p = 3 and subtract it again.
In the third test of the example you may choose p = 3 and subtract it 333333333333333333 times.
Tags: math, number theory
Correct Solution:
```
for j in range(int(input())):
(x,y) = [int(e) for e in input().split()]
if x-y == 1:
print("NO")
else:
print("YES")
```
| 92,694 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers x and y (it is guaranteed that x > y). You may choose any prime integer p and subtract it any number of times from x. Is it possible to make x equal to y?
Recall that a prime number is a positive integer that has exactly two positive divisors: 1 and this integer itself. The sequence of prime numbers starts with 2, 3, 5, 7, 11.
Your program should solve t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
Then t lines follow, each describing a test case. Each line contains two integers x and y (1 ≤ y < x ≤ 10^{18}).
Output
For each test case, print YES if it is possible to choose a prime number p and subtract it any number of times from x so that x becomes equal to y. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
Example
Input
4
100 98
42 32
1000000000000000000 1
41 40
Output
YES
YES
YES
NO
Note
In the first test of the example you may choose p = 2 and subtract it once.
In the second test of the example you may choose p = 5 and subtract it twice. Note that you cannot choose p = 7, subtract it, then choose p = 3 and subtract it again.
In the third test of the example you may choose p = 3 and subtract it 333333333333333333 times.
Tags: math, number theory
Correct Solution:
```
for _ in " "*int(input()):a,b=map(int,input().split());print("YES" if a-b!=1 else "NO")
```
| 92,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers x and y (it is guaranteed that x > y). You may choose any prime integer p and subtract it any number of times from x. Is it possible to make x equal to y?
Recall that a prime number is a positive integer that has exactly two positive divisors: 1 and this integer itself. The sequence of prime numbers starts with 2, 3, 5, 7, 11.
Your program should solve t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
Then t lines follow, each describing a test case. Each line contains two integers x and y (1 ≤ y < x ≤ 10^{18}).
Output
For each test case, print YES if it is possible to choose a prime number p and subtract it any number of times from x so that x becomes equal to y. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
Example
Input
4
100 98
42 32
1000000000000000000 1
41 40
Output
YES
YES
YES
NO
Note
In the first test of the example you may choose p = 2 and subtract it once.
In the second test of the example you may choose p = 5 and subtract it twice. Note that you cannot choose p = 7, subtract it, then choose p = 3 and subtract it again.
In the third test of the example you may choose p = 3 and subtract it 333333333333333333 times.
Tags: math, number theory
Correct Solution:
```
for i in range(int(input())):
m = list(map(int,input().split()))
if m[0]-m[1] == 1:
print("NO")
else:
print("YES")
```
| 92,696 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers x and y (it is guaranteed that x > y). You may choose any prime integer p and subtract it any number of times from x. Is it possible to make x equal to y?
Recall that a prime number is a positive integer that has exactly two positive divisors: 1 and this integer itself. The sequence of prime numbers starts with 2, 3, 5, 7, 11.
Your program should solve t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
Then t lines follow, each describing a test case. Each line contains two integers x and y (1 ≤ y < x ≤ 10^{18}).
Output
For each test case, print YES if it is possible to choose a prime number p and subtract it any number of times from x so that x becomes equal to y. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
Example
Input
4
100 98
42 32
1000000000000000000 1
41 40
Output
YES
YES
YES
NO
Note
In the first test of the example you may choose p = 2 and subtract it once.
In the second test of the example you may choose p = 5 and subtract it twice. Note that you cannot choose p = 7, subtract it, then choose p = 3 and subtract it again.
In the third test of the example you may choose p = 3 and subtract it 333333333333333333 times.
Tags: math, number theory
Correct Solution:
```
def f():
n = int(input())
for _ in range(n):
a,b = map(int, input().split())
print("YES" if a-b > 1 else "NO")
f()
```
| 92,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers x and y (it is guaranteed that x > y). You may choose any prime integer p and subtract it any number of times from x. Is it possible to make x equal to y?
Recall that a prime number is a positive integer that has exactly two positive divisors: 1 and this integer itself. The sequence of prime numbers starts with 2, 3, 5, 7, 11.
Your program should solve t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
Then t lines follow, each describing a test case. Each line contains two integers x and y (1 ≤ y < x ≤ 10^{18}).
Output
For each test case, print YES if it is possible to choose a prime number p and subtract it any number of times from x so that x becomes equal to y. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
Example
Input
4
100 98
42 32
1000000000000000000 1
41 40
Output
YES
YES
YES
NO
Note
In the first test of the example you may choose p = 2 and subtract it once.
In the second test of the example you may choose p = 5 and subtract it twice. Note that you cannot choose p = 7, subtract it, then choose p = 3 and subtract it again.
In the third test of the example you may choose p = 3 and subtract it 333333333333333333 times.
Tags: math, number theory
Correct Solution:
```
t = int(input())
for j in range(1, t + 1):
x = list(map(int, input().split()))
c = x[0] - x[1]
if c == 1:
print('NO')
else:
print('YES')
```
| 92,698 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two integers x and y (it is guaranteed that x > y). You may choose any prime integer p and subtract it any number of times from x. Is it possible to make x equal to y?
Recall that a prime number is a positive integer that has exactly two positive divisors: 1 and this integer itself. The sequence of prime numbers starts with 2, 3, 5, 7, 11.
Your program should solve t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
Then t lines follow, each describing a test case. Each line contains two integers x and y (1 ≤ y < x ≤ 10^{18}).
Output
For each test case, print YES if it is possible to choose a prime number p and subtract it any number of times from x so that x becomes equal to y. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
Example
Input
4
100 98
42 32
1000000000000000000 1
41 40
Output
YES
YES
YES
NO
Note
In the first test of the example you may choose p = 2 and subtract it once.
In the second test of the example you may choose p = 5 and subtract it twice. Note that you cannot choose p = 7, subtract it, then choose p = 3 and subtract it again.
In the third test of the example you may choose p = 3 and subtract it 333333333333333333 times.
Tags: math, number theory
Correct Solution:
```
for _ in range(int(input())):
x, y = [int(x) for x in input().split()]
print(["No", "Yes"][x-y>1])
```
| 92,699 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.