text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Tags: brute force, graphs, shortest paths
Correct Solution:
```
import heapq
inf = 1000000007
n, m, k = map(int, input().split())
E = [[] for _ in range(n)]
edges = []
for _ in range(m):
u, v, w = map(int, input().split())
E[u-1].append((v-1, w))
E[v-1].append((u-1, w))
edges.append((u-1, v-1, w))
routes = []
for _ in range(k):
a, b = map(int, input().split())
routes.append((a-1, b-1))
def dijkstra(st):
pq, dist = [(0, st)], [inf if i!=st else 0 for i in range(n)]
while len(pq)>0:
d, u = heapq.heappop(pq)
if d!=dist[u]:
continue
for v, w in E[u]:
if dist[v]>dist[u]+w:
dist[v] = dist[u]+w
heapq.heappush(pq, (dist[v], v))
return dist
dist = []
for i in range(n):
dist.append(dijkstra(i))
ans = inf
for u, v, _ in edges:
now = 0
for a, b in routes:
now += min(dist[a][b], dist[a][u]+dist[b][v], dist[a][v]+dist[b][u])
ans = min(ans, now)
print(ans)
```
| 4,400 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Tags: brute force, graphs, shortest paths
Correct Solution:
```
import heapq
n, m, k = map(int, input().split())
E = [[] for _ in range(n)]
edges = []
for _ in range(m):
u, v, w = map(int, input().split())
E[u-1].append((v-1, w))
E[v-1].append((u-1, w))
edges.append((u-1, v-1, w))
routes = []
for _ in range(k):
a, b = map(int, input().split())
routes.append((a-1, b-1))
def dijkstra(st):
pq, dist = [(0, st)], [float("inf") if i!=st else 0 for i in range(n)]
while len(pq)>0:
d, u = heapq.heappop(pq)
if d!=dist[u]:
continue
for v, w in E[u]:
if dist[v]>dist[u]+w:
dist[v] = dist[u]+w
heapq.heappush(pq, (dist[v], v))
return dist
dist = []
for i in range(n):
dist.append(dijkstra(i))
ans = float("inf")
for u, v, _ in edges:
now = 0
for a, b in routes:
now += min(dist[a][b], dist[a][u]+dist[b][v], dist[a][v]+dist[b][u])
ans = min(ans, now)
print(ans)
```
| 4,401 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Tags: brute force, graphs, shortest paths
Correct Solution:
```
import sys
input = sys.stdin.readline
N, M, K = map(int, input().split())
e = [[] for _ in range(N + 1)]
for _ in range(M):
u, v, c = map(int, input().split())
e[u].append((v, c))
e[v].append((u, c))
qs = [tuple(map(int, input().split())) for _ in range(K)]
import heapq
hpush = heapq.heappush
hpop = heapq.heappop
class dijkstra:
def __init__(self, n, e):
self.e = e
self.n = n
def path(self, s):
d = [float("inf")] * (self.n + 1)
vis = [0] * (self.n + 1)
d[s] = 0
h = [s]
while len(h):
v = hpop(h)
v1 = v % (10 ** 4)
v0 = v // (10 ** 4)
if vis[v1]: continue
vis[v1] = 1
for p in self.e[v1]:
d[p[0]] = min(d[p[0]], d[v1] + p[1])
if vis[p[0]]: continue
hpush(h, d[p[0]] * (10 ** 4) + p[0])
return d
dij = dijkstra(N, e)
ps = [[]]
for x in range(1, N + 1): ps.append(dij.path(x))
t = [ps[qs[i][0]][qs[i][1]] for i in range(K)]
res = sum(t)
for x in range(1, N + 1): ps.append(dij.path(x))
for x in range(1, N + 1):
for y, c in e[x]:
tt = t[: ]
for k in range(K):
tt[k] = min(tt[k], ps[x][qs[k][0]] + ps[y][qs[k][1]], ps[y][qs[k][0]] + ps[x][qs[k][1]])
res = min(res, sum(tt))
print(res)
```
| 4,402 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Tags: brute force, graphs, shortest paths
Correct Solution:
```
def read_generator():
while True:
tokens = input().split(' ')
for t in tokens:
yield t
reader = read_generator()
def readword():
return next(reader)
def readint():
return int(next(reader))
def readfloat():
return float(next(reader))
def readline():
return input()
def solve(n):
return n
import heapq
def finddist(s, g, n):
d = [10 ** 10] * n
handled = [False] * n
d[s] = 0
heap = [(0, s)]
heapq.heapify(heap)
while len(heap) > 0:
(l, v) = heapq.heappop(heap)
if not handled[v]:
handled[v] = True
for (nextV, w) in g[v]:
if d[nextV] > d[v] + w:
d[nextV] = d[v] + w
heapq.heappush(heap, (d[nextV], nextV))
return d
tests = 1
for t in range(tests):
n = readint()
m = readint()
k = readint()
g = [[] for _ in range(n)]
e = []
for _ in range(m):
v1, v2, w = readint() - 1, readint() - 1, readint()
g[v1].append((v2, w))
g[v2].append((v1, w))
e.append((v1, v2))
p = [(readint() - 1, readint() - 1) for _ in range(k)]
d = [finddist(i, g, n) for i in range(n)]
res = 0
for (s1, s2) in p:
res += d[s1][s2]
for (v1, v2) in e:
s = 0
for (s1, s2) in p:
s += min(d[s1][s2], d[s1][v1] + d[v2][s2], d[s1][v2] + d[v1][s2])
res = min(res, s)
print(res)
```
| 4,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Tags: brute force, graphs, shortest paths
Correct Solution:
```
n, m, k = map(int, input().split())
inf = 1000001
g = [[] for _ in range(n)]
e = []
for i in range(m):
a, b, w = map(int, input().split())
a -= 1
b -= 1
g[a].append((b, w))
g[b].append((a, w))
e.append((a, b))
def f(s, g):
d = [inf] * n
d[s] = 0
q = [s]
i = 0
while i < len(q):
w, v = divmod(q[i], n)
i += 1
if d[v] != w:
continue
for u, w in g[v]:
if d[v] + w < d[u]:
d[u] = d[v] + w
q.append(u + n * d[u])
return d
p = [f(s, g) for s in range(n)]
t, s = 0, [0] * len(e)
for i in range(k):
x, y = map(int, input().split())
if x == y:
continue
x -= 1
y -= 1
t += p[x][y]
for i, (a, b) in enumerate(e):
d = min(p[x][a] + p[b][y], p[x][b] + p[a][y])
if p[x][y] > d:
s[i] += p[x][y] - d
print(t - max(s))
```
| 4,404 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Tags: brute force, graphs, shortest paths
Correct Solution:
```
from math import inf
import heapq
n,m,k = map(int,input().split())
G = [[] for _ in range(n)]
Q = []
for _ in range(m):
x,y,w = map(int,input().split())
G[x-1].append((y-1,w))
G[y-1].append((x-1,w))
for _ in range(k):
a,b = map(int,input().split())
Q.append((a-1,b-1))
def solve(x):
dist = [inf]*n
dist[x] = 0
q = [(0,x)]
while q:
d,x = heapq.heappop(q)
for y,w in G[x]:
if d+w < dist[y]:
dist[y] = d+w
heapq.heappush(q,(d+w,y))
return dist
D = [solve(x) for x in range(n)]
ans = inf
for x in range(n):
for y,w in G[x]:
s = 0
for a,b in Q:
v = min(D[a][x] + D[y][b], D[a][y] + D[x][b], D[a][b])
s += v
ans = min(s,ans)
print(ans)
```
| 4,405 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Tags: brute force, graphs, shortest paths
Correct Solution:
```
import heapq
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def dijkstra(source,neigh,dic):
n = len(neigh)
distance = [float('inf')]*n
visited = [False]*n
heap = []
heapq.heappush(heap,(0,source))
while heap:
temp = heapq.heappop(heap)
# print(temp)
(d,index) = (temp[0],temp[1])
if distance[index] <= d: continue
distance[index] = d
for ele in neigh[index]:
if distance[ele] > distance[index] + dic[(ele,index)]:
heapq.heappush(heap,(distance[index] + dic[(ele,index)], ele))
return distance
n,m,k = map(int,input().split())
neigh = [[] for i in range(n)]
start = [0]*(k)
end = [0]*k
dic = {}
for i in range(m):
path = list(map(int,input().split()))
neigh[path[0]-1].append(path[1]-1)
neigh[path[1]-1].append(path[0]-1)
dic[(path[0]-1,path[1]-1)] = path[2]
dic[(path[1]-1,path[0]-1)] = path[2]
for i in range(k):
temp1, temp2 = map(int,input().split())
start[i] = temp1 - 1
end[i] = temp2 - 1
#if n==1000 and m==1000 and k==1000:
# print("5625644")
# exit(0)
matrix = []
for i in range(n):
matrix.append(dijkstra(i,neigh,dic))
"""
for inter in range(n):
for i in range(n):
for j in range(i+1,n):
matrix[i][j] = min(matrix[i][j], matrix[i][inter]+matrix[inter][j])
matrix[j][i] = matrix[i][j]
matrix[i][i] = 0
"""
#print(matrix)
ans = 2147483647
for ele in dic:
tot = 0
for i in range(k):
tot += min( matrix[start[i]][end[i]], matrix[start[i]][ele[0]]+matrix[ele[1]][end[i]], matrix[start[i]][ele[1]]+matrix[ele[0]][end[i]])
ans = min(ans,tot)
print(ans)
```
| 4,406 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import defaultdict
from math import inf
from heapq import heappop,heappush
def dijkstra(start):
heap,dist=[(0,start)],[inf]*(n+1)
dist[start]=0
while heap:
d,vertex=heappop(heap)
for child,weight in graph[vertex]:
weight+=d
if dist[child]>weight:
dist[child]=weight
heappush(heap,(weight,child))
return(dist)
n,m,k=map(int,input().split())
graph = [[] for _ in range(n + 1)]
weights=defaultdict(int)
connectlist=[]
for i in range(m):
a,b,w=map(int,input().split())
graph[a].append((b,w))
graph[b].append((a,w))
connectlist.append((a,b))
route=[]
distlist=[]
for i in range(n):
distlist.append(dijkstra(i+1))
for i in range(k):
src,dst=map(int,input().split())
route.append((src,dst))
ans=10**10
for i in connectlist:
tmp=0
for j in route:
tmp+=min(distlist[j[0]-1][j[1]],distlist[j[0]-1][i[0]]+distlist[j[1]-1][i[1]],distlist[j[0]-1][i[1]]+distlist[j[1]-1][i[0]])
if tmp<ans:
ans=tmp
print(ans)
```
Yes
| 4,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Submitted Solution:
```
from sys import stdin, stdout
input, print = stdin.readline, stdout.write
import heapq
(n, m, k) = map(int, input().split())
edgeList = []
edges = [[] for _ in range(n)]
for _ in range(m):
edge = tuple(map(int, input().split()))
edge = (edge[0]-1, edge[1]-1, edge[2])
edgeList.append(edge)
edges[edge[0]].append((edge[1], edge[2]))
edges[edge[1]].append((edge[0], edge[2]))
routes = []
for _ in range(k):
route = list(map(int, input().split()))
routes.append((route[0]-1, route[1]-1))
# Populate APSP (all-pairs shortest paths) by repeating Dijkstra N times.
minCosts = [[float('inf')] * n for _ in range(n)]
for i in range(n):
minCosts[i][i] = 0
pq = [(0, i)]
visited = [False] * n
while pq:
cost, u = heapq.heappop(pq)
if visited[u]:
continue
visited[u] = True
for (v, w) in edges[u]:
if minCosts[i][v] > cost+w:
minCosts[i][v] = cost+w
heapq.heappush(pq, (cost+w, v))
# Consider each edge to set to 0.
minTotal = float('inf')
for edge in edgeList:
total = 0
for route in routes:
total += min(
minCosts[route[0]][route[1]],
minCosts[route[0]][edge[0]] + minCosts[edge[1]][route[1]],
minCosts[route[0]][edge[1]] + minCosts[edge[0]][route[1]])
minTotal = min(total, minTotal)
print(str(minTotal))
```
Yes
| 4,408 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
from heapq import heappop, heappush
def main():
n, m, k = getints()
adj = [[] for _ in range(n)]
dist = [[int(1e9)] * n for _ in range(n)]
edges = []
route = []
for _ in range(m):
u, v, w = getint1()
w += 1
adj[u].append((v, w))
adj[v].append((u, w))
edges.append((u, v))
for _ in range(k):
a, b = getint1()
route.append((a, b))
for s in range(n):
dist[s][s] = 0
pq = [(0, s)]
while pq:
d, u = heappop(pq)
if d != dist[s][u]:
continue
for v, w in adj[u]:
nd = dist[s][u] + w
if dist[s][v] > nd:
dist[s][v] = nd
heappush(pq, (nd, v))
ans = int(1e9)
for u, v in edges:
cost = 0
for a, b in route:
cost += min(dist[a][b], dist[a][u] + dist[v][b], dist[a][v] + dist[u][b])
ans = min(ans, cost)
print(ans)
# 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)
def input(): return sys.stdin.readline().rstrip("\r\n")
def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint1(): return list(map(lambda x: int(x) - 1, input().split()))
# endregion
if __name__ == "__main__":
main()
```
Yes
| 4,409 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
from heapq import *
def short(n,path,st):
visi = [0]*n
dist = [10**20]*n
dist[st] = 0
he = [(0,st)]
heapify(he)
while len(he):
x = heappop(he)[1]
if visi[x]:
continue
visi[x] = 1
for yy in path[x]:
i,j = yy
if visi[i]:
continue
if dist[i] > dist[x]+j:
dist[i] = dist[x]+j
heappush(he,(dist[i],i))
return dist
def main():
n,m,k = map(int,input().split())
path = [[] for _ in range(n)]
edg = []
for _ in range(m):
u1,v1,c1 = map(int,input().split())
edg.append((u1-1,v1-1,c1))
path[u1-1].append((v1-1,c1))
path[v1-1].append((u1-1,c1))
dist = [short(n,path,i) for i in range(n)]
route = [tuple(map(lambda xx:int(xx)-1,input().split())) for _ in range(k)]
ans = 0
for i in route:
ans += dist[i[0]][i[1]]
for i in edg:
ans1 = 0
for j in route:
ans1 += min(dist[j[0]][i[0]]+dist[i[1]][j[1]],
dist[j[0]][i[1]]+dist[i[0]][j[1]],
dist[j[0]][j[1]])
ans = min(ans,ans1)
print(ans)
# Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
```
Yes
| 4,410 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Submitted Solution:
```
print("bye bye")
```
No
| 4,411 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Submitted Solution:
```
P=1000000007
A=[1]
B=[0]
n,k,d=map(int,input().split())
for i in range(n):
S1 = 0
S2 = 0
for j in range(k):
if j<=i:
S1= (S1+A[i-j])%P
if (j>=d-1):
S2= (S2+A[i-j])%P
else:
S2= (S2+B[i-j])%P
A.append(S1)
B.append(S2)
print (B[n])
```
No
| 4,412 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Submitted Solution:
```
import sys
input = sys.stdin.readline
from collections import defaultdict
from math import inf
from heapq import heappop,heappush
def dijkstra(start):
heap,dist=[(0,start)],[inf]*(n+1)
dist[start]=0
while heap:
d,vertex=heappop(heap)
for child,weight in graph[vertex]:
weight+=d
if dist[child]>weight:
dist[child]=weight
heappush(heap,(weight,child))
return(dist)
n,m,k=map(int,input().split())
graph = [[] for _ in range(n + 1)]
weights=defaultdict(int)
connectlist=[]
for i in range(m):
a,b,w=map(int,input().split())
graph[a].append((b,w))
graph[b].append((a,w))
connectlist.append((a,b))
route=[]
distlist=[]
for i in range(n):
distlist.append(dijkstra(i+1))
for i in range(k):
src,dst=map(int,input().split())
route.append((src,dst))
ans=10**10
for i in connectlist:
tmp=0
for j in route:
tmp+=min(distlist[j[0]-1][j[1]],distlist[j[0]-1][i[0]]+distlist[j[1]-1][i[1]],distlist[j[0]-1][i[1]]+distlist[j[1]-1][i[1]])
if tmp<ans:
ans=tmp
print(ans)
```
No
| 4,413 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are a mayor of Berlyatov. There are n districts and m two-way roads between them. The i-th road connects districts x_i and y_i. The cost of travelling along this road is w_i. There is some path between each pair of districts, so the city is connected.
There are k delivery routes in Berlyatov. The i-th route is going from the district a_i to the district b_i. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district a_i to the district b_i to deliver products.
The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with 0).
Let d(x, y) be the cheapest cost of travel between districts x and y.
Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with 0. In other words, you have to find the minimum possible value of β_{i = 1}^{k} d(a_i, b_i) after applying the operation described above optimally.
Input
The first line of the input contains three integers n, m and k (2 β€ n β€ 1000; n - 1 β€ m β€ min(1000, (n(n-1))/(2)); 1 β€ k β€ 1000) β the number of districts, the number of roads and the number of courier routes.
The next m lines describe roads. The i-th road is given as three integers x_i, y_i and w_i (1 β€ x_i, y_i β€ n; x_i β y_i; 1 β€ w_i β€ 1000), where x_i and y_i are districts the i-th road connects and w_i is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.
The next k lines describe courier routes. The i-th route is given as two integers a_i and b_i (1 β€ a_i, b_i β€ n) β the districts of the i-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
Output
Print one integer β the minimum total courier routes cost you can achieve (i.e. the minimum value β_{i=1}^{k} d(a_i, b_i), where d(x, y) is the cheapest cost of travel between districts x and y) if you can make some (at most one) road cost zero.
Examples
Input
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3
Output
22
Input
5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5
Output
13
Note
The picture corresponding to the first example:
<image>
There, you can choose either the road (2, 4) or the road (4, 6). Both options lead to the total cost 22.
The picture corresponding to the second example:
<image>
There, you can choose the road (3, 4). This leads to the total cost 13.
Submitted Solution:
```
from sys import stdin
input = stdin.readline
from heapq import heappush, heappop
n, m, k = map(int, input().split())
# edge = [tuple(map(int, input().split())) for _ in range(m)]
# courier = [tuple(map(int, input().split())) for _ in range(k)]
r = [tuple(map(int,inp.split())) for inp in stdin.read().splitlines()]
adj = [[] for _ in range(n+1)]
for u, v, w in r[:m]:
adj[u].append([v, w])
adj[v].append([u, w])
INF = 10 ** 7
dis = [[INF] * (n + 1) for _ in range(n + 1)]
# for i in range(1,n+1):
# dis[i][i] = 0
for s in range(1, n + 1):
dis[s][s] = 0
h = [(0, s)]
while h:
curd, cur = heappop(h)
if curd > dis[s][cur]:
continue
for nxt, w in adj[cur]:
nd = curd + w
if nd < dis[s][nxt]:
dis[s][nxt] = nd
heappush(h, (nd, nxt))
res = INF
for s, e, _ in r[:m]:
temp = 0
for cs, ce in r[m:]:
temp += min(dis[cs][ce], dis[cs][s] + dis[ce][e], dis[cs][e] + dis[ce][s])
res = min(res,temp)
print(res)
```
No
| 4,414 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a tree with n vertices numbered 1, β¦, n. A tree is a connected simple graph without cycles.
Let dist(u, v) be the number of edges in the unique simple path connecting vertices u and v.
Let diam(l, r) = max dist(u, v) over all pairs u, v such that l β€ u, v β€ r.
Compute β_{1 β€ l β€ r β€ n} diam(l, r).
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of vertices in the tree.
The next n - 1 lines describe the tree edges. Each of these lines contains two integers u, v (1 β€ u, v β€ n) β endpoint indices of the respective tree edge. It is guaranteed that the edge list indeed describes a tree.
Output
Print a single integer β β_{1 β€ l β€ r β€ n} diam(l, r).
Examples
Input
4
1 2
2 4
3 2
Output
10
Input
10
1 8
2 9
5 6
4 8
4 2
7 9
3 6
10 4
3 9
Output
224
Submitted Solution:
```
a = input()
a = list(a)
b = input()
b = list(b)
l = []
for i in range(len(a)):
if a[i] != b[i]: l.append('1')
else: l.append('0')
print(''.join(l))
```
No
| 4,415 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kilani and Abd are neighbors for 3000 years, but then the day came and Kilani decided to move to another house. As a farewell gift, Kilani is going to challenge Abd with a problem written by their other neighbor with the same name Abd.
<image>
The problem is:
You are given a connected tree rooted at node 1.
You should assign a character a or b to every node in the tree so that the total number of a's is equal to x and the total number of b's is equal to n - x.
Let's define a string for each node v of the tree as follows:
* if v is root then the string is just one character assigned to v:
* otherwise, let's take a string defined for the v's parent p_v and add to the end of it a character assigned to v.
You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes.
Input
The first line contains two integers n and x (1 β€ n β€ 10^5; 0 β€ x β€ n) β the number of vertices in the tree the number of a's.
The second line contains n - 1 integers p_2, p_3, ..., p_{n} (1 β€ p_i β€ n; p_i β i), where p_i is the parent of node i.
It is guaranteed that the input describes a connected tree.
Output
In the first line, print the minimum possible total number of distinct strings.
In the second line, print n characters, where all characters are either a or b and the i-th character is the character assigned to the i-th node.
Make sure that the total number of a's is equal to x and the total number of b's is equal to n - x.
If there is more than one answer you can print any of them.
Example
Input
9 3
1 2 2 4 4 4 3 1
Output
4
aabbbbbba
Note
The tree from the sample is shown below:
<image>
The tree after assigning characters to every node (according to the output) is the following:
<image>
Strings for all nodes are the following:
* string of node 1 is: a
* string of node 2 is: aa
* string of node 3 is: aab
* string of node 4 is: aab
* string of node 5 is: aabb
* string of node 6 is: aabb
* string of node 7 is: aabb
* string of node 8 is: aabb
* string of node 9 is: aa
The set of unique strings is \{a, aa, aab, aabb\}, so the number of distinct strings is 4.
Submitted Solution:
```
line_1=input()
line_2=input()
line_1=line_1.split(' ')
num=int(line_1[1])
line_2=line_2.split(' ')
ans=0
count=0
a_list=[]
for x in line_2:
count+=1
if int(x)<int(num):
if int(num)+1>count:
a_list.append('a')
else:
a_list.append('b')
ans+=1
else:
a_list.append('b')
print(ans)
for g in a_list:
print(g,end='')
```
No
| 4,416 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kilani and Abd are neighbors for 3000 years, but then the day came and Kilani decided to move to another house. As a farewell gift, Kilani is going to challenge Abd with a problem written by their other neighbor with the same name Abd.
<image>
The problem is:
You are given a connected tree rooted at node 1.
You should assign a character a or b to every node in the tree so that the total number of a's is equal to x and the total number of b's is equal to n - x.
Let's define a string for each node v of the tree as follows:
* if v is root then the string is just one character assigned to v:
* otherwise, let's take a string defined for the v's parent p_v and add to the end of it a character assigned to v.
You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes.
Input
The first line contains two integers n and x (1 β€ n β€ 10^5; 0 β€ x β€ n) β the number of vertices in the tree the number of a's.
The second line contains n - 1 integers p_2, p_3, ..., p_{n} (1 β€ p_i β€ n; p_i β i), where p_i is the parent of node i.
It is guaranteed that the input describes a connected tree.
Output
In the first line, print the minimum possible total number of distinct strings.
In the second line, print n characters, where all characters are either a or b and the i-th character is the character assigned to the i-th node.
Make sure that the total number of a's is equal to x and the total number of b's is equal to n - x.
If there is more than one answer you can print any of them.
Example
Input
9 3
1 2 2 4 4 4 3 1
Output
4
aabbbbbba
Note
The tree from the sample is shown below:
<image>
The tree after assigning characters to every node (according to the output) is the following:
<image>
Strings for all nodes are the following:
* string of node 1 is: a
* string of node 2 is: aa
* string of node 3 is: aab
* string of node 4 is: aab
* string of node 5 is: aabb
* string of node 6 is: aabb
* string of node 7 is: aabb
* string of node 8 is: aabb
* string of node 9 is: aa
The set of unique strings is \{a, aa, aab, aabb\}, so the number of distinct strings is 4.
Submitted Solution:
```
from sys import stdin, stdout
meta = stdin.readline()
tree = stdin.readline()
for i in tree:
print(tree)
```
No
| 4,417 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kilani and Abd are neighbors for 3000 years, but then the day came and Kilani decided to move to another house. As a farewell gift, Kilani is going to challenge Abd with a problem written by their other neighbor with the same name Abd.
<image>
The problem is:
You are given a connected tree rooted at node 1.
You should assign a character a or b to every node in the tree so that the total number of a's is equal to x and the total number of b's is equal to n - x.
Let's define a string for each node v of the tree as follows:
* if v is root then the string is just one character assigned to v:
* otherwise, let's take a string defined for the v's parent p_v and add to the end of it a character assigned to v.
You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes.
Input
The first line contains two integers n and x (1 β€ n β€ 10^5; 0 β€ x β€ n) β the number of vertices in the tree the number of a's.
The second line contains n - 1 integers p_2, p_3, ..., p_{n} (1 β€ p_i β€ n; p_i β i), where p_i is the parent of node i.
It is guaranteed that the input describes a connected tree.
Output
In the first line, print the minimum possible total number of distinct strings.
In the second line, print n characters, where all characters are either a or b and the i-th character is the character assigned to the i-th node.
Make sure that the total number of a's is equal to x and the total number of b's is equal to n - x.
If there is more than one answer you can print any of them.
Example
Input
9 3
1 2 2 4 4 4 3 1
Output
4
aabbbbbba
Note
The tree from the sample is shown below:
<image>
The tree after assigning characters to every node (according to the output) is the following:
<image>
Strings for all nodes are the following:
* string of node 1 is: a
* string of node 2 is: aa
* string of node 3 is: aab
* string of node 4 is: aab
* string of node 5 is: aabb
* string of node 6 is: aabb
* string of node 7 is: aabb
* string of node 8 is: aabb
* string of node 9 is: aa
The set of unique strings is \{a, aa, aab, aabb\}, so the number of distinct strings is 4.
Submitted Solution:
```
def dfs(root, tree_list, cur_level, levels):
for leaf in tree_list[root]:
if len(levels)<=cur_level+1:
levels.append([])
levels[cur_level+1].append(leaf)
levels = dfs(leaf, tree_list, cur_level+1, levels)
return levels
def solve(n, x, original_tree):
tree = [[] for i in range(len(original_tree)+1)]
for i in range(len(original_tree)):
tree[original_tree[i]-1].append(i+1)
levels = dfs(0, tree, 0, [[0]])
level_num = [len(i) for i in levels]
a, b = x, n-x
sol = ['' for i in range(n)]
flag = False
for index, num in enumerate(level_num):
if a >= num:
a -= num
for node in levels[index]:
sol[node] = 'a'
elif b >= num:
b -= num
for node in levels[index]:
sol[node] = 'b'
else:
for node in levels[index]:
if a > 0:
a-=1
sol[node] = 'a'
flag = True
else:
sol[node] = 'b'
ans_num = len(level_num)
if flag:
ans_num+=1
return ans_num, ''.join(sol)
q_n, q_x = map(int, input().split())
q_tree = list(map(int,input().split()))
ans_num, solution = solve(q_n, q_x, q_tree)
print(ans_num)
print(solution)
```
No
| 4,418 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Kilani and Abd are neighbors for 3000 years, but then the day came and Kilani decided to move to another house. As a farewell gift, Kilani is going to challenge Abd with a problem written by their other neighbor with the same name Abd.
<image>
The problem is:
You are given a connected tree rooted at node 1.
You should assign a character a or b to every node in the tree so that the total number of a's is equal to x and the total number of b's is equal to n - x.
Let's define a string for each node v of the tree as follows:
* if v is root then the string is just one character assigned to v:
* otherwise, let's take a string defined for the v's parent p_v and add to the end of it a character assigned to v.
You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes.
Input
The first line contains two integers n and x (1 β€ n β€ 10^5; 0 β€ x β€ n) β the number of vertices in the tree the number of a's.
The second line contains n - 1 integers p_2, p_3, ..., p_{n} (1 β€ p_i β€ n; p_i β i), where p_i is the parent of node i.
It is guaranteed that the input describes a connected tree.
Output
In the first line, print the minimum possible total number of distinct strings.
In the second line, print n characters, where all characters are either a or b and the i-th character is the character assigned to the i-th node.
Make sure that the total number of a's is equal to x and the total number of b's is equal to n - x.
If there is more than one answer you can print any of them.
Example
Input
9 3
1 2 2 4 4 4 3 1
Output
4
aabbbbbba
Note
The tree from the sample is shown below:
<image>
The tree after assigning characters to every node (according to the output) is the following:
<image>
Strings for all nodes are the following:
* string of node 1 is: a
* string of node 2 is: aa
* string of node 3 is: aab
* string of node 4 is: aab
* string of node 5 is: aabb
* string of node 6 is: aabb
* string of node 7 is: aabb
* string of node 8 is: aabb
* string of node 9 is: aa
The set of unique strings is \{a, aa, aab, aabb\}, so the number of distinct strings is 4.
Submitted Solution:
```
print("hello")
```
No
| 4,419 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Tags: constructive algorithms
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
a=[]
for i in range(n):
if(l[i]%2!=0):
a.append(l[i])
for i in range(n):
if (l[i] % 2 == 0):
a.append(l[i])
print(*a ,end=" ")
```
| 4,420 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Tags: constructive algorithms
Correct Solution:
```
import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
n = input()
O = []
E = []
A = map(int, input().split())
for a in A:
if a % 2 == 0:
E.append(a)
else:
O.append(a)
ans = O + E
print(*ans)
```
| 4,421 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Tags: constructive algorithms
Correct Solution:
```
for i in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
odd=[str(i) for i in l if i%2!=0]
even =[str(i) for i in l if i%2==0]
x=odd+even
print(" ".join(x))
```
| 4,422 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Tags: constructive algorithms
Correct Solution:
```
T = int(input())
for t in range(T):
N = int(input())
arr = list(map(int, input().split()))
ans = []
for i in arr:
if i%2:
ans.append(i)
for i in arr:
if i%2 == 0:
ans.append(i)
print(" ".join(list(map(str,ans))))
```
| 4,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Tags: constructive algorithms
Correct Solution:
```
n=int(input())
for i in range(n):
a=int(input())
l=list(map(int,input().split()))
e=[]
o=[]
for i in range(len(l)):
if(l[i]%2==0):
e.append(l[i])
else:
o.append(l[i])
print(*(o+e))
```
| 4,424 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Tags: constructive algorithms
Correct Solution:
```
n = int(input())
while n:
input()
nums = []
for i in map(int, input().split(' ')):
if i % 2:
nums += [i]
else:
nums = [i] + nums
print(' '.join(map(str, nums)))
n -= 1
```
| 4,425 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Tags: constructive algorithms
Correct Solution:
```
# Programmers_Hive
import os
import sys
from io import BytesIO, IOBase
def main():
# main code
t=int(input())
for x in range(t):
n=int(input())
l=list(map(int,input().split()))
even=[]
odd=[]
for i in l:
if i%2==0:
even+=[i]
else:
odd+=[i]
even.sort()
odd.sort()
for i in range(len(odd)-1,-1,-1):
print(odd[i],end=" ")
for i in range(len(even)-1,-1,-1):
print(even[i],end=" ")
print()
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
```
| 4,426 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Tags: constructive algorithms
Correct Solution:
```
t = int(input())
while t:
n = int(input())
s = input().strip().split()
s = [int(s[i]) for i in range(len(s))]
odd = [s[i] for i in range(len(s)) if s[i] & 1 == 1 ]
even = [s[i] for i in range(len(s)) if s[i] & 1 == 0]
odd.sort(reverse = True)
even.sort()
for i in range(len(odd)):
print(odd[i], end = " ")
for i in range(len(even)):
print(even[i], end = " ")
print()
t = t - 1
```
| 4,427 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
arr = [int(j) for j in input().split()]
ans = []
for x in arr:
if x%2 == 0:
ans += [x]
for x in arr:
if x%2 == 1:
ans += [x]
print(*ans)
```
Yes
| 4,428 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Submitted Solution:
```
def AvgHeight(n,l):
a = [0 for i in range(n)]
j = 0
x = n-1
for i in range(n):
if j>x:
break
elif(l[i]%2!=0):
a[j] = l[i]
j += 1
else:
a[x] = l[i]
x -= 1
for i in range(n):
print(a[i],end=" ")
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int,input().split()))
AvgHeight(n,l)
```
Yes
| 4,429 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Submitted Solution:
```
for _ in range(int(input())):
a=int(input())
l=list(map(int,input().split()))
l1,l2=[],[]
for i in l:
if i%2==1:
l1.append(i)
else:
l2.append(i)
print(*(l1+l2))
```
Yes
| 4,430 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Submitted Solution:
```
import sys,math,bisect
inf = float('inf')
input = sys.stdin.readline
mod = (10**9)+7
def lcm(a,b):
return int((a/math.gcd(a,b))*b)
def gcd(a,b):
return int(math.gcd(a,b))
def binarySearch(a,x):
i = bisect.bisect_left(a,x)
if i!=len(a) and a[i]==x:
return i
else:
return -1
def lowerBound(a, x):
i = bisect.bisect_left(a, x)
if i:
return (i-1)
else:
return -1
def upperBound(a,x):
i = bisect.bisect_right(a,x)
if i!= len(a)+1 and a[i-1]==x:
return (i-1)
else:
return -1
def freq(a,x):
z = upperBound(a,x) - lowerBound(a,x)
if z<=0:
return 0
return z
"""
n = int(input())
n,k = map(int,input().split())
arr = list(map(int,input().split()))
"""
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
even = []
odd = []
ans = []
for i in arr:
if i%2==0:
even.append(i)
else:
odd.append(i)
for i in even:
ans.append(i)
for i in odd:
ans.append(i)
print(*ans)
```
Yes
| 4,431 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Submitted Solution:
```
for T in range(int(input())):
# one number
n = int(input())
# SPACED items "1 2 3" or dataset "1 2 3 4 5 ..." NEED TO BE SPLIT
d = [int(p) for p in input().split()]
evens = []
odds = []
for i in d:
if i % 2 == 0:
evens.append(i)
else:
odds.append(i)
evens.extend(odds)
print (evens)
```
No
| 4,432 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Submitted Solution:
```
for i in range(int(input())):
n=int(input())
h=list(map(int,input().split()))
e=[]
o=[]
for i in h:
if i%2==0:
e.append(i)
else:
o.append(i)
print(e+o)
```
No
| 4,433 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
l=list(map(int,input().split()))
odd=[]
for i in range(0,len(l)-1):
if l[i]%2==0:
print(l[i],end=' ')
else:
odd.append(l[i])
for i in range(0,len(odd)-1):
print(odd[i],end=' ')
print()
```
No
| 4,434 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sayaka Saeki is a member of the student council, which has n other members (excluding Sayaka). The i-th member has a height of a_i millimeters.
It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.
A pair of two consecutive members u and v on a line is considered photogenic if their average height is an integer, i.e. (a_u + a_v)/(2) is an integer.
Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.
Input
The first line contains a single integer t (1β€ tβ€ 500) β the number of test cases.
The first line of each test case contains a single integer n (2 β€ n β€ 2000) β the number of other council members.
The second line of each test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 2 β
10^5) β the heights of each of the other members in millimeters.
It is guaranteed that the sum of n over all test cases does not exceed 2000.
Output
For each test case, output on one line n integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.
Example
Input
4
3
1 1 2
3
1 1 1
8
10 9 13 15 3 16 9 13
2
18 9
Output
1 1 2
1 1 1
13 9 13 15 3 9 16 10
9 18
Note
In the first test case, there is one photogenic pair: (1, 1) is photogenic, as (1+1)/(2)=1 is integer, while (1, 2) isn't, as (1+2)/(2)=1.5 isn't integer.
In the second test case, both pairs are photogenic.
Submitted Solution:
```
from sys import stdin,stdout
def main():
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
e = []
o = []
for i in list(map(int,input().split())):
if i%2 == 0:
e.append(i)
else:
o.append(i)
arr = o + e
for x in arr:
stdout.write(str(x)+' ')
stdout.write('\n')
if __name__=='__main__':
main()
```
No
| 4,435 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Tags: brute force, implementation
Correct Solution:
```
t=int(input())
for i in range(t):
a=[int(c) for c in input().split()]
if max(a[1],a[0])<min(a[3],a[2]):
print("NO")
elif min(a[1],a[0])>max(a[3],a[2]):
print("NO")
else:
print("YES")
```
| 4,436 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Tags: brute force, implementation
Correct Solution:
```
for _ in range(int(input())):
m,n,o,p=map(int,input().split())
a=max(m,n)
b=min(m,n)
c=max(o,p)
d=min(o,p)
if(c>b and a>d):
print("YES")
else:
print("NO")
```
| 4,437 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Tags: brute force, implementation
Correct Solution:
```
for _ in range(int(input())):
l=list(map(int,input().split()))
m=max(l)
i=l.index(m)
l[i]=-1
m2=max(l)
j=l.index(m2)
if (i+j==1) or (i+j==5):
print("NO")
else:
print("YES")
```
| 4,438 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Tags: brute force, implementation
Correct Solution:
```
for _ in range(int(input())):
si=list(map(int,input().split()))
ai=sorted(si)
if max(si[0],si[1])==ai[-1] and max(si[2],si[3])==ai[-2]:print("YES")
elif max(si[0],si[1])==ai[-2] and max(si[2],si[3])==ai[-1]:print("YES")
else:print("NO")
```
| 4,439 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Tags: brute force, implementation
Correct Solution:
```
t = int(input())
while t>0:
a = list(map(int,input().split()))
b = [max(a[0],a[1]),max(a[2],a[3])]
a.sort()
b.sort()
a = a[2:]
if a==b:
print("YES")
else:
print("NO")
t = t-1
```
| 4,440 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Tags: brute force, implementation
Correct Solution:
```
t=int(input())
for _ in range(t):
a=list(map(int, input().split()))
b=a.copy()
a.sort(reverse=True)
c=[]
c.append(max(b[0], b[1]))
c.append(max(b[2], b[3]))
c.sort(reverse=True)
if(c==a[0:2]):
print('YES')
else:
print('NO')
```
| 4,441 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Tags: brute force, implementation
Correct Solution:
```
def Fair(arr):
maximum_a = max(arr)
newa = list(arr)
player_list = [max(arr[0],arr[1]),max(arr[2],arr[3])]
for i in range(len(newa)):
if newa[i] == maximum_a:
del newa[i]
break
maximum_b = max(newa)
if maximum_a in player_list:
if maximum_b in player_list:
return 'YES'
else:
return 'NO'
else:
return 'NO'
t = int(input().strip())
for i in range(t):
object_a = input().split(' ')
for i in range(len(object_a)):
object_a[i] = int(object_a[i])
print(Fair(object_a))
```
| 4,442 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Tags: brute force, implementation
Correct Solution:
```
t = int(input())
for i in range(t):
a,b,c,d = map(int,input().split())
Y = [a,b,c,d]
Y.sort(reverse=True)
if (max(a,b) == Y[0] or max(a,b) == Y[1]) and (max(c,d) == Y[0] or max(c,d) == Y[1]):
print("YES")
else:
print("NO")
```
| 4,443 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Submitted Solution:
```
t=int(input())
while(t):
t=t-1
a=list(map(int,input().split()))
b=sorted(a)
maxx=b[-1]
maxx2=b[-2]
if(maxx in a[0:2]):
if(maxx2 in a[2:4]):
print("YES")
else:
print("NO")
elif(maxx2 in a[0:2]):
if(maxx in a[2:4]):
print("YES")
else:
print("NO")
else:
print("NO")
```
Yes
| 4,444 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Submitted Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
a,b,c,d=map(int,input().split())
if min(a,b)>max(c,d) or min(c,d)>max(a,b):
print("NO")
else:
print("YES")
```
Yes
| 4,445 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Submitted Solution:
```
# cook your dish here
for i in range(int(input())):
a=list(map(int,input().split()))
temp=a[:]
a.sort()
m1=a[3]
m2=a[2]
b=[]
b.append(m1)
b.append(m2)
b.sort()
c=[]
num1=max(temp[0],temp[1])
num2=max(temp[3],temp[2])
c.append(num1)
c.append(num2)
c.sort()
if(b==c):
print('YES')
else:
print('NO')
```
Yes
| 4,446 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Submitted Solution:
```
t=int(input())
for _ in range(t):
a,b,c,d=map(int,input().split())
x1 = max(a,b)
x2 = max(c,d)
if x1>min(c,d) and x2>min(a,b):
print("YES")
else:
print("NO")
```
Yes
| 4,447 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Submitted Solution:
```
for _ in range(int(input())):
a, b, c, d = map(int, input().split())
if(max(a,b) < min(c, d)):
print("YES")
else:
print("NO")
```
No
| 4,448 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Submitted Solution:
```
t=int(input())
for i in range(t):
p1,p2,p3,p4=map(int,input().split())
f1=max(p1,p2)
f2=max(p3,p4)
f3=max(p1,p3)
f4=max(p2,p4)
if f1!=f3 and f1!=f4:
print("No")
break
if f2!=f3 and f2!=f4:
print("No")
break
print("Yes")
```
No
| 4,449 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Submitted Solution:
```
t=int(input())
for i in range(t):
l=list(map(int,input().split()))
s=sorted(l)
p1,p2=s[-1],s[-2]
m1=[]
c=l[0]
for i in range(1,len(l)):
m1.append(max(c,l[i]))
c=l[i]
if(m1[-1]==p1 and m1[0]==p2):
print('YES')
else:
print('NO')
```
No
| 4,450 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to s_i and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
The tournament is called fair if the two players with the highest skills meet in the finals.
Determine whether the given tournament is fair.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
A single line of test case contains four integers s_1, s_2, s_3, s_4 (1 β€ s_i β€ 100) β skill of the players. It is guaranteed that all the numbers in the array are different.
Output
For each testcase, output YES if the tournament is fair, or NO otherwise.
Example
Input
4
3 7 9 5
4 5 6 9
5 3 8 1
6 5 3 2
Output
YES
NO
YES
NO
Note
Consider the example:
1. in the first test case, players 2 and 3 with skills 7 and 9 advance to the finals;
2. in the second test case, players 2 and 4 with skills 5 and 9 advance to the finals. The player with skill 6 does not advance, but the player with skill 5 advances to the finals, so the tournament is not fair;
3. in the third test case, players 1 and 3 with skills 5 and 8 advance to the finals;
4. in the fourth test case, players 1 and 3 with skills 6 and 3 advance to the finals. The player with skill 5 does not advance, but the player with skill 3 advances to the finals, so the tournament is not fair.
Submitted Solution:
```
import sys
n = int(input())
for line in sys.stdin:
s1, s2, s3, s4 = line.split()
t = [s1,s2,s3,s4]
t.sort(reverse = True)
t = t[:2]
t1 , t2 = max(s1,s2), max(s3,s4)
test = [t1,t2]
test.sort(reverse = True)
if test == t:
print("YES")
else:
print("NO")
```
No
| 4,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Tags: binary search, combinatorics, dp
Correct Solution:
```
######### ## ## ## #### ##### ## # ## # ##
# # # # # # # # # # # # # # # # # # #
# # # # ### # # # # # # # # # # # #
# ##### # # # # ### # # # # # # # # #####
# # # # # # # # # # # # # # # # # #
######### # # # # ##### # ##### # ## # ## # #
"""
PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE
PPPPPPPP RRRRRRRR OOOOOO VV VV EE
PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE
PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE
PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE
PP RRRR OOOOOOOO VV VV EEEEEE
PP RR RR OOOOOOOO VV VV EE
PP RR RR OOOOOO VV VV EE
PP RR RR OOOO VVVV EEEEEEEEEE
"""
"""
Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away.
"""
import sys
input = sys.stdin.readline
read = lambda: map(int, input().split())
# from bisect import bisect_left as lower_bound;
# from bisect import bisect_right as upper_bound;
# from math import ceil, factorial;
def ceil(x):
if x != int(x):
x = int(x) + 1
return x
def factorial(x, m):
val = 1
while x>0:
val = (val * x) % m
x -= 1
return val
def fact(x):
val = 1
while x > 0:
val *= x
x -= 1
return val
# swap_array function
def swaparr(arr, a,b):
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
## gcd function
def gcd(a,b):
if b == 0:
return a;
return gcd(b, a % b);
## lcm function
def lcm(a, b):
return (a * b) // gcd(a, b)
## nCr function efficient using Binomial Cofficient
def nCr(n, k):
if k > n:
return 0
if(k > n - k):
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0, hi = None):
if hi == None:
hi = len(a);
while lo < hi:
mid = (lo+hi)//2;
if a[mid] < x:
lo = mid+1;
else:
hi = mid;
return lo;
## prime factorization
def primefs(n):
## if n == 1 ## calculating primes
primes = {}
while(n%2 == 0 and n > 0):
primes[2] = primes.get(2, 0) + 1
n = n//2
for i in range(3, int(n**0.5)+2, 2):
while(n%i == 0 and n > 0):
primes[i] = primes.get(i, 0) + 1
n = n//i
if n > 2:
primes[n] = primes.get(n, 0) + 1
## prime factoriazation of n is stored in dictionary
## primes and can be accesed. O(sqrt n)
return primes
## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p):
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
temp = a
a = b
b = temp
return a,b;
# find function with path compression included (recursive)
# def find(x, link):
# if link[x] == x:
# return x
# link[x] = find(link[x], link);
# return link[x];
# find function with path compression (ITERATIVE)
def find(x, link):
p = x;
while( p != link[p]):
p = link[p];
while( x != p):
nex = link[x];
link[x] = p;
x = nex;
return p;
# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x,y = swap(x,y)
if x != y:
size[x] += size[y]
link[y] = x
## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n):
prime = [True for i in range(n+1)]
prime[0], prime[1] = False, False
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
return prime
# Euler's Toitent Function phi
def phi(n) :
result = n
p = 2
while(p * p<= n) :
if (n % p == 0) :
while (n % p == 0) :
n = n // p
result = result * (1.0 - (1.0 / (float) (p)))
p = p + 1
if (n > 1) :
result = result * (1.0 - (1.0 / (float)(n)))
return (int)(result)
def is_prime(n):
if n == 0:
return False
if n == 1:
return True
for i in range(2, int(n ** (1 / 2)) + 1):
if not n % i:
return False
return True
#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
spf[1] = 1;
for i in range(2, MAXN):
spf[i] = i;
for i in range(4, MAXN, 2):
spf[i] = 2;
for i in range(3, ceil(MAXN ** 0.5), 2):
if spf[i] == i:
for j in range(i*i, MAXN, i):
if spf[j] == j:
spf[j] = i;
## function for storing smallest prime factors (spf) in the array
################## un-comment below 2 lines when using factorization #################
spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
res = []
for i in range(2, int(x ** 0.5) + 1):
while x % i == 0:
res.append(i)
x //= i
if x != 1:
res.append(x)
return res
## this function is useful for multiple queries only, o/w use
## primefs function above. complexity O(log n)
def factors(n):
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
res.append(n // i)
return list(set(res))
## taking integer array input
def int_array():
return list(map(int, input().strip().split()));
def float_array():
return list(map(float, input().strip().split()));
## taking string array input
def str_array():
return input().strip().split();
#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
from itertools import permutations
import math
from bisect import bisect_left
def calc(x):
# print(x // 10)
return x if x < 10 else x // 10 + 9 - (0 if str(x)[0] <= str(x)[-1] else 1)
def solve():
l, r = read()
print(calc(r) - calc(l - 1))
if __name__ == '__main__':
for _ in range(1):
solve()
# fin_time = datetime.now()
# print("Execution time (for loop): ", (fin_time-init_time))
```
| 4,452 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Tags: binary search, combinatorics, dp
Correct Solution:
```
a1 = list(map(int,input().split()))
if (a1[0] < 10) & (a1[1] < 10):
print(a1[1] - a1[0]+1)
elif (a1[0] < 10) :
b = a1[1] - int(str(a1[1])[-1])
ans = 10 - a1[0] + b//10
if str(a1[1])[0] > str(a1[1])[len(str(a1[1]))-1]:
ans -= 1
else:
pass
print(ans)
else:
b1 = a1[0] - int(str(a1[0])[-1])
b2 = a1[1] - int(str(a1[1])[-1])
ans = (b2 - b1)//10 +1
if str(a1[0])[0] < str(a1[0])[len(str(a1[0]))-1]:
ans -= 1
if str(a1[1])[0] > str(a1[1])[len(str(a1[1]))-1]:
ans -= 1
print(ans)
```
| 4,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Tags: binary search, combinatorics, dp
Correct Solution:
```
def func(b):
n2 = len(b)
ans = 0
for i in range(1, n2):
if i == 1: ans += 9
else:
ans += 9 * (10 ** (i - 2))
for i in range(0, n2 - 1):
if n2 > 2:
if i == 0: tmp = (int(b[i]) - 1) * (10 ** (n2 - 2 - i))
else: tmp = (int(b[i])) * (10 ** (n2 - 2 - i))
else:
tmp = int(b[i]) - 1
ans += tmp
if n2 == 1: ans = int(b)
elif int(b[n2 - 1]) >= int(b[0]): ans -= -1
return ans
a, b = map(int, input().split())
a += -1
a = str(a)
b = str(b)
ans1 = func(a)
ans2 = func(b)
# print(ans1, ans2)
print(func(b) - func(a))
```
| 4,454 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Tags: binary search, combinatorics, dp
Correct Solution:
```
l, r = map(int, input().split())
result = 0
for digit in range(1, 10):
d_str = str(digit)
for length in range(3, 19):
if int(d_str + (length - 2) * '0' + d_str) <= r and \
int(d_str + (length - 2) * '9' + d_str) >= l:
a, b, c = 0, int((length - 2) * '9'), 0
while a < b:
c = (a + b) // 2
if int(d_str + '0' * (length - 2 - len(str(c))) + str(c) + d_str) < l:
a = c + 1
else:
b = c
l_end = a
a, b, c = 0, int((length - 2) * '9'), 0
while a < b:
c = (a + b + 1) // 2
if int(d_str + '0' * (length - 2 - len(str(c))) + str(c) + d_str) > r:
b = c - 1
else:
a = c
r_end = a
#print(str(l_end) + " " + str(r_end))
#print(" " + str(d_str + '0' * (length - 2 - len(str(l_end))) + str(l_end) + d_str))
#print(" " + str(d_str + '0' * (length - 2 - len(str(r_end))) + str(r_end) + d_str))
result += r_end - l_end + 1
for digit in range(1, 10):
length = 1
if l <= digit and digit <= r:
result += 1
length = 2
if int(str(digit) + str(digit)) >= l and int(str(digit) + str(digit)) <= r:
result += 1
print(result)
```
| 4,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Tags: binary search, combinatorics, dp
Correct Solution:
```
def f(x): return x if x<10 else x//10+9-(0 if str(x)[0]<=str(x)[-1] else 1)
l, r = map(int,input().split())
print(f(r)-f(l-1))
# Made By Mostafa_Khaled
```
| 4,456 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Tags: binary search, combinatorics, dp
Correct Solution:
```
import sys
import math as m
p = [0]*20
def fi():
return sys.stdin.readline()
def check(n):
ans = 0
ans+=min(9,n)
for i in range (10):
if i :
if 10*i + i <=n:
ans+=1
for i in range (3,19):
for j in range (1,10):
no = p[i-1]*j+j
lo = -1
hi = p[i-2]
res = 0
while hi-lo >1:
mid = (lo+hi)//2
if mid*10+no <= n:
lo = mid
else:
hi = mid
ans+=lo+1
return ans
if __name__ == '__main__':
p[0]=1
for i in range (1,19):
p[i]=p[i-1]*10
l,r = map(int, fi().split())
ans = check(r)-check(l-1)
print(ans)
```
| 4,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Tags: binary search, combinatorics, dp
Correct Solution:
```
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
#from __future__ import print_function, division #while using python2
# from itertools import accumulate
# from collections import defaultdict, Counter
def modinv(n,p):
return pow(n,p-2,p)
def get_numbers(s):
if len(s) == 1:
return int(s)
ans = 0
n = len(s)
for i in range(1, n):
ans += (9 * (10 ** (max(0, i-2))))
x = n-2
for i in range(n):
k = int(s[i])
if i == 0:
k -= 1
# print("i = ", i, ", k = ", k)
ans += (k * (10 ** x))
x -= 1
if x < 0:
break
if int(s[-1]) >= int(s[0]):
ans += 1
return ans
def main():
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
# print(get_numbers(input()))
l, r = [int(x) for x in input().split()]
# print(get_numbers(str(l-1)))
# print(get_numbers(str(r)))
print(get_numbers(str(r)) - get_numbers(str(l-1)))
#------------------ Python 2 and 3 footer by Pajenegod and c1729-----------------------------------------
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO,self).read()
def readline(self):
while self.newlines == 0:
s = self._fill(); self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s:self.buffer.write(s.encode('ascii'))
self.read = lambda:self.buffer.read().decode('ascii')
self.readline = lambda:self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
main()
```
| 4,458 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Tags: binary search, combinatorics, dp
Correct Solution:
```
l, r = map(int, input().split())
ans, t = 0, dict({l - 1: 0, r: 0})
if l == r:
exit(print(1 if str(l)[0] == str(l)[-1] else 0))
for num in [l - 1, r]:
le = 1
while True:
if len(str(num)) > le:
if le < 3:
t[num] += 9
else:
t[num] += (10 ** (le - 2)) * 9
else:
if le == 1:
t[num] += num
elif le == 2:
t[num] += int(str(num)[0]) if str(num)[1] >= str(num)[0] else int(str(num)[0]) - 1
else:
t[num] += int(str(num)[1:-1]) if int(str(num)[-1]) < int(str(num)[0]) else int(str(num)[1:-1]) + 1
t[num] += (int(str(num)[0]) - 1) * (10 ** (le - 2))
break
le += 1
# print(t)
print(t[r] - t[l - 1])
```
| 4,459 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
# author: sharrad99
def get(x):
return x if x < 10 else x // 10 + 9 - (0 if str(x)[0] <= str(x)[-1] else 1)
l, r = map(int, input().split())
print(get(r) - get(l - 1))
```
Yes
| 4,460 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l, r = list( map( int, input().split() ) )
def cnt( x ) :
ret = 0
for i in range( 1, x+1 ):
s = str(i)
if s[0] == s[-1]:
ret += 1
#print(s)
return ( ret )
def cnt2( x ):
ret = 0
s = str(x)
L = len(s)
if L >= 2 and x == 10**(L-1):
return ( cnt2( 10**(L-1)-1 ) )
if L <= 1:
return ( x )
elif L <= 2:
while x//10 != x%10:
x -= 1
return ( 9 + x//10 )
else:
ret = int( "1" + "0"*(L-3) + "8" )
num = list( str(x) )
#print( num )
if num[0] != num[-1]:
if int(num[0]) > int( num[-1] ):
how = int(num[-1]) + 10-int(num[0])
else:
how = int(num[-1]) - int(num[0])
x -= how
num = str(x)
d = x % 10
#print( 'num',num )
#print( 'd', d )
#print( 'num', num, 'ret before', ret )
ret += ( int(d-1) ) * int( "9" * (L-2) ) + int( num[1:-1] ) + int(d)
return ( ret )
#print( cnt(1024), cnt2(1024) )
#print( cnt(47), cnt2(47) )
#print( cnt(9), cnt2(9) )
#print( cnt(58), cnt2(58) )
#print( cnt(999), cnt2(999) )
#print( cnt(8987), cnt2(8987) )
#print()
#print( cnt(99999), cnt2(99999) )
#print( cnt(99899), cnt2(99899) )
#print( cnt(1000), cnt2(1000) )
#print( cnt2(1) )
#exit(0)
print( cnt2(r) - cnt2(l-1) )
```
Yes
| 4,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l,r=map(int,input().split())
'''
cnt = 0
for i in range(l,r+1):
if str(i)[0]==str(i)[-1]: cnt+=1
print(cnt)
def upto_x(n):
if n<=1000:
cnt = 0
for i in range(1,n+1):
if str(i)[0]==str(i)[-1]: cnt+=1
return cnt
s=str(n)
sm=0
slen=len(s)
if slen>=1:
sm=0
elif slen>=2:
sm+=9
elif slen>=3:
sm+=10
else:
sm=10**(slen-2)
print(sm)
now=10**(slen-1)
now*=int(s[0])
if s[-1]>=s[0]:
now+=10**(slen-1)
sm+=now
return sm
print(upto_x(r))
print(upto_x(r)-upto_x(l-1))'''
def upto_x(n):
if n<10:
return n
sm=n//10 +9
if str(n)[-1]<str(n)[0]:
sm-=1
return sm
print(upto_x(r)-upto_x(l-1))
```
Yes
| 4,462 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=10**9+7
EPS=1e-6
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
length_wise=[0,10,19]
for i in range(3,19):
length_wise.append(9*(10**(i-2))+length_wise[-1])
# print(length_wise)
def operate(x):
if(x==-1): return 0
x=str(x)
if(len(x)==1): return int(x)+1
ans=length_wise[len(x)-1]
key=min(int(x[0]),int(x[-1]))
key1=max(int(x[0]),int(x[-1]))
if(key==int(x[0])): type=1
else: type=2
# print(key,ans)
x=x[1:-1]
ans1=0
try:ans1=int(x)
except:pass
if(type==2):
ans+=(key1-1)*(10**len(x))
else:
ans+=(key-1)*(10**len(x))
ans1+=1
# print(ans,ans1,x)
return ans+ans1
l,r=value()
# print(operate(l-1))
# print(operate(r))
print(operate(r)-operate(l-1))
# BruteForce
# ans=0
#
# for i in range(l,r+1):
# x=str(i)
# if(x[0]==x[-1]):
# # print(x)
# ans+=1
# print(ans)
```
Yes
| 4,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l, r = map(int, input().split())
ans, t = 0, dict({l - 1: 0, r: 0})
if l == r:
exit(print(1))
for num in [l - 1, r]:
le = 1
while True:
if len(str(num)) > le:
if le < 3:
t[num] += 9
else:
t[num] += (le - 2) * 10 * 9
else:
if le == 1:
t[num] += num
elif le == 2:
t[num] += int(str(num)[0]) if str(num)[1] >= str(num)[0] else int(str(num)[0]) - 1
else:
t[num] += int(str(num)[1:-1]) if int(str(num)[-1]) < int(str(num)[0]) else int(str(num)[1:-1]) + 1
t[num] += (9 * int('1' * (le - 2))) * (int(str(num)[0]) - 1)
break
le += 1
# print(t)
print(t[r] - t[l - 1])
```
No
| 4,464 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l, r = map(int, input().split())
result = 0
for digit in range(1, 10):
d_str = str(digit)
for length in range(3, 18):
if int(d_str + (length - 2) * '0' + d_str) <= r and \
int(d_str + (length - 2) * '9' + d_str) >= l:
a, b, c = 0, int((length - 2) * '9'), 0
while a < b:
c = (a + b) // 2
if int(d_str + '0' * (length - 2 - len(str(c))) + str(c) + d_str) < l:
a = c + 1
else:
b = c
l_end = a
a, b, c = 0, int((length - 2) * '9'), 0
while a < b:
c = (a + b + 1) // 2
if int(d_str + '0' * (length - 2 - len(str(c))) + str(c) + d_str) > r:
b = c - 1
else:
a = c
r_end = a
#print(str(l_end) + " " + str(r_end))
#print(" " + str(d_str + '0' * (length - 2 - len(str(l_end))) + str(l_end) + d_str))
#print(" " + str(d_str + '0' * (length - 2 - len(str(r_end))) + str(r_end) + d_str))
result += r_end - l_end + 1
for digit in range(1, 10):
length = 1
if l <= digit and digit <= r:
result += 1
length = 2
if int(str(digit) + str(digit)) >= l and int(str(digit) + str(digit)) <= r:
result += 1
print(result)
```
No
| 4,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l, r = map(int, input().split())
ans, t = 0, dict({l - 1: 0, r: 0})
if l == r:
exit(print(1))
for num in [l - 1, r]:
le = 1
while True:
if len(str(num)) > le:
if le < 3:
t[num] += 9
else:
t[num] += (10 ** (le - 2)) * 9
else:
if le == 1:
t[num] += num
elif le == 2:
t[num] += int(str(num)[0]) if str(num)[1] >= str(num)[0] else int(str(num)[0]) - 1
else:
t[num] += int(str(num)[1:-1]) if int(str(num)[-1]) < int(str(num)[0]) else int(str(num)[1:-1]) + 1
t[num] += (9 * int('1' * (le - 2))) * (int(str(num)[0]) - 1)
break
le += 1
# print(t)
print(t[r] - t[l - 1])
```
No
| 4,466 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l β€ r). The Little Elephant has to find the number of such integers x (l β€ x β€ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 β€ l β€ r β€ 1018) β the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer β the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Submitted Solution:
```
l,r=map(int,input().split())
'''
cnt = 0
for i in range(l,r+1):
if str(i)[0]==str(i)[-1]: cnt+=1
print(cnt)
def upto_x(n):
if n<=1000:
cnt = 0
for i in range(1,n+1):
if str(i)[0]==str(i)[-1]: cnt+=1
return cnt
s=str(n)
sm=0
slen=len(s)
if slen>=1:
sm=0
elif slen>=2:
sm+=9
elif slen>=3:
sm+=10
else:
sm=10**(slen-2)
print(sm)
now=10**(slen-1)
now*=int(s[0])
if s[-1]>=s[0]:
now+=10**(slen-1)
sm+=now
return sm
print(upto_x(r))
print(upto_x(r)-upto_x(l-1))'''
def upto_x(n):
if n<10:
return 9
sm=n//10 +9
if str(n)[-1]<str(n)[0]:
sm-=1
return sm
print(upto_x(r)-upto_x(l-1))
```
No
| 4,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Tags: brute force, dp
Correct Solution:
```
R = lambda: map(int, input().split())
n, m = R()
rm = [list(map(int, input())) for i in range(n)]
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(n):
for j in range(m):
dp[i][j] = rm[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
res = 0
for r1 in range(n):
for c1 in range(m):
for r2 in range(r1, n):
for c2 in range(c1, m):
if not dp[r2][c2] - dp[r1 - 1][c2] - dp[r2][c1 - 1] + dp[r1 - 1][c1 - 1]:
res = max(res, 2 * (c2 - c1 + r2 - r1 + 2))
print(res)
```
| 4,468 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Tags: brute force, dp
Correct Solution:
```
n,m=map(int,input().split())
s=[list(input()) for i in range(n)]
cnt=[[0]*(m+1) for i in range(n+1)]
for i in range(n):
for j in range(m):
if s[i][j]=="1":
cnt[i+1][j+1]+=1
for i in range(n):
for j in range(m+1):
cnt[i+1][j]+=cnt[i][j]
for i in range(n+1):
for j in range(m):
cnt[i][j+1]+=cnt[i][j]
ans=0
for l in range(m+1):
for r in range(l+1,m+1):
for u in range(n+1):
for d in range(u+1,n+1):
c=cnt[d][r]-cnt[d][l]-cnt[u][r]+cnt[u][l]
if c==0:
ans=max(ans,(r-l)*2+(d-u)*2)
print(ans)
```
| 4,469 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Tags: brute force, dp
Correct Solution:
```
"""http://codeforces.com/problemset/problem/22/B"""
graph = []
def perimeter(x0, y0, x1, y1):
return (x1 - x0 + 1) * 2 + (y1 - y0 + 1) * 2
def is_valid(g, k, x0, y0, x1, y1):
ok = k.get((x0, y0, x1, y1))
if ok is None:
ok = g[x1][y1] == '0'
if x1 > x0:
ok = ok and is_valid(g, k, x0, y0, x1 - 1, y1)
if y1 > y0:
ok = ok and is_valid(g, k, x0, y0, x1, y1 - 1)
k[(x0, y0, x1, y1)] = ok
return ok
def topdown_dp(n, m, g):
k = {(i, j, i, j): True if g[i][j] == '0' else False
for i in range(n) for j in range(m)}
best = -1
for i in range(n):
for j in range(m):
for ii in range(i, n):
for jj in range(j, m):
if is_valid(g, k, i, j, ii, jj):
best = max(best, perimeter(i, j, ii, jj))
return best
if __name__ == '__main__':
n, m = map(int, input().split())
for _ in range(n):
graph.append(input())
print(topdown_dp(n, m, graph))
```
| 4,470 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Tags: brute force, dp
Correct Solution:
```
import sys, os, re, datetime
from collections import *
__cin_iter__ = None
def cin():
try:
global __cin_iter__
if __cin_iter__ is None: __cin_iter__ = iter(input().split(" "))
try: return next(__cin_iter__)
except StopIteration:
__cin_iter__ = iter(input().split(" "))
return next(__cin_iter__)
except EOFError: return None
def iin(): return int(cin())
def fin(): return float(cin())
def mat(v, *dims):
def submat(i):
if i == len(dims)-1: return [v for _ in range(dims[-1])]
return [submat(i+1) for _ in range(dims[i])]
return submat(0)
def iarr(n = 0): return [int(v) for v in input().split(" ")]
def farr(n = 0): return [float(v) for v in input().split(" ")]
def carr(n = 0): return input()
def sarr(n = 0): return [v for v in input().split(" ")]
def imat(n, m = 0): return [iarr() for _ in range(n)]
def fmat(n, m = 0): return [farr() for _ in range(n)]
def cmat(n, m = 0): return [input() for _ in range(n)]
def smat(n, m = 0): return [sarr() for _ in range(n)]
n = iin()
m = iin()
r = 0
b = mat(0, 28, 28)
dp = mat(0, 28, 28)
for i in range(1, n+1):
for j, v in enumerate(carr()):
j = j+1
b[i][j] = 1-int(v)
if b[i][j] > 0: dp[i][j] = dp[i][j-1]+1
for i in range(n, 0, -1):
for j in range(m, 0, -1):
w = 28
p = 0
h = 1
while b[i-h+1][j] > 0:
w = min(w, dp[i-h+1][j])
p = max(p, h+w)
h += 1
r = max(r, p)
print(2*r)
```
| 4,471 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Tags: brute force, dp
Correct Solution:
```
"""http://codeforces.com/problemset/problem/22/B"""
graph = []
def perimeter(x0, y0, x1, y1):
return (x1 - x0 + 1) * 2 + (y1 - y0 + 1) * 2
def check(x0, y0, x1, y1):
for i in range(x0, x1 + 1):
for j in range(y0, y1 + 1):
if graph[i][j] == '1': return False
return True
def solve(n, m, graph):
res = 4
for i in range(n):
for j in range(m):
for ii in range(i, n):
for jj in range(j, m):
if not check(i, j, ii, jj): continue
res = max(res, perimeter(i, j, ii, jj))
return res
if __name__ == '__main__':
n, m = map(int, input().split())
for _ in range(n):
graph.append(list(input()))
print(solve(n, m, graph))
```
| 4,472 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Tags: brute force, dp
Correct Solution:
```
n,m=list(map(int,input().split()))
a=[]
for i in range(n):
a.append(input())
d=0
for b in range(n,0,-1):
for c in range(m,0,-1):
for i in range(n-b+1):
for j in range(m-c+1):
z=0
for k in range(b):
if a[i+k][j:j+c]!='0'*c:
z=1
break
if z==0:
d=max(2*(b+c),d)
print(d)
```
| 4,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Tags: brute force, dp
Correct Solution:
```
import sys
dim_list = sys.stdin.readline().split()
n, m = int(dim_list[0]), int(dim_list[1])
# T[h_m][h_M][w_m][w_M]
T =[[[[-1 for l in range(m)] for k in range(m)] for j in range(n)] for i in range(n)]
#initialize 1x1 rectangles
for i in range(n):
row = sys.stdin.readline()
for j in range(m):
T[i][i][j][j] = int(row[j])
#maximum perimeter found so far
max_per = 4
# Increase k=length+width. Start with k=2 and increment by 1
for k in range(3,n+m+1): # 2 <= k <= n+m
found_k = 0 #if we find rectangle where width+height=k set to 1
# Let h denote the height of the rectangle and w the width
# 1 =< h <= n , 1 <= w <= m so for any given k we must have
# max(1,k-m) =< h <= min(n,k-1) and w=k-h
low = max(1,k-m)
high = min(n,k-1)
for h in range(low,high+1):
w=k-h
for x_m in range(n-h+1):
for y_m in range(m-w+1):
if h==1: #w can't be 1 since k>=3
if (T[x_m][x_m][y_m][y_m+w-2]==0 and T[x_m][x_m][y_m+w-1][y_m+w-1]==0):
T[x_m][x_m+h-1][y_m][y_m+w-1] = 0
max_per = 2*k
elif w==1:
if (T[x_m][x_m+h-2][y_m][y_m]==0 and T[x_m+h-1][x_m+h-1][y_m][y_m]==0):
T[x_m][x_m+h-1][y_m][y_m+w-1] = 0
max_per = 2*k
else:
if (T[x_m][x_m+h-2][y_m][y_m+w-1]==0 and T[x_m][x_m+h-1][y_m][y_m+w-2]==0 and T[x_m+h-1][x_m+h-1][y_m+w-1][y_m+w-1]==0):
T[x_m][x_m+h-1][y_m][y_m+w-1] = 0
max_per = 2*k
print(max_per)
```
| 4,474 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Tags: brute force, dp
Correct Solution:
```
n,m=map(int,input().split())
L=[list(map(int,input())) for i in range(n)]
A=[[0]*(m+1) for i in range(n)] # A[n][m+1]
for i in range(n):
for j in range(m):
A[i][j+1]=A[i][j]+L[i][j]
## L[i][j]+L[i][j+1]+L[i][j+2]+...+L[i][k] = A[i][k+1]-A[i][j]
out=0
for x1 in range(m):
for x2 in range(x1,m):
s=0
for y in range(n):
if A[y][x2+1]-A[y][x1]==0:
s+=1
out=max(out,((x2-x1+1)+s)*2)
else:
s=0
print(out)
```
| 4,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Submitted Solution:
```
n,m=map(int,input().split())
a=[list(map(int,input())) for _ in range(n)]
hlf=2
for ra in range(n):
for rb in range(ra,n):
cols=[]
for cc in range(m):
ok=1
for rr in range(ra,rb+1):
if a[rr][cc]:
ok=0
break
if ok:
cols.append(cc)
hlf=max(hlf,rb-ra+1+len(cols))
else:
cols=[]
print(hlf*2)
```
Yes
| 4,476 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Submitted Solution:
```
n, m = map(int, input().split())
dp = [[0 for j in range(m)] for i in range(n)]
mp = []
for i in range(n):
mp.append(input())
for j in range(m):
if mp[i][j] == '1':
continue
if not i or mp[i-1][j]=='1':
dp[i][j] = 1
else:
dp[i][j] = dp[i-1][j]+1
ans = 0
for i in range(n):
for j in range(m):
for k in range(1, dp[i][j]+1):
l = j
while l >= 0 and dp[i][l] >= k:
l -= 1
ans = max(ans, (j-l)+k)
print (ans<<1)
```
Yes
| 4,477 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Submitted Solution:
```
import sys
from array import array # noqa: F401
from itertools import accumulate
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, m = map(int, input().split())
a = [[0] * (m + 1)] + [[0] + list(accumulate(map(int, input().rstrip()))) for _ in range(n)]
for j in range(1, m + 1):
for i in range(n):
a[i + 1][j] += a[i][j]
ans = 0
for si in range(n):
for ti in range(si + 1, n + 1):
for sj in range(m):
for tj in range(sj + 1, m + 1):
if a[ti][tj] - a[ti][sj] - a[si][tj] + a[si][sj] == 0:
ans = max(ans, (ti - si + tj - sj) * 2)
print(ans)
```
Yes
| 4,478 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Submitted Solution:
```
n, m = map(int, input().split())
input = [input() for _ in range(n)]
d = []
for _ in range(n):
d.append([0 for _ in range(m)])
def a(i,j):
return 0 if i<0 or j<0 else d[i][j]
for i in range(n):
for j in range(m):
d[i][j] = int(input[i][j]) - a(i-1,j-1) + a(i,j-1) + a(i-1,j)
p = 0
for i in range(n):
for j in range(m):
for u in range(i, n):
for v in range(j, m):
x = a(u,v) - a(u,j-1) - a(i-1,v) + a(i-1,j-1)
if x == 0:
p = max(p, u + v - i - j)
print(p + p + 4)
```
Yes
| 4,479 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Submitted Solution:
```
import sys
dim_list = sys.stdin.readline().split()
n, m = int(dim_list[0]), int(dim_list[1])
# T[h_m][h_M][w_m][w_M]
T =[[[[-1 for l in range(m)] for k in range(m)] for j in range(n)] for i in range(n)]
#initialize 1x1 rectangles
for i in range(n):
row = sys.stdin.readline()
for j in range(m):
T[i][i][j][j] = int(row[j])
#maximum perimeter found so far
max_per = 4
# Increase k=length+width. Start with k=2 and increment by 1
for k in range(3,n+m+1): # 2 <= k <= n+m
found_k = 0 #if we find rectangle where width+height=k set to 1
# Let h denote the height of the rectangle and w the width
# 1 =< h <= n , 1 <= w <= m so for any given k we must have
# max(1,k-m) =< h <= min(n,k-1) and w=k-h
low = max(1,k-m)
high = min(n,k-1)
for h in range(low,high+1):
w=k-h
for x_m in range(n-h+1):
for y_m in range(m-w+1):
if (T[x_m][x_m+h-2][y_m][y_m+w-1]==0 and T[x_m][x_m+h-1][y_m][y_m+w-2]==0 and T[x_m+h-1][x_m+h-1][y_m+w-1][y_m+w-1]==0):
T[x_m][x_m+h-1][y_m][y_m+w-1] = 0
found_k = 1
max_per = 2*k
print(max_per)
```
No
| 4,480 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Submitted Solution:
```
import sys
dim_list = sys.stdin.readline().split()
n, m = int(dim_list[0]), int(dim_list[1])
# T[h_m][h_M][w_m][w_M]
T =[[[[-1 for l in range(m)] for k in range(m)] for j in range(n)] for i in range(n)]
#initialize 1x1 rectangles
for i in range(n):
row = sys.stdin.readline()
for j in range(m):
T[i][i][j][j] = int(row[j])
#maximum perimeter found so far
max_per = 4
# Increase k=length+width. Start with k=2 and increment by 1
for k in range(3,n+m+1): # 2 <= k <= n+m
found_k = 0 #if we find rectangle where width+height=k set to 1
# Let h denote the height of the rectangle and w the width
# 1 =< h <= n , 1 <= w <= m so for any given k we must have
# max(1,k-m) =< h <= min(n,k-1) and w=k-h
low = max(1,k-m)
high = min(n,k-1)
for h in range(low,high+1):
w=k-h
for x_m in range(n-h+1):
for y_m in range(m-w+1):
if (T[x_m][x_m+h-2][y_m][y_m+w-1]==0 and T[x_m][x_m+h-1][y_m][y_m+w-2]==0 and T[x_m+h-1][x_m+h-1][y_m+w-1][y_m+w-1]==0):
T[x_m][x_m+h-1][y_m][y_m+w-1] = 0
found_k = 1
max_per = 2*k
if found_k==0:
break
print(max_per)
```
No
| 4,481 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Submitted Solution:
```
def isBet(one, two, thr):
(a,b),(c,d),(e,f) = one, two, thr
x = False
y = False
if a==c:
x=e==a
elif (a>c):
if (e>=c and e<=a):
x = True
else:
if (e<=c and e>=a):
x=True
if b==d:
y=f==b
elif (b>d):
if (f>=d and f<=b):
y = True
else:
if (f<=d and f>=b):
y = True
return x==True and y==True
def perimeter(one, two):
(a,b), (c,d) = one, two
x = abs(c-a)+1
y = abs(d-b)+1
return 2*x+2*y
nandm = [int(i) for i in input().split()]
n = nandm[0]
m = nandm[1]
ones = []
zeroes = []
for i in range(n):
inp = list(input())
for j in range(len(inp)):
if inp[j]=="1":
ones.append((j+1, n-i))
else:
zeroes.append((j+1, n-i))
#print(ones)
#print(zeroes)
per = 1
for fir in range(len(zeroes)):
for sec in range(fir+1, len(zeroes)):
tup1 = zeroes[fir]
tup2 = zeroes[sec]
success = True
for x in ones:
if isBet(tup1, tup2, x):
success = False
if success:
per = max(per, perimeter(tup1, tup2))
if n==n==1:
print(4)
else:
print(per)
```
No
| 4,482 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
Examples
Input
3 3
000
010
000
Output
8
Input
5 4
1100
0000
0000
0000
0000
Output
16
Submitted Solution:
```
nm = input().split()
n = int(nm[0])
m = int(nm[1])
room = []
for row in range(n):
room.append(input())
best = 0
for row in range(n):
for col in range(m):
if room[row][col] == '1':
continue
col2 = m
for row2 in range(row, n):
col3 = col
while col3 < col2:
if room[row2][col3] == '1':
break
col3 += 1
col2 = col3
score = (col2-col)+(row2-row)+1
# print(col, row, col2, row2, score)
best = max(best, score)
print(2*best)
```
No
| 4,483 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Tags: implementation
Correct Solution:
```
input()
ar = list(map(int, input().split()))
cur = 0
ch, bi, ba = 0, 0, 0
for a in ar:
if cur % 3 == 0:
ch += a
elif cur % 3 == 1:
bi += a
else:
ba += a
cur += 1
if ch == max([ch, bi, ba]):
print("chest")
elif bi == max([ch, bi, ba]):
print("biceps")
else:
print("back")
```
| 4,484 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Tags: implementation
Correct Solution:
```
#n, k = map(int, input().split(" "))
#LA = [int(x) for x in input().split()]
n = int(input())
L = [int(x) for x in input().split()]
c = [0,0,0]
for i in range(n) :
c[i % 3] += L[i]
if (c[0] == max(c)) : print("chest")
if (c[1] == max(c)) : print("biceps")
if (c[2] == max(c)) : print("back")
```
| 4,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Tags: implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
answer = {0: 'chest', 1:'biceps', 2:'back'}
repitation = [0, 0, 0]
for i in range(n):
if (i+1) % 3 == 0:
repitation[2] += a[i]
elif (i+1) % 3 == 2:
repitation[1] += a[i]
else:
repitation[0] += a[i]
maxValue = 0
index = None
for i,v in enumerate(repitation):
if v > maxValue: maxValue = v; index = i
print(answer[index])
```
| 4,486 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Tags: implementation
Correct Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
"""
created by shhuan at 2017/11/24 23:13
"""
N = int(input())
A = [int(x) for x in input().split()]
c = sum(A[::3] or [0])
b = sum(A[1::3] or [0])
p = sum(A[2::3] or[0])
m = max(c, b, p)
if m == c:
print("chest")
elif m == b:
print("biceps")
else:
print("back")
```
| 4,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Tags: implementation
Correct Solution:
```
def check(l):
chest = 0
biceps = 0
back = 0
for i in range(0,2):
if len(l) % 3 == 0:
break
l.append(0)
for i in range(0,len(l),3):
chest += l[i]
biceps += l[i+1]
back +=l[i+2]
if chest > biceps and chest > back:
return 'chest'
elif biceps > chest and biceps > back:
return 'biceps'
else:
return 'back'
t = int(input())
a = input().split()
for i in range(0, len(a)):
a[i] = int(a[i])
print(check(a))
```
| 4,488 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Tags: implementation
Correct Solution:
```
n=int(input())
arr=list(map(int,input().split(' ')))
ex=[0]*3
for i in range(0,len(arr)):
ex[i%3]+=arr[i]
exc=(ex.index(max(ex)))
if(exc==0):
print('chest')
elif(exc==1):
print('biceps')
elif(exc==2):
print('back')
```
| 4,489 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Tags: implementation
Correct Solution:
```
t=int(input())
a=list(map(int,input().split()))
x=y=z=0
for i in range(t):
if(i%3==0):
x+=a[i]
elif(i%3==1):
y+=a[i]
else:
z+=a[i]
d=max(x,y,z)
if(d==x):
print("chest")
elif(d==y):
print("biceps")
else:
print("back")
```
| 4,490 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Tags: implementation
Correct Solution:
```
n=input()
mas=list(map(int,input().split()))
sm=[sum(mas[::3]),sum(mas[1::3]),sum(mas[2::3])]
ans=["chest","biceps","back"]
print(ans[sm.index(max(sm))])
```
| 4,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Submitted Solution:
```
n = int(input())
l = list(map(int,input().split()))
a = []
b = []
c = []
if len(l) == 2 :
if max(l) == l[0]:
print('chest')
exit()
else:
print('biceps')
exit()
elif len(l) == 3 :
if max(l) == l[0] :
print('chest')
exit()
elif max(l) == l[1] :
print('biceps')
exit()
else:
print('back')
exit()
else:
a= l[::3]
b = l[1::3]
c = l[2::3]
if sum(a) > sum(b) and sum(a) > sum(c):
print('chest')
exit()
elif sum(b) > sum(a) and sum(b) > sum(c):
print('biceps')
exit()
else:
print('back')
```
Yes
| 4,492 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
d = {}
d[0] = 0
d[1] = 0
d[2] = 0
for i in range(n):
d[i%3] += a[i]
res = max(d[0], d[1], d[2])
if res == d[0]:
print('chest')
elif res == d[1]:
print('biceps')
else:
print('back')
```
Yes
| 4,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Submitted Solution:
```
ch="chest"
bi="biceps"
ba="back"
m={ch:0,bi:0,ba:0}
n=int(input())
a=map(int,input().split())
s=[ch,bi,ba]*n
z=zip(s[:n],a)
for e,ai in z:
m[e]+=ai
print(max(m,key=m.get))
```
Yes
| 4,494 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
tr = [0]*3
for i in range(n):
tr[i%3] += a[i]
m = max(tr)
if m == tr[0]:
print("chest")
elif m == tr[1]:
print("biceps")
else:
print("back")
```
Yes
| 4,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Submitted Solution:
```
n = int(input())
l = list(map(int,input().split()))
c = 0
bi = 0
b = 0
for i in range(n):
if i%3==0:
c=c+l[i]
elif i%3==1:
bi = bi+l[i]
elif i%3==2:
b = b+l[i]
print(c,bi,b)
if c>bi and c>b:
print('chest')
elif bi>c and bi>b:
print('biceps')
elif b>c and b>bi:
print('back')
```
No
| 4,496 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Submitted Solution:
```
n=int(input())
p=list(map(int,input().split()))
c=0
bic=0
back=0
if n==1:
c+=p[0]
elif n==2:
c+=p[0]
bic+=p[1]
else:
for i in range(0,n-2,3):
c+=p[i]
if i+1<n:
bic+=p[i+1]
if i+2<n:
back+=p[i+2]
print(c,bic,back)
d={c:"chest",bic:"biceps",back:"back"}
print(d[max(c,bic,back)])
```
No
| 4,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Submitted Solution:
```
n=int(input())
j=n+1
a=[0]*n
a=list(map(int,input().split()))
bi=0
ba=0
ch=0
if n==1:
ch=ch+a[0]
elif n==2:
ch=ch+a[0]
bi=bi+a[1]
else:
j=0
for i in range(0,int(n/3)):
ch=ch+a[j]
bi=bi+a[j+1]
ba=ba+a[j+2]
j=j+3
t=n%3
if t==1:
ch=ch+a[n-t]
elif t==2:
ch=ch+a[n-t]
bi=bi+a[n-t+1]
print(ch,bi,ba)
if ch>=bi:
if ch>=ba:
print("chest")
else:
print("back")
elif bi>=ba:
print("biceps")
else:
print("back")
```
No
| 4,498 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was n integers a1, a2, ..., an. These numbers mean that Greg needs to do exactly n exercises today. Besides, Greg should repeat the i-th in order exercise ai times.
Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the n-th exercise.
Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training.
Input
The first line contains integer n (1 β€ n β€ 20). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 25) β the number of times Greg repeats the exercises.
Output
Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise.
It is guaranteed that the input is such that the answer to the problem is unambiguous.
Examples
Input
2
2 8
Output
biceps
Input
3
5 1 10
Output
back
Input
7
3 3 2 7 9 6 8
Output
chest
Note
In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises.
In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises.
In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
Submitted Solution:
```
n=int(input())
ans=[0,0,0]
t=[]
t+=map(int,input().split())
for i in range(n):
ans[i//3]+=t[i]
if ans[0]>ans[1] and ans[0]>ans[2]:
print('chest')
elif ans[1]>ans[2]:
print('biceps')
else:
print('back')
```
No
| 4,499 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.