output_description stringlengths 15 956 | submission_id stringlengths 10 10 | status stringclasses 3 values | problem_id stringlengths 6 6 | input_description stringlengths 9 2.55k | attempt stringlengths 1 13.7k | problem_description stringlengths 7 5.24k | samples stringlengths 2 2.72k |
|---|---|---|---|---|---|---|---|
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s587387450 | Wrong Answer | p03591 | Input is given from Standard Input in the following format:
S | a = input()
print(a.startswith("YAKI"))
| Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s674798551 | Runtime Error | p03591 | Input is given from Standard Input in the following format:
S | S
| Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s052262960 | Runtime Error | p03591 | Input is given from Standard Input in the following format:
S | print('YNEOS'[if input()[:4]!='YAKI'::2]) | Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s339990245 | Runtime Error | p03591 | Input is given from Standard Input in the following format:
S | h, w = map(int, input().split())
graph = [[] * w for i in range(h)]
for i in range(h):
graph[i] = list(input())
A = 0
B = 0
C = 0
D = 0
E = 0
F = 0
G = 0
H = 0
I = 0
J = 0
K = 0
L = 0
M = 0
N = 0
O = 0
P = 0
Q = 0
R = 0
S = 0
T = 0
U = 0
V = 0
W = 0
X = 0
Y = 0
Z = 0
for i in range(h):
A = A + graph[i].count("a")
for i in range(h):
B = B + graph[i].count("b")
for i in range(h):
C = C + graph[i].count("c")
for i in range(h):
D = D + graph[i].count("d")
for i in range(h):
E = E + graph[i].count("e")
for i in range(h):
F = F + graph[i].count("f")
for i in range(h):
G = G + graph[i].count("g")
for i in range(h):
H = H + graph[i].count("h")
for i in range(h):
I = I + graph[i].count("i")
for i in range(h):
J = J + graph[i].count("j")
for i in range(h):
K = K + graph[i].count("k")
for i in range(h):
L = L + graph[i].count("l")
for i in range(h):
M = M + graph[i].count("m")
for i in range(h):
N = N + graph[i].count("n")
for i in range(h):
O = O + graph[i].count("o")
for i in range(h):
P = P + graph[i].count("p")
for i in range(h):
Q = Q + graph[i].count("q")
for i in range(h):
R = R + graph[i].count("r")
for i in range(h):
S = S + graph[i].count("s")
for i in range(h):
T = T + graph[i].count("t")
for i in range(h):
U = U + graph[i].count("u")
for i in range(h):
V = V + graph[i].count("v")
for i in range(h):
W = W + graph[i].count("w")
for i in range(h):
X = X + graph[i].count("x")
for i in range(h):
Y = Y + graph[i].count("y")
for i in range(h):
Z = Z + graph[i].count("z")
List = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
if w % 2 == 0 and h % 2 == 0:
for i in range(26):
if List[i] % 4 == 0:
if i == 25:
print("Yes")
else:
continue
else:
print("No")
break
elif w % 2 == 0:
a_2 = 0
a_4 = 0
for i in range(26):
if List[i] % 4 == 1 or List[i] % 4 == 3:
print("No")
break
elif List[i] % 4 == 0:
a_4 = a_4 + 1
else:
a_2 = a_2 + 1
if a_2 % 2 == (w // 2) % 2 and a_2 <= (w // 2):
print("Yes")
elif List[i] % 4 == 1 or List[i] % 4 == 3:
print()
else:
print("No")
elif h % 2 == 0:
a_2 = 0
a_4 = 0
for i in range(26):
if List[i] % 4 == 1 or List[i] % 4 == 3:
print("No")
break
elif List[i] % 4 == 0:
a_4 = a_4 + 1
else:
a_2 = a_2 + 1
if a_2 % 2 == (h // 2) % 2 and a_2 <= (h // 2):
print("Yes")
elif List[i] % 4 == 1 or List[i] % 4 == 3:
print()
else:
print("No")
else:
a_1 = 0
a_2 = 0
a_3 = 0
a_4 = 0
for i in range(26):
if List[i] % 4 == 1:
a1 = List[i]
a_1 = a_1 + 1
if a_1 == 2:
print("No")
break
elif List[i] % 4 == 2:
a_2 = a_2 + 1
elif List[i] % 4 == 3:
a3 = List[i]
a_3 = a_3 + 1
if a_3 == 2:
print("No")
else:
a_4 = a_4 + 1
if a_1 == 1 and a_3 == 0:
if a_2 % 2 == (((h - 1) // 2) + ((w - 1) // 2)) % 2 and a_2 <= (
(h - 1) // 2
) + ((w - 1) // 2) - (
max(0, 2 * (((a_1 - 1) // 4) - (((h - 1) // 2) * (w - 1) // 2)))
):
print("Yes")
else:
print("No")
elif a_1 == 0 and a_3 == 1:
if a_2 % 2 == (((h - 1) // 2) + ((w - 1) // 2) + 1) % 2 and a_2 <= (
(h - 1) // 2
) + ((w - 1) // 2) - 1 - (
max(0, 2 * (((a_3 - 3) // 4) - (((h - 1) // 2) * (w - 1) // 2)))
):
print("Yes")
else:
print("No")
elif a_1 == 2:
print()
elif a_3 == 2:
print()
else:
print("No")
| Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s973955612 | Runtime Error | p03591 | Input is given from Standard Input in the following format:
S | S = str(input())
if S[1:4] = 'YAKI'
print('Yes')
else:
print('No') | Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s738403418 | Runtime Error | p03591 | Input is given from Standard Input in the following format:
S | a, b, k = map(int, input().split())
print(
"NYoe s"[
1
in [
k == (b - t) * s + t * (a - s) for t in range(b + 1) for s in range(a + 1)
] :: 2
]
)
| Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s300857128 | Accepted | p03591 | Input is given from Standard Input in the following format:
S | print("YNeos"["YAKI" != input()[:4] :: 2])
| Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s482379490 | Runtime Error | p03591 | Input is given from Standard Input in the following format:
S | git merge upstream/master | Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
* * * | s388785741 | Wrong Answer | p03591 | Input is given from Standard Input in the following format:
S | print("YNeos"[not input()[:4] == "Yaki" :: 2])
| Statement
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves _yakiniku_ (a Japanese term meaning
grilled meat. _yaki_ : grilled, _niku_ : meat). He supposes that Snuke likes
grilled things starting with `YAKI` in Japanese, and does not like other
things.
You are given a string S representing the Japanese name of Ringo's present to
Snuke. Determine whether S starts with `YAKI`. | [{"input": "YAKINIKU", "output": "Yes\n \n\n`YAKINIKU` starts with `YAKI`.\n\n* * *"}, {"input": "TAKOYAKI", "output": "No\n \n\n`TAKOYAKI` (a Japanese snack. _tako_ : octopus) does not start with `YAKI`.\n\n* * *"}, {"input": "YAK", "output": "No"}] |
In the i-th line, print the response to the i-th query. If the intersection
(c_i, d_i) cannot be reached from the intersection (a_i, b_i) by walking along
the roads, print `-1` instead.
* * * | s519663122 | Runtime Error | p03084 | Input is given from Standard Input in the following format:
N M Q
S
T
a_1 b_1 c_1 d_1
a_2 b_2 c_2 d_2
:
a_Q b_Q c_Q d_Q | def read():
N, M, Q = map(int, input().split(" "))
S = input()
T = input()
qs = []
for _ in range(N):
a, b, c, d = map(int, input().split(" "))
qs.append((a, b, c, d))
return N, M, Q, S, T, qs
DS = {"W": (-1, 0), "E": (1, 0), "N": (0, -1), "S": (0, 1)}
def ans_q(N, M, Q, S, T, a, b, c, d):
# ds[i][j] スタート地点からの距離(未達は-1)
ds = [[-1 for _ in range(M)] for _ in range(N)]
q = [(a - 1, b - 1)]
ds[a - 1][b - 1] = 0
while q:
i, j = q.pop()
dd = ds[i][j]
drs = [DS.get(s) for s in [S[i], T[j]]]
for dx, dy in drs:
i2 = i + dy
j2 = j + dx
if i2 < 0 or i2 >= N or j2 < 0 or j2 >= M:
continue
if ds[i2][j2] >= 0 and ds[i2][j2] <= dd + 1:
continue
ds[i2][j2] = dd + 1
q.append((i2, j2))
return ds[c - 1][d - 1]
if __name__ == "__main__":
N, M, Q, S, T, qs = read()
for a, b, c, d in qs:
print(ans_q(N, M, Q, S, T, a, b, c, d))
| Statement
In Takaha-shi, the capital of Republic of AtCoder, there are N roads extending
east and west, and M roads extending north and south. There are no other
roads. The i-th east-west road from the north and the j-th north-south road
from the west cross at the intersection (i, j). Two east-west roads do not
cross, nor do two north-south roads. The distance between two adjacent roads
in the same direction is 1.
Each road is one-way; one can only walk in one direction. The permitted
direction for each road is described by a string S of length N and a string T
of length M, as follows:
* If the i-th character in S is `W`, one can only walk westward along the i-th east-west road from the north;
* If the i-th character in S is `E`, one can only walk eastward along the i-th east-west road from the north;
* If the i-th character in T is `N`, one can only walk northward along the i-th north-south road from the west;
* If the i-th character in T is `S`, one can only walk southward along the i-th south-west road from the west.
Process the following Q queries:
* In the i-th query, a_i, b_i, c_i and d_i are given. What is the minimum distance to travel to reach the intersection (c_i, d_i) from the intersection (a_i, b_i) by walking along the roads? | [{"input": "4 5 4\n EEWW\n NSNNS\n 4 1 1 4\n 1 3 1 2\n 4 2 3 2\n 3 3 3 5", "output": "6\n 11\n 5\n 4\n \n\nThe permitted direction for each road is shown in the following figure (north\nupward):\n\n\n\nFor each of the four queries, a route that achieves the minimum travel\ndistance is as follows:\n\n\n\n* * *"}, {"input": "3 3 2\n EEE\n SSS\n 1 1 3 3\n 3 3 1 1", "output": "4\n -1\n \n\nThe travel may be impossible.\n\n* * *"}, {"input": "9 7 10\n EEEEEWEWW\n NSSSNSN\n 4 6 9 2\n 3 7 6 7\n 7 5 3 5\n 1 1 8 1\n 4 3 5 4\n 7 4 6 4\n 2 5 8 6\n 6 6 2 7\n 2 4 7 5\n 7 2 9 7", "output": "9\n -1\n 4\n 9\n 2\n 3\n 7\n 7\n 6\n -1"}] |
In the i-th line, print the response to the i-th query. If the intersection
(c_i, d_i) cannot be reached from the intersection (a_i, b_i) by walking along
the roads, print `-1` instead.
* * * | s670903132 | Wrong Answer | p03084 | Input is given from Standard Input in the following format:
N M Q
S
T
a_1 b_1 c_1 d_1
a_2 b_2 c_2 d_2
:
a_Q b_Q c_Q d_Q | from collections import defaultdict
from heapq import heappop, heappush
class Graph(object):
def __init__(self):
self.graph = defaultdict(list)
def __len__(self):
return len(self.graph)
def add_edge(self, src, dst, weight=1):
self.graph[src].append((dst, weight))
def get_nodes(self):
return self.graph.keys()
class Dijkstra(object):
def __init__(self, graph, start):
self.g = graph.graph
self.dist = defaultdict(lambda: float("inf"))
self.dist[start] = 0
self.prev = defaultdict(lambda: None)
self.Q = []
heappush(self.Q, (self.dist[start], start))
while self.Q:
dist_u, u = heappop(self.Q)
if self.dist[u] < dist_u:
continue
for v, weight in self.g[u]:
alt = dist_u + weight
if self.dist[v] > alt:
self.dist[v] = alt
self.prev[v] = u
heappush(self.Q, (alt, v))
def shortest_distance(self, goal):
return self.dist[goal]
n, m, q = list(map(int, input().split()))
s = list(input())
t = list(input())
inputs = []
for i, s_ in enumerate(s):
if s_ == "E":
inputs += [(i * m + j, i * m + j + 1, 1) for j in range(0, m - 1)]
else:
inputs += [(i * m + j, i * m + j - 1, 1) for j in range(m - 1, 0, -1)]
for i, t_ in enumerate(t):
if t_ == "N":
inputs += [(j * m + i, (j - 1) * m + i, 1) for j in range(m - 1, 0, -1)]
else:
inputs += [(j * m + i, (j + 1) * m + i, 1) for j in range(0, m - 1)]
g = Graph()
for src, dst, weight in inputs:
g.add_edge(src, dst, weight)
for i in range(q):
a, b, c, d = list(map(int, input().split()))
dij = Dijkstra(g, (a - 1) * m + b - 1)
dist = dij.shortest_distance((c - 1) * m + d - 1)
if dist != float("inf"):
print(dist)
else:
print(-1)
| Statement
In Takaha-shi, the capital of Republic of AtCoder, there are N roads extending
east and west, and M roads extending north and south. There are no other
roads. The i-th east-west road from the north and the j-th north-south road
from the west cross at the intersection (i, j). Two east-west roads do not
cross, nor do two north-south roads. The distance between two adjacent roads
in the same direction is 1.
Each road is one-way; one can only walk in one direction. The permitted
direction for each road is described by a string S of length N and a string T
of length M, as follows:
* If the i-th character in S is `W`, one can only walk westward along the i-th east-west road from the north;
* If the i-th character in S is `E`, one can only walk eastward along the i-th east-west road from the north;
* If the i-th character in T is `N`, one can only walk northward along the i-th north-south road from the west;
* If the i-th character in T is `S`, one can only walk southward along the i-th south-west road from the west.
Process the following Q queries:
* In the i-th query, a_i, b_i, c_i and d_i are given. What is the minimum distance to travel to reach the intersection (c_i, d_i) from the intersection (a_i, b_i) by walking along the roads? | [{"input": "4 5 4\n EEWW\n NSNNS\n 4 1 1 4\n 1 3 1 2\n 4 2 3 2\n 3 3 3 5", "output": "6\n 11\n 5\n 4\n \n\nThe permitted direction for each road is shown in the following figure (north\nupward):\n\n\n\nFor each of the four queries, a route that achieves the minimum travel\ndistance is as follows:\n\n\n\n* * *"}, {"input": "3 3 2\n EEE\n SSS\n 1 1 3 3\n 3 3 1 1", "output": "4\n -1\n \n\nThe travel may be impossible.\n\n* * *"}, {"input": "9 7 10\n EEEEEWEWW\n NSSSNSN\n 4 6 9 2\n 3 7 6 7\n 7 5 3 5\n 1 1 8 1\n 4 3 5 4\n 7 4 6 4\n 2 5 8 6\n 6 6 2 7\n 2 4 7 5\n 7 2 9 7", "output": "9\n -1\n 4\n 9\n 2\n 3\n 7\n 7\n 6\n -1"}] |
Output the amount of money each of Alice and Brown receive in a line. | s689659368 | Accepted | p00352 | The input is given in the following format.
a b
A line of data is given that contains two values of money: a (1000 ≤ a ≤
50000) for Alice and b (1000 ≤ b ≤ 50000) for Brown. | print(int(sum(map(int, input().split())) / 2))
| Handsel
Alice and Brown are brothers in a family and each receives pocket money in
celebration of the coming year. They are very close and share the total amount
of the money fifty-fifty. The pocket money each receives is a multiple of
1,000 yen.
Write a program to calculate each one’s share given the amount of money Alice
and Brown received. | [{"input": "1000 3000", "output": "2000"}, {"input": "5000 5000", "output": "5000"}, {"input": "1000 2000", "output": "1500"}] |
Output the amount of money each of Alice and Brown receive in a line. | s930696883 | Accepted | p00352 | The input is given in the following format.
a b
A line of data is given that contains two values of money: a (1000 ≤ a ≤
50000) for Alice and b (1000 ≤ b ≤ 50000) for Brown. | print(sum(map(int, input().split())) >> 1)
| Handsel
Alice and Brown are brothers in a family and each receives pocket money in
celebration of the coming year. They are very close and share the total amount
of the money fifty-fifty. The pocket money each receives is a multiple of
1,000 yen.
Write a program to calculate each one’s share given the amount of money Alice
and Brown received. | [{"input": "1000 3000", "output": "2000"}, {"input": "5000 5000", "output": "5000"}, {"input": "1000 2000", "output": "1500"}] |
Output the amount of money each of Alice and Brown receive in a line. | s075783693 | Wrong Answer | p00352 | The input is given in the following format.
a b
A line of data is given that contains two values of money: a (1000 ≤ a ≤
50000) for Alice and b (1000 ≤ b ≤ 50000) for Brown. | print(sum(map(int, input().split())) / 2)
| Handsel
Alice and Brown are brothers in a family and each receives pocket money in
celebration of the coming year. They are very close and share the total amount
of the money fifty-fifty. The pocket money each receives is a multiple of
1,000 yen.
Write a program to calculate each one’s share given the amount of money Alice
and Brown received. | [{"input": "1000 3000", "output": "2000"}, {"input": "5000 5000", "output": "5000"}, {"input": "1000 2000", "output": "1500"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s489249691 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | N = int(input())
A = [int(x) for x in input().split()]
B = [0] * (N + 1)
for i in range(N):
B[A[i]] = i + 1
L = list(range(N + 2))
R = list(range(N + 2))
cnt = 0
for i in range(N, 0, -1):
l = L[B[i]]
r = R[B[i]]
cnt += i * (B[i] - l + 1) * (r - B[i] + 1)
L[r + 1] = l
R[l - 1] = r
print(cnt)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s616152771 | Wrong Answer | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | # B - Minimum Sum
N = int(input())
A = list(enumerate(list(map(int, input().split()))))
left = [i - 1 for i in range(N)]
right = [i + 1 for i in range(N)]
A.sort(key=lambda x: x[1], reverse=True)
ans = 0
for i, a in A:
il, ir = left[i], right[i]
ans += a * (i - il) * (ir - i)
if ir < N:
left[ir] = il
if il > 0:
right[il] = ir
print(ans)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s395065318 | Wrong Answer | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | # AGC005 B
N = int(input())
A = list(map(int, input().split()))[::-1]
MIN = A[0]
BACK = A[0]
type2 = 1
BACKSUM = A[0]
ans = A[0]
for i in range(1, N):
if A[i] < MIN:
MIN = A[i]
ans += MIN * (i + 1)
BACKSUM = MIN * (i + 1)
type2 = 1
else:
if A[i] > BACK:
ans += A[i] + BACKSUM
BACKSUM = A[i] + BACKSUM
type2 = 1
else:
ans += A[i] + BACKSUM - ((BACK - A[i]) * type2)
BACKSUM = A[i] + BACKSUM - ((BACK - A[i]) * type2)
type2 += 1
# print(BACKSUM)
BACK = A[i]
print(ans)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s276952553 | Runtime Error | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);
int main() {
int N;
cin>>N;
int a[N];
int tmptmp;
for(int i=0;i<N;++i) {
cin>>tmptmp;
tmptmp--;
a[tmptmp] = i;
}
set<ll> st;
st.insert(-1);
st.insert(N);
auto tmp=st.begin(), tmp1=st.begin(), tmp2=st.begin();
ll ans = 0;
for(ll i=0;i<N;++i) {
st.insert(a[i]);
tmp = st.find(a[i]);
tmp1 = st.find(a[i]);
tmp1--;
tmp2 = st.find(a[i]);
tmp2++;
ans += (i+1)*(*tmp-*tmp1)%mod*(*tmp2-*tmp)%mod;
ans %= mod;
}
cout<<ans<<endl;
}
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s212557171 | Wrong Answer | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | st = input().split()
N = int(st[0])
a = st[1:]
ret = 0
b = [10 for k in range(N)]
for i, ai in enumerate(a):
nai = int(ai)
for j in range(0, i + 1):
b[j] = min(b[j], nai)
ret += b[j]
print(ret)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s091669264 | Wrong Answer | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | 1 / (not " " in input())
1 / (not " " in input())
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s969449052 | Runtime Error | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | n = int(input())
a = input().split()
score = 0
for k in range(0, n):
for l in range(k, n):
score += int(min(int(a[k : l + 1])))
print(score)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s801630189 | Runtime Error | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | from collections import deque
n=int(input())
a=list(map(int,input().split()))
A=[a[0]+a[0]<a[1]-a[0]>a[1]]+a+[a[-1]+a[-1]<a[-2]-a[-1]>a[-2]]
valley=0
peak=1
dp=deque()
for i in range(1,n+1):
if A[i-1]<A[i]>A[i+1]:
dp.append((i-1,peak))
elif A[i-1]>A[i]<A[i+1]:
dp.append((i-1,valley))
l,lf=dp.popleft()
r,rf=dp.popleft()
if lf == peak:
ans=a[0]
else:
ans=a[0]*(r+1)
for i in range(1,n-1):
if r == i:
t,tf=r,rf
r,rf=dp.popleft()
if rf == valley:
ans+=a[i]*(i-l+1)*(r-i+1)
else:
ans+=a[i]
l,lf=t,tf
else:
if lf == valley:
ans+=a[i]*(r-i+1)
else:
ans+=a[i]*(i-l+1)
print(ans) | Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s365642603 | Runtime Error | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | def main():
class Avltree:
def __init__(self,key=None):
self.key = key
self.left = None
self.right = None
self.balance = "Even"
def search(self,key):
if self.key == None:
return None
if self.key > key:
if self.left == None:
return None
else:
return self.left.search(key)
if self.key < key:
if self.right == None:
return None
else:
return self.right.search(key)
return self
def search_lower(self,key,key_lower):
if self.key == None:
return key_lower
if self.key > key:
if self.left == None:
return key_lower
else:
return self.left.search_lower(key,key_lower)
if self.key < key:
key_lower = self.key
if self.right == None:
return key_lower
else:
return self.right.search_lower(key,key_lower)
if self.left == None:
return key_lower
else:
if key_lower == None:
return self.left.end_higher(self.left.key)
else:
return max(key_lower,self.left.end_higher(self.left.key))
def search_higher(self,key,key_higher):
if self.key == None:
return key_higher
if self.key > key:
key_higher = self.key
if self.left == None:
return key_higher
else:
return self.left.search_higher(key,key_higher)
if self.key < key:
if self.right == None:
return key_higher
else:
return self.right.search_higher(key,key_higher)
if self.right == None:
return key_higher
else:
if key_higher == None:
return self.right.end_lower(self.right.key)
else:
return min(key_higher,self.right.end_lower(self.right.key))
def end_lower(self,end_lower_key):
if self.left == None:
return end_lower_key
else:
end_lower_key = self.left.key
return self.left.end_lower(end_lower_key)
def end_higher(self,end_higher_key):
if self.right == None:
return end_higher_key
else:
end_higher_key = self.right.key
return self.right.end_higher(end_higher_key)
def DoubleRightRotation(self):
tl = self.left
self.left = tl.right.right
tl.right.right = self
tlr = tl.right
tl.right = tlr.left
tlr.left = tl
if tlr.balance == "Left":
tlr.right.balance = "Right"
tlr.left.balance = "Even"
elif tlr.balance == "Right":
tlr.right.balance = "Even"
tlr.left.balance = "Left"
elif tlr.balance == "Even":
tlr.right.balance = "Even"
tlr.left.balance = "Even"
tlr.balance = "Even"
return tlr
def DoubleLeftRotation(self):
tr = self.right
self.right = tr.left.left
tr.left.left = self
trl = tr.left
tr.left = trl.right
trl.right = tr
if trl.balance == "Right":
trl.left.balance = "Left"
trl.right.balance = "Even"
elif trl.balance == "Left":
trl.left.balance = "Even"
trl.right.balance = "Right"
elif trl.balance == "Even":
trl.left.balance = "Even"
trl.right.balance = "Even"
trl.balance = "Even"
return trl
def SingleLeftRotation(self):
tr = self.right
tr.balance = "Even"
self.balance = "Even"
self.right = tr.left
tr.left = self
return tr
def SingleRightRotation(self):
tl = self.left
tl.balance = "Even"
self.balance = "Even"
self.left = tl.right
tl.right = self
return tl
def replace(self,p,v):
if p.left == self:
p.left = v
else :
p.right = v
def insert(self,key):
if self.key == None:
self.key = key
return self
if key < self.key:
if self.left == None:
self.left = Avltree(key)
else:
self.left.insertx(self,key)
elif key > self.key:
if self.right == None:
self.right = Avltree(key)
else:
self.right.insertx(self,key)
else: # key == self.key:
pass # do not overwrite
def insertx(self,p,key):
if self.key > key:
if self.left == None:
self.left = Avltree(key)
else:
if not self.left.insertx(self, key):
return False
if self.balance == "Right":
self.balance = "Even"
return False
elif self.balance == "Even":
self.balance = "Left"
return True
elif self.balance == "Left":
if self.left.balance == "Right":
self.replace(p,self.DoubleRightRotation())
elif self.left.balance == "Left":
self.replace(p,self.SingleRightRotation())
return False
if self.key < key:
if self.right == None:
self.right = Avltree(key)
else:
if not self.right.insertx(self, key):
return False
if self.balance == "Left":
self.balance = "Even"
return False
elif self.balance == "Even":
self.balance = "Right"
return True
elif self.balance == "Right":
if self.right.balance == "Left":
self.replace(p,self.DoubleLeftRotation())
elif self.right.balance == "Right":
self.replace(p,self.SingleLeftRotation())
return False
return False
def to_s(self):
return self.key
def left_s(self):
if self.left == None:
return None
return (self.left).key
def right_s(self):
if self.right == None:
return None
return (self.right).key
n=int(input())
a=list(map(int,input().split()))
ans=0
l=[0]*n
for i in range(n):
l[a[i]-1]=i
root=Avltree()
root.insert(-1)
root.insert(n)
for i in range(n):
root.insert(l[i])
ans+=(i+1)*(l[i]-root.search_lower(l[i],None))*(root.search_higher(l[i],None)-l[i])
print(ans)
if __name__ == '__main__':
main() | Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s236313411 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | import sys
sys.setrecursionlimit(10000000)
MOD = 10**9 + 7
INF = 10**15
class SegmentTree:
def __init__(self, N, f, unit):
self.f = f
self.unit = unit
self.N = N
self.tree = [self.unit] * (2 * self.N)
# self._build(array)
def _build(self, array):
for i, x in enumerate(array, self.N):
self.tree[i] = x
for i in range(self.N - 1, 0, -1):
self.tree[i] = self.f(self.tree[i << 1], self.tree[i << 1 | 1])
def update(self, k, x):
k += self.N
self.tree[k] = x
while k > 1:
k >>= 1
self.tree[k] = self.f(self.tree[k << 1], self.tree[k << 1 | 1])
def query(self, l, r):
l += self.N
r += self.N
vl = self.unit
vr = self.unit
while l < r:
if l & 1:
vl = self.f(vl, self.tree[l])
l += 1
if r & 1:
r -= 1
vr = self.f(self.tree[r], vr)
l >>= 1
r >>= 1
return self.f(vl, vr)
def debug(self):
print(self.tree)
def main():
N = int(input())
A = list(map(int, input().split()))
st = SegmentTree(N, max, -1)
L = [-1] * N
for i, a in enumerate(A):
L[i] = st.query(0, a - 1)
st.update(a - 1, i)
st = SegmentTree(N, min, N)
R = [N] * N
for i in range(N - 1, -1, -1):
R[i] = st.query(0, A[i] - 1)
st.update(A[i] - 1, i)
ans = sum((R[i] - i) * (i - L[i]) * A[i] for i in range(N))
print(ans)
if __name__ == "__main__":
main()
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s412645841 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(N, A):
ans = 0
Ai = [(a, i + 1) for i, a in enumerate(A)]
Ai.sort(key=lambda x: x[0])
st = SegmentTree(n=N + 2, f=lambda x, y: x + y)
st.set_val(0, 1)
st.set_val(N + 1, 1)
for a, i in Ai:
lc = st.query(0, i)
l = st.bisect_left(lambda x: x < lc)
r = st.bisect_left(lambda x: x < lc + 1)
ans += a * (i - l) * (r - i)
st.set_val(i, 1)
print(ans)
class SegmentTree:
def __init__(self, n=None, f=max, identity_factory=int, initial_values=None):
assert n or initial_values
size = n if n else len(initial_values)
d = [identity_factory() for _ in range(2 * size + 1)]
self.__n, self.__d, self.__f, self.__e = size, d, f, identity_factory
if initial_values:
for i, v in enumerate(initial_values):
d[size + i] = v
for i in range(size - 1, 0, -1):
d[i] = f(d[i << 1], d[i << 1 | 1])
def get_val(self, index):
return self.__d[index + self.__n]
def set_val(self, index, new_value):
i, d, f = index + self.__n, self.__d, self.__f
if d[i] == new_value:
return
d[i], i = new_value, i >> 1
while i:
d[i], i = f(d[i << 1], d[i << 1 | 1]), i >> 1
def modify(self, index, value):
self.set_val(index, self.__f(self.__d[index + self.__n], value))
def query(self, from_inclusive, to_exclusive):
ans = self.__e()
if to_exclusive <= from_inclusive:
return ans
l, r, d, f = (
from_inclusive + self.__n,
to_exclusive + self.__n,
self.__d,
self.__f,
)
while l < r:
if l & 1:
ans, l = f(ans, d[l]), l + 1
if r & 1:
ans, r = f(d[r - 1], ans), r - 1
l, r = l >> 1, r >> 1
return ans
def bisect_left(self, func):
"""func()がFalseになるもっとも左のindexを探す"""
i, j, n, f, d, v = (
self.__n,
self.__n + self.__n,
self.__n,
self.__f,
self.__d,
self.__e(),
)
while i < j:
if i & 1:
nv = f(v, d[i])
if not func(nv):
break
v, i = nv, i + 1
i, j = i >> 1, j >> 1
while i < n:
nv = f(v, d[i << 1])
if func(nv):
v, i = nv, i << 1 | 1
else:
i = i << 1
return i - n
if __name__ == "__main__":
input = sys.stdin.readline
N = int(input())
(*A,) = map(int, input().split())
main(N, A)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s203779470 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | class Node:
def __init__(self, key):
self.key = key
self.lch = None
self.rch = None
self.bias = 0
self.size = 1
class AVLTree:
def __init__(self):
self.root = None
def rotate_left(self, v):
u = v.rch
u.size = v.size
v.size -= u.rch.size + 1 if u.rch is not None else 1
v.rch = u.lch
u.lch = v
if u.bias == -1:
u.bias = v.bias = 0
else:
u.bias = 1
v.bias = -1
return u
def rotate_right(self, v):
u = v.lch
u.size = v.size
v.size -= u.lch.size + 1 if u.lch is not None else 1
v.lch = u.rch
u.rch = v
if u.bias == 1:
u.bias = v.bias = 0
else:
u.bias = -1
v.bias = 1
return u
def rotateLR(self, v):
u = v.lch
t = u.rch
t.size = v.size
v.size -= u.size - (t.rch.size if t.rch is not None else 0)
u.size -= t.rch.size + 1 if t.rch is not None else 1
u.rch = t.lch
t.lch = u
v.lch = t.rch
t.rch = v
self.update_bias_double(t)
return t
def rotateRL(self, v):
u = v.rch
t = u.lch
t.size = v.size
v.size -= u.size - (t.lch.size if t.lch is not None else 0)
u.size -= t.lch.size + 1 if t.lch is not None else 1
u.lch = t.rch
t.rch = u
v.rch = t.lch
t.lch = v
self.update_bias_double(t)
return t
def update_bias_double(self, v):
if v.bias == 1:
v.rch.bias = -1
v.lch.bias = 0
elif v.bias == -1:
v.rch.bias = 0
v.lch.bias = 1
else:
v.rch.bias = 0
v.lch.bias = 0
v.bias = 0
def add(self, key):
if self.root is None:
self.root = Node(key)
return
v = self.root
history = []
while v is not None:
if key < v.key:
history.append((v, 1))
v = v.lch
elif v.key < key:
history.append((v, -1))
v = v.rch
else:
return
p, pdir = history[-1]
if pdir == 1:
p.lch = Node(key)
else:
p.rch = Node(key)
while history:
v, direction = history.pop()
v.bias += direction
v.size += 1
new_v = None
b = v.bias
if b == 0:
break
if b == 2:
u = v.lch
if u.bias == -1:
new_v = self.rotateLR(v)
else:
new_v = self.rotate_right(v)
break
if b == -2:
u = v.rch
if u.bias == 1:
new_v = self.rotateRL(v)
else:
new_v = self.rotate_left(v)
break
if new_v is not None:
if len(history) == 0:
self.root = new_v
return
p, pdir = history.pop()
p.size += 1
if pdir == 1:
p.lch = new_v
else:
p.rch = new_v
while history:
p, pdir = history.pop()
p.size += 1
def remove(self, key):
v = self.root
history = []
while v is not None:
if key < v.key:
history.append((v, 1))
v = v.lch
elif v.key < key:
history.append((v, -1))
v = v.rch
else:
break
else:
return False
if v.lch is not None:
history.append((v, 1))
lmax = v.lch
while lmax.rch is not None:
history.append((lmax, -1))
lmax = lmax.rch
v.key = lmax.key
v = lmax
c = v.rch if v.lch is None else v.lch
if history:
p, pdir = history[-1]
if pdir == 1:
p.lch = c
else:
p.rch = c
else:
self.root = c
return True
while history:
new_p = None
p, pdir = history.pop()
p.bias -= pdir
p.size -= 1
b = p.bias
if b == 2:
if p.lch.bias == -1:
new_p = self.rotateLR(p)
else:
new_p = self.rotate_right(p)
elif b == -2:
if p.rch.bias == 1:
new_p = self.rotateRL(p)
else:
new_p = self.rotate_left(p)
elif b != 0:
break
if new_p is not None:
if len(history) == 0:
self.root = new_p
return True
gp, gpdir = history[-1]
if gpdir == 1:
gp.lch = new_p
else:
gp.rch = new_p
if new_p.bias != 0:
break
while history:
p, pdir = history.pop()
p.size -= 1
return True
def member(self, key):
v = self.root
while v is not None:
if key < v.key:
v = v.lch
elif v.key < key:
v = v.rch
else:
return True
return False
def lower_bound(self, key):
ret = None
v = self.root
while v is not None:
if v.key >= key:
if ret is None or ret > v.key:
ret = v.key
v = v.lch
else:
v = v.rch
return ret
def upper_bound(self, key):
ret = None
v = self.root
while v is not None:
if v.key <= key:
if ret is None or ret < v.key:
ret = v.key
v = v.rch
else:
v = v.lch
return ret
def find_kth_element(self, k):
v = self.root
s = 0
while v is not None:
t = s + v.lch.size if v.lch is not None else s
if t == k:
return v.key
elif t < k:
s = t + 1
v = v.rch
else:
v = v.lch
return None
def __contains__(self, key):
return self.member(key)
def __delitem__(self, key):
return self.remove(key)
def __bool__(self):
return self.root is not None
def __len__(self):
return self.root.size if self.root is not None else 0
if __name__ == "__main__":
N = int(input())
A = [(a, i) for i, a in enumerate(map(int, input().split()))]
A.sort(reverse=True)
T = AVLTree()
T.add(-1)
T.add(N)
ans = 0
while A:
a, i = A.pop()
l = T.upper_bound(i)
r = T.lower_bound(i)
ans += a * (r - i) * (i - l)
T.add(i)
print(ans)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s229172282 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | import random
import time
# Treap
# d = 0: right rotation
# d = 1: left rotation
def rotate(nd, d):
c = nd[d]
if d:
e = c[1]
nd[1] = c[0]
c[0] = nd
else:
e = c[0]
nd[0] = c[1]
c[1] = nd
r = c[4] = nd[4]
nd[4] = r - (e[4] if e else 0) - 1
return c
# insert a node with key = val and priority = pri
root = None
def insert(val, pri):
global root
st = []
dr = []
x = root
while x:
st.append(x)
if x[2] == val:
return
d = x[2] < val
dr.append(d)
x = x[d]
# [<left>, <right>, <key>, <priority>, <count>]
nd = [None, None, val, pri, 1]
while st:
x = st.pop()
d = dr.pop()
x[d] = nd
x[4] += 1
if x[3] >= nd[3]:
break
rotate(x, d)
else:
root = nd
for x in st:
x[4] += 1
def __delete(nd):
st = []
dr = []
while nd[0] or nd[1]:
l = nd[0]
r = nd[1]
d = (l[3] <= r[3]) if l and r else (l is None)
st.append(rotate(nd, d))
dr.append(d ^ 1)
nd = x = None
while st:
nd = x
x = st.pop()
d = dr.pop()
x[d] = nd
x[4] -= 1
return x
def delete(val):
global root
x = root
st = []
y = None
while x:
if val == x[2]:
break
y = x
d = x[2] < val
st.append(y)
x = x[d]
else:
return
if y:
y[d] = __delete(x)
for x in st:
x[4] -= 1
else:
root = __delete(x)
def find(val):
global root
x = root
while x:
if val == x[2]:
return 1
x = x[x[2] < val]
return 0
def search1(val):
global root
x = root
ans = -1
while x:
if val == x[2]:
return val
if val > x[2]:
ans = max(ans, x[2])
if x[1] == None:
return ans
else:
x = x[1]
if val < x[2]:
if x[0] == None:
return ans
else:
x = x[0]
return ans
def search2(val):
global root
x = root
ans = 10**20
while x:
if val == x[2]:
return val
if val > x[2]:
if x[1] == None:
return ans
else:
x = x[1]
if val < x[2]:
ans = min(ans, x[2])
if x[0] == None:
return ans
else:
x = x[0]
return ans
N = int(input())
A = list(map(int, input().split()))
A = [A[i] - 1 for i in range(0, N)]
function = [0 for i in range(0, N)]
for i in range(0, N):
function[A[i]] = i
random.seed()
randint = random.randint
insert(function[0], randint(1, 200000))
data1 = [0 for i in range(0, N)]
data2 = [0 for i in range(0, N)]
data1[0] = -1
data2[0] = N
for i in range(1, N):
if search1(function[i]) != -1:
data1[i] = search1(function[i])
else:
data1[i] = -1
if search2(function[i]) != 10**20:
data2[i] = search2(function[i])
else:
data2[i] = N
insert(function[i], randint(1, 200000))
ans = 0
for i in range(0, N):
prod = (function[i] - data1[i]) * (data2[i] - function[i])
ans += prod * (i + 1)
print(ans)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s768051347 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | n = int(input())
p = list(map(int, input().split()))
dn = {x: i for i, x in enumerate(p)}
#####segfunc#####
def segfunc(x, y):
return x + y
#################
#####ide_ele#####
ide_ele = 0
#################
class SegTree:
"""
init(init_val, ide_ele): 配列init_valで初期化 O(N)
update(k, x): k番目の値をxに更新 O(logN)
query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
"""
def __init__(self, init_val, segfunc=segfunc, ide_ele=ide_ele):
"""
init_val: 配列の初期値
segfunc: 区間にしたい操作
ide_ele: 単位元
n: 要素数
num: n以上の最小の2のべき乗
tree: セグメント木(1-index)
"""
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = ide_ele
self.num = 1 << (n - 1).bit_length()
self.tree = [ide_ele] * 2 * self.num
# 配列の値を葉にセット
for i in range(n):
self.tree[self.num + i] = init_val[i]
# 構築していく
for i in range(self.num - 1, 0, -1):
self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
def update(self, k, x):
"""
k番目の値をxに更新
k: index(0-index)
x: update value
"""
k += self.num
self.tree[k] = x
while k > 1:
self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
k >>= 1
def query(self, l, r):
"""
[l, r)のsegfuncしたものを得る
l: index(0-index)
r: index(0-index)
"""
res = self.ide_ele
l += self.num
r += self.num
while l < r:
if l & 1:
res = self.segfunc(res, self.tree[l])
l += 1
if r & 1:
res = self.segfunc(res, self.tree[r - 1])
l >>= 1
r >>= 1
return res
st = SegTree([0] * n)
ans = 0
for k in range(1, n + 1):
i = dn[k]
# idx=iを含み、区間和が0になる最長の区間を調べる。
l, r = 0, i
while r - l > 1:
x = (l + r) // 2
if st.query(x, i + 1) > 0:
l, r = x, r
else:
l, r = l, x
l0 = l if st.query(l, i + 1) == 0 else r
l, r = i, n - 1
while r - l > 1:
x = (l + r) // 2
if st.query(i, x + 1) > 0:
l, r = l, x
else:
l, r = x, r
r0 = r if st.query(i, r + 1) == 0 else l
ans += k * (i - l0 + 1) * (r0 - i + 1)
st.update(i, 1)
print(ans)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s424459953 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | import sys
from bisect import bisect
class Node:
def __init__(self, key, height):
self.key = key # ノードの木
self.height = height # このノードを根とする部分木の高さ
self.left = None
self.right = None
def size(self, n):
return 0 if n is None else n.height
def bias(self): # 左の方が高いと正、右が高いと負の値を返す
return self.size(self.left) - self.size(self.right)
# 木の高さの計算
def calcSize(self):
self.height = 1 + max(self.size(self.left), self.size(self.right))
class AVLTree:
def __init__(self):
self.root = None # 根
self.change = False # 修正フラグ
###############
# 回転操作, 修正操作
###############
def rotateL(self, n): # ノードnの左回転
r = n.right
rl = r.left
r.left = n
n.right = rl
r.left.calcSize()
r.calcSize()
return r
def rotateR(self, n):
l = n.left
lr = l.right
l.right = n
n.left = lr
l.right.calcSize()
l.calcSize()
return l
def rotateLR(self, n): # 二重回転;左回転→右回転
n.left = self.rotateL(n.left)
return self.rotateR(n)
def rotateRL(self, n):
n.right = self.rotateR(n.right)
return self.rotateL(n)
def balanceL(self, n):
if not self.change:
return n
h = n.height
if n.bias() == 2:
if n.left.bias() >= 0:
n = self.rotateR(n)
else:
n = self.rotateLR(n)
else:
n.calcSize()
self.change = h != n.height
return n
def balanceR(self, n):
if not self.change:
return n
h = n.height
if n.bias() == -2:
if n.right.bias() <= 0:
n = self.rotateL(n)
else:
n = self.rotateRL(n)
else:
n.calcSize()
self.change = h != n.height
return n
###############
# Nodeの追加
###############
def insert(self, key):
self.root = self.insert_sub(self.root, key)
def insert_sub(self, t, key): # 新たなノードの挿入。初期位置は根。
if t is None:
self.change = True
return Node(key, 1)
if key < t.key:
t.left = self.insert_sub(t.left, key)
return self.balanceL(t)
elif key > t.key:
t.right = self.insert_sub(t.right, key)
return self.balanceR(t)
else:
self.change = False
return t
###############
# Nodeの探索
###############
def search(self, key, lower, higher):
t = self.root
lb, hb = lower, higher
while t:
if key < t.key:
hb = t.key
t = t.left
else:
lb = t.key
t = t.right
return lb, hb
def solve():
input = sys.stdin.readline
N = int(input())
P = [int(p) for p in input().split()]
pindex = dict()
for i, p in enumerate(P):
pindex[p - 1] = i
Ans = (pindex[0] + 1) * (N - pindex[0])
T = AVLTree()
T.insert(pindex[0])
for i in range(1, N):
pid = pindex[i]
l, r = T.search(pid, -1, N)
Ans += (i + 1) * (pid - l) * (r - pid)
T.insert(pid)
print(Ans)
return 0
if __name__ == "__main__":
solve()
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s577755270 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | # -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2**62 - 1
def read_int():
return int(buff_readline())
def read_int_n():
return list(map(int, buff_readline().split()))
def read_float():
return float(buff_readline())
def read_float_n():
return list(map(float, buff_readline().split()))
def read_str():
return readline().strip()
def read_str_n():
return readline().strip().split()
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, "sec")
return ret
return wrap
class SegmentTree:
def __init__(self, array, operator, identity_element):
_len = len(array)
self.__op = operator
self.__size = 1 << (_len - 1).bit_length()
self.__tree = (
[identity_element] * self.__size
+ array
+ [identity_element] * (self.__size - _len)
)
self.__ie = identity_element
for i in range(self.__size - 1, 0, -1):
self.__tree[i] = operator(self.__tree[i * 2], self.__tree[i * 2 + 1])
def update(self, i, v):
i += self.__size
self.__tree[i] = v
while i:
i //= 2
self.__tree[i] = self.__op(self.__tree[i * 2], self.__tree[i * 2 + 1])
def query(self, l, r):
"""[l, r)"""
l += self.__size
r += self.__size
ret = self.__ie
while l < r:
if l & 1:
ret = self.__op(ret, self.__tree[l])
l += 1
if r & 1:
r -= 1
ret = self.__op(ret, self.__tree[r])
l //= 2
r //= 2
return ret
@mt
def slv(N, A):
ai = [(a, i) for i, a in enumerate(A)]
ai.sort()
lst = SegmentTree([-1] * N, max, -1)
rst = SegmentTree([N] * N, min, N)
ans = 0
for a, i in ai:
l = lst.query(0, i)
r = rst.query(i + 1, N)
ans += (i - l) * (r - i) * a
lst.update(i, i)
rst.update(i, i)
return ans
def main():
N = read_int()
A = read_int_n()
print(slv(N, A))
if __name__ == "__main__":
main()
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s956326264 | Wrong Answer | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect
def read():
return int(input())
def reads():
return [int(x) for x in input().split()]
setrecursionlimit(1000000)
N = read()
a = [x - 1 for x in reads()]
b = [0] * N
for i in range(N):
b[a[i]] = i
ps = [-1, N]
res = 0
for i in range(N):
p = bisect(ps, b[i])
print((ps[p] - b[i]), (b[i] - ps[p - 1]), (i + 1))
res += (ps[p] - b[i]) * (b[i] - ps[p - 1]) * (i + 1)
ps.insert(p, b[i])
print(res)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s318678426 | Wrong Answer | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | N = int(input())
A = list(map(int, input().split()))
idx = [0] * N
for i, a in enumerate(A):
idx[a - 1] = i
inf = N + 1
L = 1
while L < N:
L *= 2
A = A + (L - len(A)) * [inf]
seg = [inf] * (2 * L - 1)
K = 2 * L - 1
def renew(k, l, r):
global seg
if K - L <= k < K:
seg[k] = A[k - L + 1]
return
else:
renew(2 * k + 1, l, (l + r) // 2)
renew(2 * k + 2, (l + r) // 2, r)
seg[k] = min(seg[2 * k + 1], seg[2 * k + 2])
return
renew(0, 0, L)
def up(i, x, k, l, r):
if seg[(k - 1) // 2] == x:
if k == 0:
return k, l, r
if k % 2 == 1:
r = r + r - l
elif k % 2 == 0:
l = l - r + l
return up(i, x, (k - 1) // 2, l, r)
else:
return k, l, r
def downleft(i, x, k, l, r, b, e):
if seg[k] < x:
if K - L <= k < K:
return b, e
if seg[2 * k + 2] < x:
b, e = downleft(i, x, 2 * k + 2, (l + r) // 2, r, b, e)
return b, e
else:
b, e = min(b, (l + r) // 2), e
if seg[2 * k + 1] < x:
b, e = downleft(i, x, 2 * k + 1, l, (l + r) // 2, b, e)
return b, e
elif seg[k] > x:
return min(b, l), e
return b, e
def downright(i, x, k, l, r, b, e):
if seg[k] < x:
if K - L <= k < K:
return b, e
if seg[2 * k + 1] < x:
b, e = downright(i, x, 2 * k + 1, l, (l + r) // 2, b, e)
return b, e
else:
b, e = b, max((l + r) // 2, e)
if seg[2 * k + 2] < x:
b, e = downright(i, x, 2 * k + 2, (l + r) // 2, r, b, e)
return b, e
elif seg[k] > x:
return b, max(r, e)
return b, e
ans = 0
for i in range(N):
k, l, r = up(idx[i], i + 1, idx[i] + L - 1, idx[i], idx[i] + 1)
if l > 0:
bl, br = downleft(idx[i], i + 1, k - 1, l - r + l, l, l, l)
else:
bl = l
if r < L:
el, er = downright(idx[i], i + 1, k + 1, r, r + r - l, r, r)
else:
er = L
er = min(er, N)
ans += (i + 1) * (idx[i] - bl + 1) * (er - idx[i])
print(ans)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s538998816 | Runtime Error | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | # -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
sys.setrecursionlimit(10000)
def read_int():
return int(input())
def read_int_n():
return list(map(int, input().split()))
def read_float():
return float(input())
def read_float_n():
return list(map(float, input().split()))
def read_str():
return input().strip()
def read_str_n():
return list(map(str, input().split()))
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, "sec")
return ret
return wrap
@mt
def slv(N, A):
import numpy as np
A = np.array(A)
def f(A):
n = len(A)
if not n:
return 0
if n == 1:
return A[0]
i = np.argmin(A)
return A[i] * (i + 1) * (n - i) + f(A[:i]) + f(A[i + 1 :])
return f(A)
def main():
N = read_int()
A = read_int_n()
print(slv(N, A))
if __name__ == "__main__":
main()
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s441079148 | Runtime Error | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | def getlist():
return list(map(int, input().split()))
class Node:
def __init__(self, key, parent=None, left=None, right=None):
self.key = key
self.p = parent
self.left = left
self.right = right
def __repr__(self):
return "Node(%s)" % repr(self.key)
def search(self, k):
if self is None or k == self.key:
return self
if k < self.key:
return self.left.search(k)
else:
return self.right.search(k)
def minimum(self):
minimum = self
while minimum.left:
minimum = minimum.left
return minimum
def maximum(self):
while self.right:
self = self.right
return self
# 次節点
def successor(self):
if self.right:
return self.right.minimum()
y = self.p
while y and self == y.right:
self = y
y = y.p
return y
# 前節点
def predecessor(self):
if self.left:
return self.left.maximum()
y = self.p
while y and self == y.left:
self = y
y = y.p
return y
# 二分探索木のルートを返すメソッド
def root(self):
while self.p:
self = self.p
return self
# 挿入
def insert(self, value):
z = Node(value)
y = None
self = self.root()
while self:
y = self
if z.key < self.key:
self = self.left
else:
self = self.right
z.p = y
if y is None:
pass
elif z.key < y.key:
y.left = z
else:
y.right = z
N = int(input())
a = getlist()
l = [0] * (N + 1)
for i in range(1, N + 1):
l[a[i - 1]] = i
ans = 0
DD = Node(N + 1)
DD.insert(0)
for i in range(1, N + 1):
val = l[i]
DD.insert(val)
ans += (
i
* (DD.search(val).successor().key - val)
* (val - DD.search(val).predecessor().key)
)
print(ans)
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the answer.
Note that the answer may not fit into a 32-bit integer.
* * * | s687013582 | Accepted | p03987 | The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N | def main():
n = int(input())
a = list(map(int, input().split()))
a = sorted(list(enumerate(a)), key=lambda x: -x[1])
class unionfind:
# size:要素数,tree:unionfind木
def __init__(self, size): # self,要素数
self.size = size
self.tree = [i for i in range(self.size)] # root,depth
# rootを探す
def root(self, index):
temp_list = []
temp = self.tree[index]
while index != temp:
temp_list.append(index)
index = temp
temp = self.tree[index]
for i in temp_list:
self.tree[i] = index
return index
# 結合
def unite_r(self, index1, index2):
r1 = self.root(index1)
r2 = self.root(index2)
if r1 < r2:
self.tree[r1] = r2
else:
self.tree[r2] = r1
def unite_l(self, index1, index2):
r1 = self.root(index1)
r2 = self.root(index2)
if r1 > r2:
self.tree[r1] = r2
else:
self.tree[r2] = r1
# 同じか判定
def same(self, index1, index2):
r1 = self.root(index1)
r2 = self.root(index2)
return r1 == r2
ur = unionfind(n + 2)
ul = unionfind(n + 2)
vis = [False] * (n + 2)
ans = 0
for i, j in a:
vis[i + 1] = True
if vis[i + 2]:
ur.unite_r(i + 1, i + 2)
ul.unite_l(i + 1, i + 2)
if vis[i]:
ur.unite_r(i, i + 1)
ul.unite_l(i, i + 1)
ans += j * (ur.root(i + 1) - i) * (i + 2 - ul.root(i + 1))
print(ans)
main()
| Statement
One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
his friend.
Find the following:
 | [{"input": "3\n 2 1 3", "output": "9\n \n\n* * *"}, {"input": "4\n 1 3 2 4", "output": "19\n \n\n* * *"}, {"input": "8\n 5 4 8 1 2 6 7 3", "output": "85"}] |
Print the maximum possible score of A.
* * * | s236603047 | Accepted | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | N, M, Q = map(int, (input().split()))
A = []
if N == 10:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
for a3 in range(a2, M + 1):
for a4 in range(a3, M + 1):
for a5 in range(a4, M + 1):
for a6 in range(a5, M + 1):
for a7 in range(a6, M + 1):
for a8 in range(a7, M + 1):
for a9 in range(a8, M + 1):
for a10 in range(a9, M + 1):
A.append(
[
a1,
a2,
a3,
a4,
a5,
a6,
a7,
a8,
a9,
a10,
]
)
if N == 9:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
for a3 in range(a2, M + 1):
for a4 in range(a3, M + 1):
for a5 in range(a4, M + 1):
for a6 in range(a5, M + 1):
for a7 in range(a6, M + 1):
for a8 in range(a7, M + 1):
for a9 in range(a8, M + 1):
A.append([a1, a2, a3, a4, a5, a6, a7, a8, a9])
if N == 8:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
for a3 in range(a2, M + 1):
for a4 in range(a3, M + 1):
for a5 in range(a4, M + 1):
for a6 in range(a5, M + 1):
for a7 in range(a6, M + 1):
for a8 in range(a7, M + 1):
A.append([a1, a2, a3, a4, a5, a6, a7, a8])
if N == 7:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
for a3 in range(a2, M + 1):
for a4 in range(a3, M + 1):
for a5 in range(a4, M + 1):
for a6 in range(a5, M + 1):
for a7 in range(a6, M + 1):
A.append([a1, a2, a3, a4, a5, a6, a7])
if N == 6:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
for a3 in range(a2, M + 1):
for a4 in range(a3, M + 1):
for a5 in range(a4, M + 1):
for a6 in range(a5, M + 1):
A.append([a1, a2, a3, a4, a5, a6])
if N == 5:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
for a3 in range(a2, M + 1):
for a4 in range(a3, M + 1):
for a5 in range(a4, M + 1):
A.append([a1, a2, a3, a4, a5])
if N == 4:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
for a3 in range(a2, M + 1):
for a4 in range(a3, M + 1):
A.append([a1, a2, a3, a4])
if N == 3:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
for a3 in range(a2, M + 1):
A.append([a1, a2, a3])
if N == 2:
for a1 in range(1, M + 1):
for a2 in range(a1, M + 1):
A.append([a1, a2])
ans = 0
abcd = []
for i in range(Q):
abc = list(map(int, (input().split())))
abcd.append(abc)
dd = []
for i in range(len(A)):
d = 0
for j in range(Q):
if A[i][abcd[j][1] - 1] - A[i][abcd[j][0] - 1] == abcd[j][2]:
d += abcd[j][3]
dd.append(d)
print(str(max(dd)))
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s996199646 | Wrong Answer | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | import sys
sys.setrecursionlimit(700000)
def s_in():
return input()
def n_in():
return int(input())
def l_in():
return list(map(int, input().split()))
def print_l(l):
print(" ".join(map(str, l)))
class Interval:
def __init__(self, li):
self.li = li
self.n = len(li)
self.sum_li = [li[0]]
for i in range(1, self.n):
self.sum_li.append(self.sum_li[i - 1] + li[i])
def sum(self, a, b=None):
if b is None:
return self.sum(0, a)
res = self.sum_li[min(self.n - 1, b - 1)]
if a > 0:
res -= self.sum_li[a - 1]
return res
n, m, q = l_in()
abcd = []
for _ in range(q):
abcd.append(l_in())
s = []
def check(s):
return sum(d for a, b, c, d in abcd if s[b - 1] - s[a - 1] == c)
def search(s, current):
tmp = 0
for d in range(current, m + 1):
s.append(d)
if len(s) == n:
tmp = max(tmp, check(s))
else:
tmp = max(tmp, search(s, d))
s.pop()
return tmp
print(search(s, 0))
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s165760160 | Runtime Error | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | N, M, Q = map(int, input().split())
a = [0 for _ in range(Q + 1)]
b = [0 for _ in range(Q + 1)]
c = [0 for _ in range(Q + 1)]
d = [0 for _ in range(Q + 1)]
for i in range(1, Q + 1):
a[i], b[i], c[i], d[i] = map(int, input().split())
def asc(x):
y = sorted(x)
return list(x) == y
def cond(p, i):
if asc(p):
try:
if p[b[i]] - p[a[i]] == c[i]:
# print(p)
# print('{}-{}={} | d={}'.format(p[a[i]],p[b[i]],c[i],d[i]))
return d[i]
except IndexError:
return 0
return 0
import itertools
lisM = list(range(1, M + 1))
print(lisM)
p = itertools.permutations(lisM, N + 1)
D = []
for x in p:
for i in range(1, Q + 1):
D += [cond(x, i)]
print(max(D))
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s023071611 | Accepted | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | def examA():
K = I()
A, B = LI()
for i in range(A, B + 1):
if i % K == 0:
print("OK")
return
print("NG")
return
def examB():
X = I()
now = 100
ans = 0
while now < X:
ans += 1
now += now // 100
print(ans)
return
def examC():
N, M, Q = LI()
A = [LI() for _ in range(Q)]
A.sort()
dp = [[0] * M for _ in range(N)]
for i in range(Q):
a, b, c, d = A[i]
a -= 1
b -= 1
for j in range(M):
dp[b][j] = max(dp[b][j], 0)
ans = 0
print(ans)
return
# 反省して下さい。
def examC2():
N, M, Q = LI()
A = [LI() for _ in range(Q)]
C = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# rint(C)
ans = 0
for L in C:
cur = 0
for a, b, c, d in A:
if L[b - 1] - L[a - 1] == c:
cur += d
ans = max(cur, ans)
print(ans)
return
def examD():
A, B, N = LI()
if N >= B - 1:
ans = (A * (B - 1)) // B
else:
ans = (A * N) // B
print(ans)
return
def examE():
N, M = LI()
ans = []
if N % 2 == 1:
for i in range(N // 2):
ans.append((i + 1, N - i))
else:
flag = False
for i in range(N // 2):
if N - i - (i + 1) <= N // 2:
flag = True
if flag:
ans.append((i + 1, N - i - 1))
else:
ans.append((i + 1, N - i))
# print(ans)
for v in ans[:M]:
print(" ".join(map(str, v)))
return
def examF():
N = I()
A = LI()
V = [[] for _ in range(N)]
for _ in range(N - 1):
u, v = LI()
u -= 1
v -= 1
V[u].append(v)
V[v].append(u)
dp = [-1] * N
dp[0] = 1
LIS = deque()
LIS.append(A[0])
def dfs(s):
# print(LIS)
for i in V[s]:
if dp[i] != -1:
continue
flag = []
if A[i] > LIS[-1]:
LIS.append(A[i])
flag = False
else:
flag = [
bisect.bisect_left(LIS, A[i]),
LIS[bisect.bisect_left(LIS, A[i])],
]
LIS[flag[0]] = A[i]
dp[i] = len(LIS)
dfs(i)
# print(flag)
if not flag:
LIS.pop()
else:
LIS[flag[0]] = flag[1]
return
dfs(0)
ans = dp
for v in ans:
print(v)
return
import sys, bisect, itertools, heapq, math, random
from copy import deepcopy
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I():
return int(readline())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LSI():
return list(map(str, sys.stdin.readline().split()))
def LS():
return sys.stdin.readline().split()
def SI():
return sys.stdin.readline().strip()
global mod, mod2, inf, alphabet, _ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10 ** (-12)
alphabet = [chr(ord("a") + i) for i in range(26)]
sys.setrecursionlimit(10**7)
if __name__ == "__main__":
examC2()
"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s098695284 | Accepted | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | import glob
# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = "ABC\\165\\C"
# テスト用ファイル置き場のトップ
TOP_PATH = "C:\\AtCoder"
class Common:
problem = []
index = 0
def __init__(self, rel_path):
self.rel_path = rel_path
def initialize(self, path):
file = open(path)
self.problem = file.readlines()
self.index = 0
return
def input_data(self):
try:
IS_TEST
self.index += 1
return self.problem[self.index - 1]
except NameError:
return input()
def resolve(self):
pass
def exec_resolve(self):
try:
IS_TEST
for path in glob.glob(TOP_PATH + "\\" + self.rel_path + "/*.txt"):
print("Test: " + path)
self.initialize(path)
self.resolve()
print("\n\n")
except NameError:
self.resolve()
class C(Common):
def rec(self, A, now_index):
point1 = 0
point2 = 0
if now_index == self.N - 1:
for i in self.abcd:
if A[i[1] - 1] - A[i[0] - 1] == i[2]:
point1 += i[3]
else:
for i in range(A[now_index], self.M + 1):
A[now_index + 1] = i
tmp = self.rec(A, now_index + 1)
if tmp > point2:
point2 = tmp
if point1 > point2:
return point1
return point2
def resolve(self):
self.N, self.M, self.Q = map(int, self.input_data().split())
self.abcd = []
for i in range(self.Q):
self.abcd.append(list(map(int, self.input_data().split())))
A = [0] * self.N
A[0] = 1
point = self.rec(A, 0)
print(str(point))
solver = C(REL_PATH)
solver.exec_resolve()
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s901840697 | Wrong Answer | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
N, M, Q = lr()
ans = 0
rest = []
tmp = 0
for i in range(Q):
rest.append(lr())
rest = deque(sorted(rest, key=lambda x: (x[1], x[0]))[::-1])
while len(rest) > 0:
a, b, c, d = rest.popleft()
Nr = len(rest)
A = [M] * N
bi = [0] * N
tmp = 0
t = M - c
r = A[a - 1] - t
if t >= 1 and r >= 0:
for j in range(a):
A[j] = t
tmp += d
bi[b - 1] = -1
bi[a - 1] = 1
for i in range(Nr):
a, b, c, d = rest[i]
t = A[b - 1] - c
r = A[a - 1] - t
if t >= 1 and r >= 0:
if not (sum(bi[:a]) and r > 0):
bi[b - 1] = -1
bi[a - 1] = 1
for j in range(a):
A[j] -= r
tmp += d
ans = max(tmp, ans)
print(ans)
resolve()
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s367141085 | Accepted | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
n, m, q = map(int, input().split())
# print(n,m,q)
a = [[int(x) for x in input().split()] for _ in range(q)]
# print(a)
ans = 0
import itertools
for b in itertools.combinations_with_replacement([i for i in range(1, m + 1)], n):
cnt = 0
for v in a:
if b[v[1] - 1] - b[v[0] - 1] == v[2]:
cnt += v[3]
ans = max(ans, cnt)
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)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s531180599 | Wrong Answer | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | A, B, N = map(int, input().split())
if B > N:
res = A * N // B - A * (N // B)
else:
N = N - N % B - 1
res = A * N // B - A * (N // B)
print(res)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s600713717 | Wrong Answer | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | a = list(map(int, input().split()))
b = []
c = []
out = 0
for i in range(a[2]):
b.append(list(map(int, input().split())))
for i in range(a[0]):
c.append(1)
for i in range(a[0]):
for j in range(a[1] + 1):
d = 0
for k in range(a[2]):
# print('i:'+str(i)+' j:'+str(j)+' k:'+str(k))
# print(c)
if c[b[k][1] - 1] - c[b[k][0] - 1] == b[k][2]:
d += b[k][3]
if d > out:
out = d
if j + 1 > a[1]:
c[a[0] - 1 - i] = j
else:
c[a[0] - 1 - i] = j + 1
print(out)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s187112020 | Runtime Error | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | # 166d
x = int(input())
for i in range(-73, 73):
for j in range(-73, 73):
print(i, j)
if i**5 - j**5 == x:
a = i
b = j
break
print(a, b)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s799772216 | Accepted | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | n, m, q = list(map(lambda x: int(x), input().split(" ")))
e = []
for i in range(q):
s = list(map(lambda x: int(x), input().split(" ")))
e.append(s)
def helper(l, tmp, n, all):
if len(tmp) == n:
r = tmp.copy()
all.append(r)
return
for i, v in enumerate(l):
tmp.append(v)
helper(l[i:], tmp, n, all)
tmp.pop()
candidates = []
helper(list(range(1, m + 1)), [], n, candidates)
max_val = 0
for c in candidates:
sum = 0
for j in e:
if c[j[1] - 1] - c[j[0] - 1] == j[2]:
sum += j[3]
max_val = max(max_val, sum)
print(max_val)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s251890648 | Wrong Answer | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | def solve(arr, abcd, Q):
ans = 0
for i in range(Q):
if arr[abcd[i][1] - 1] - arr[abcd[i][0] - 1] == abcd[i][2]:
ans += abcd[i][3]
return ans
N, M, Q = map(int, input().split())
abcd = []
for _ in range(Q):
tmp = list(map(int, input().split()))
abcd.append(tmp)
ans = 0
for i1 in range(1, M + 1):
for i2 in range(1, M + 1):
if i1 > i2:
continue
for i3 in range(1, M + 1):
if i2 > i3:
continue
if N == 3:
tmp = [i1, i2, i3]
tmp_ans = solve(tmp, abcd, Q)
ans = max(tmp_ans, ans)
for i4 in range(1, M + 1):
if i3 > i4:
continue
if N == 4:
tmp = [i1, i2, i3, i4]
tmp_ans = solve(tmp, abcd, Q)
ans = max(tmp_ans, ans)
for i5 in range(1, M + 1):
if i4 > i5:
continue
if N == 5:
tmp = [i1, i2, i3, i4, i5]
tmp_ans = solve(tmp, abcd, Q)
ans = max(tmp_ans, ans)
for i6 in range(1, M + 1):
if i5 > i6:
continue
if N == 6:
tmp = [i1, i2, i3, i4, i5, i6]
tmp_ans = solve(tmp, abcd, Q)
ans = max(tmp_ans, ans)
for i7 in range(1, M + 1):
if i6 > i7:
continue
if N == 7:
tmp = [i1, i2, i3, i4, i5, i6, i7]
tmp_ans = solve(tmp, abcd, Q)
ans = max(tmp_ans, ans)
for i8 in range(1, M + 1):
if i7 > i8:
continue
if N == 8:
tmp = [i1, i2, i3, i4, i5, i6, i7, i8]
tmp_ans = solve(tmp, abcd, Q)
ans = max(tmp_ans, ans)
for i9 in range(1, M + 1):
if i8 > i9:
continue
if N == 9:
tmp = [i1, i2, i3, i4, i5, i6, i7, i8, i9]
tmp_ans = solve(tmp, abcd, Q)
ans = max(tmp_ans, ans)
for i10 in range(1, M + 1):
if i9 > i10:
continue
if N == 10:
tmp = [
i1,
i2,
i3,
i4,
i5,
i6,
i7,
i8,
i9,
i10,
]
tmp_ans = solve(tmp, abcd, Q)
ans = max(tmp_ans, ans)
print(ans)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s543961738 | Accepted | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | N, M, Q = map(int, input().split())
S = [list(map(int, input().split())) for i in range(Q)]
A = []
if N == 10:
for a in range(1, M + 1):
for b in range(a, M + 1):
for c in range(b, M + 1):
for d in range(c, M + 1):
for e in range(d, M + 1):
for f in range(e, M + 1):
for g in range(f, M + 1):
for h in range(g, M + 1):
for i in range(h, M + 1):
for j in range(i, M + 1):
A.append([a, b, c, d, e, f, g, h, i, j])
elif N == 9:
for a in range(1, M + 1):
for b in range(a, M + 1):
for c in range(b, M + 1):
for d in range(c, M + 1):
for e in range(d, M + 1):
for f in range(e, M + 1):
for g in range(f, M + 1):
for h in range(g, M + 1):
for i in range(h, M + 1):
A.append([a, b, c, d, e, f, g, h, i])
elif N == 8:
for a in range(1, M + 1):
for b in range(a, M + 1):
for c in range(b, M + 1):
for d in range(c, M + 1):
for e in range(d, M + 1):
for f in range(e, M + 1):
for g in range(f, M + 1):
for h in range(g, M + 1):
A.append([a, b, c, d, e, f, g, h])
elif N == 7:
for a in range(1, M + 1):
for b in range(a, M + 1):
for c in range(b, M + 1):
for d in range(c, M + 1):
for e in range(d, M + 1):
for f in range(e, M + 1):
for g in range(f, M + 1):
A.append([a, b, c, d, e, f, g])
elif N == 6:
for a in range(1, M + 1):
for b in range(a, M + 1):
for c in range(b, M + 1):
for d in range(c, M + 1):
for e in range(d, M + 1):
for f in range(e, M + 1):
A.append([a, b, c, d, e, f])
elif N == 5:
for a in range(1, M + 1):
for b in range(a, M + 1):
for c in range(b, M + 1):
for d in range(c, M + 1):
for e in range(d, M + 1):
A.append([a, b, c, d, e])
elif N == 4:
for a in range(1, M + 1):
for b in range(a, M + 1):
for c in range(b, M + 1):
for d in range(c, M + 1):
A.append([a, b, c, d])
elif N == 3:
for a in range(1, M + 1):
for b in range(a, M + 1):
for c in range(b, M + 1):
A.append([a, b, c])
elif N == 2:
for a in range(1, M + 1):
for b in range(a, M + 1):
A.append([a, b])
d = [0] * len(A)
for s in range(len(A)):
for i in range(Q):
if A[s][S[i][1] - 1] - A[s][S[i][0] - 1] == S[i][2]:
d[s] = d[s] + S[i][3]
print(max(d))
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s825334583 | Runtime Error | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | N, N, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
abcd.sort()
total = 0
for i in range(2**Q):
now = str(format(i, "b")).zfill(Q)
A = [-1] * N
flag = 0
tmp = 0
for j in range(Q):
if now[j] == "1":
mi = abcd[j][0] - 1
ma = abcd[j][1] - 1
if A[mi] == -1:
A[mi] = 0
if A[ma] == -1:
A[ma] = A[mi] + abcd[j][2]
elif A[ma] != A[mi] + abcd[j][2]:
flag = 1
break
tmp += abcd[j][3]
if tmp > total:
total = tmp
print(total)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s928524669 | Accepted | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | n, m, p = map(int, input().split())
A = []
B = []
C = []
D = []
maximum = 0
for i in range(p):
a, b, c, d = map(int, input().split())
A.append(a)
B.append(b)
C.append(c)
D.append(d)
for z in range(m):
for y in range(z, m):
if n > 2:
for x in range(y, m):
if n > 3:
for w in range(x, m):
if n > 4:
for v in range(w, m):
if n > 5:
for u in range(v, m):
if n > 6:
for t in range(u, m):
if n > 7:
for s in range(t, m):
if n > 8:
for r in range(s, m):
if n > 9:
for q in range(
r, m
):
L = [
z,
y,
x,
w,
v,
u,
t,
s,
r,
q,
]
a = 0
for l in range(
p
):
if (
L[
B[l]
- 1
]
- L[
A[l]
- 1
]
== C[l]
):
a += D[
l
]
maximum = max(
maximum, a
)
else:
L = [
z,
y,
x,
w,
v,
u,
t,
s,
r,
]
a = 0
for l in range(p):
if (
L[B[l] - 1]
- L[
A[l] - 1
]
== C[l]
):
a += D[l]
maximum = max(
maximum, a
)
else:
L = [z, y, x, w, v, u, t, s]
a = 0
for l in range(p):
if (
L[B[l] - 1]
- L[A[l] - 1]
== C[l]
):
a += D[l]
maximum = max(maximum, a)
else:
L = [z, y, x, w, v, u, t]
a = 0
for l in range(p):
if (
L[B[l] - 1] - L[A[l] - 1]
== C[l]
):
a += D[l]
maximum = max(maximum, a)
else:
L = [z, y, x, w, v, u]
a = 0
for l in range(p):
if L[B[l] - 1] - L[A[l] - 1] == C[l]:
a += D[l]
maximum = max(maximum, a)
else:
L = [z, y, x, w, v]
a = 0
for l in range(p):
if L[B[l] - 1] - L[A[l] - 1] == C[l]:
a += D[l]
maximum = max(maximum, a)
else:
L = [z, y, x, w]
a = 0
for l in range(p):
if L[B[l] - 1] - L[A[l] - 1] == C[l]:
a += D[l]
maximum = max(maximum, a)
else:
L = [z, y, x]
a = 0
for l in range(p):
if L[B[l] - 1] - L[A[l] - 1] == C[l]:
a += D[l]
maximum = max(maximum, a)
else:
L = [z, y]
a = 0
for l in range(p):
if L[B[l] - 1] - L[A[l] - 1] == C[l]:
a += D[l]
maximum = max(maximum, a)
print(maximum)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s918260325 | Wrong Answer | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | N, M, Q = (int(x) for x in input().split())
ABCD = [0] * Q
for T in range(0, Q):
ABCD[T] = [int(x) for x in input().split()]
MaxP = 0
for A1 in range(0, M + 1):
for A2 in range(A1, M + 1):
if N <= 2:
Point = 0
A = [A1, A2]
for TT in range(0, Q):
if A[ABCD[TT][1] - 1] - A[ABCD[TT][0] - 1] == ABCD[TT][2]:
Point = Point + ABCD[TT][3]
if Point > MaxP:
MaxP = Point
else:
for A3 in range(A2, M + 1):
if N <= 3:
Point = 0
A = [A1, A2, A3]
for TT in range(0, Q):
if A[ABCD[TT][1] - 1] - A[ABCD[TT][0] - 1] == ABCD[TT][2]:
Point = Point + ABCD[TT][3]
if Point > MaxP:
MaxP = Point
else:
for A4 in range(A3, M + 1):
if N <= 4:
Point = 0
A = [A1, A2, A3, A4]
for TT in range(0, Q):
if (
A[ABCD[TT][1] - 1] - A[ABCD[TT][0] - 1]
== ABCD[TT][2]
):
Point = Point + ABCD[TT][3]
if Point > MaxP:
MaxP = Point
else:
for A5 in range(A4, M + 1):
if N <= 5:
Point = 0
A = [A1, A2, A3, A4, A5]
for TT in range(0, Q):
if (
A[ABCD[TT][1] - 1] - A[ABCD[TT][0] - 1]
== ABCD[TT][2]
):
Point = Point + ABCD[TT][3]
if Point > MaxP:
MaxP = Point
else:
for A6 in range(A5, M + 1):
if N <= 6:
Point = 0
A = [A1, A2, A3, A4, A5, A6]
for TT in range(0, Q):
if (
A[ABCD[TT][1] - 1]
- A[ABCD[TT][0] - 1]
== ABCD[TT][2]
):
Point = Point + ABCD[TT][3]
if Point > MaxP:
MaxP = Point
else:
for A7 in range(A6, M + 1):
if N <= 7:
Point = 0
A = [A1, A2, A3, A4, A5, A6, A7]
for TT in range(0, Q):
if (
A[ABCD[TT][1] - 1]
- A[ABCD[TT][0] - 1]
== ABCD[TT][2]
):
Point = Point + ABCD[TT][3]
if Point > MaxP:
MaxP = Point
else:
for A8 in range(A7, M + 1):
if N <= 8:
Point = 0
A = [
A1,
A2,
A3,
A4,
A5,
A6,
A7,
A8,
]
for TT in range(0, Q):
if (
A[ABCD[TT][1] - 1]
- A[ABCD[TT][0] - 1]
== ABCD[TT][2]
):
Point = (
Point
+ ABCD[TT][3]
)
if Point > MaxP:
MaxP = Point
else:
for A9 in range(A8, M + 1):
if N <= 9:
Point = 0
A = [
A1,
A2,
A3,
A4,
A5,
A6,
A7,
A8,
A9,
]
for TT in range(
0, Q
):
if (
A[
ABCD[
TT
][1]
- 1
]
- A[
ABCD[
TT
][0]
- 1
]
== ABCD[TT][
2
]
):
Point = (
Point
+ ABCD[
TT
][3]
)
if Point > MaxP:
MaxP = Point
else:
for A10 in range(
A9, M + 1
):
Point = 0
A = [
A1,
A2,
A3,
A4,
A5,
A6,
A7,
A8,
A9,
A10,
]
for TT in range(
0, Q
):
if (
A[
ABCD[
TT
][
1
]
- 1
]
- A[
ABCD[
TT
][
0
]
- 1
]
== ABCD[
TT
][2]
):
Point = (
Point
+ ABCD[
TT
][
3
]
)
if Point > MaxP:
MaxP = Point
print(MaxP)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the maximum possible score of A.
* * * | s548225851 | Accepted | p02695 | Input is given from Standard Input in the following format:
N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q | (N, M, Q) = map(int, input().split())
Qs = []
for i in range(Q):
Qs.append(list(map(int, input().split())))
A = [1] * N
score = 0
while 1:
if A[0] > M:
break
current_score = 0
for i in range(Q):
if A[Qs[i][1] - 1] - A[Qs[i][0] - 1] == Qs[i][2]:
current_score += Qs[i][3]
score = max(score, current_score)
for i in range(1, N):
if A[i] == M:
A[i - 1] += 1
for j in range(i, N):
A[j] = A[i - 1]
break
if i == N - 1:
A[N - 1] += 1
print(score)
| Statement
Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i
, c_i , d_i ).
Consider a sequence A satisfying the following conditions:
* A is a sequence of N positive integers.
* 1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.
Let us define a score of this sequence as follows:
* The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)
Find the maximum possible score of A. | [{"input": "3 4 3\n 1 3 3 100\n 1 2 2 10\n 2 3 2 10", "output": "110\n \n\nWhen A = \\\\{1, 3, 4\\\\}, its score is 110. Under these conditions, no sequence\nhas a score greater than 110, so the answer is 110.\n\n* * *"}, {"input": "4 6 10\n 2 4 1 86568\n 1 4 0 90629\n 2 3 0 90310\n 3 4 1 29211\n 3 4 3 78537\n 3 4 2 8580\n 1 2 1 96263\n 1 4 2 2156\n 1 2 0 94325\n 1 4 3 94328", "output": "357500\n \n\n* * *"}, {"input": "10 10 1\n 1 10 9 1", "output": "1"}] |
Print the area and circumference of the circle in a line. Put a single space
between them. The output should not contain an absolute error greater than
10-5. | s788495671 | Accepted | p02400 | A real number r is given. | import math
a=float(input())
B=math.pi*a*a
C=2*math.pi*a
print(str(B)+" "+str(C))
| Circle
Write a program which calculates the area and circumference of a circle for
given radius r. | [{"input": "2", "output": "12.566371 12.566371"}, {"input": "3", "output": "28.274334 18.849556"}] |
Print the area and circumference of the circle in a line. Put a single space
between them. The output should not contain an absolute error greater than
10-5. | s012410948 | Wrong Answer | p02400 | A real number r is given. | x = input()
x = float(x)
print("%f %f" % (x**2 * 3.1415926535, 2 * 3.1415926535 * x))
| Circle
Write a program which calculates the area and circumference of a circle for
given radius r. | [{"input": "2", "output": "12.566371 12.566371"}, {"input": "3", "output": "28.274334 18.849556"}] |
Print the area and circumference of the circle in a line. Put a single space
between them. The output should not contain an absolute error greater than
10-5. | s970230238 | Wrong Answer | p02400 | A real number r is given. | r = input()
| Circle
Write a program which calculates the area and circumference of a circle for
given radius r. | [{"input": "2", "output": "12.566371 12.566371"}, {"input": "3", "output": "28.274334 18.849556"}] |
Print the area and circumference of the circle in a line. Put a single space
between them. The output should not contain an absolute error greater than
10-5. | s989054567 | Runtime Error | p02400 | A real number r is given. | W, H, x, y, r = list(map(int, input().split()))
if r <= x <= (W - r) and r <= y <= (H - r):
print("Yes")
else:
print("No")
| Circle
Write a program which calculates the area and circumference of a circle for
given radius r. | [{"input": "2", "output": "12.566371 12.566371"}, {"input": "3", "output": "28.274334 18.849556"}] |
Print the area and circumference of the circle in a line. Put a single space
between them. The output should not contain an absolute error greater than
10-5. | s731829904 | Accepted | p02400 | A real number r is given. | r=float(input())
print("{0:.6f} {1:.6f}".format(r**2*3.141592653589,r*2*3.141592653589))
| Circle
Write a program which calculates the area and circumference of a circle for
given radius r. | [{"input": "2", "output": "12.566371 12.566371"}, {"input": "3", "output": "28.274334 18.849556"}] |
Print the area and circumference of the circle in a line. Put a single space
between them. The output should not contain an absolute error greater than
10-5. | s734272064 | Runtime Error | p02400 | A real number r is given. | a, b = map(int, input().split())
d = a // b
r = a % b
f = a / b
print("{:d} {:d} {:.5f}".format(d, r, f))
| Circle
Write a program which calculates the area and circumference of a circle for
given radius r. | [{"input": "2", "output": "12.566371 12.566371"}, {"input": "3", "output": "28.274334 18.849556"}] |
Print the area and circumference of the circle in a line. Put a single space
between them. The output should not contain an absolute error greater than
10-5. | s681071371 | Wrong Answer | p02400 | A real number r is given. | a=float(input())
b=a*3.1415926
print(a*b,2*b) | Circle
Write a program which calculates the area and circumference of a circle for
given radius r. | [{"input": "2", "output": "12.566371 12.566371"}, {"input": "3", "output": "28.274334 18.849556"}] |
If it is possible to perform the operations so that the heights of the squares
are non-decreasing from left to right, print `Yes`; otherwise, print `No`.
* * * | s106624928 | Runtime Error | p02953 | Input is given from Standard Input in the following format:
N
H_1 H_2 ... H_N | a, b, c = map(int, input().split())
if (a - b) >= c:
print(0)
else:
print(c - a + b)
| Statement
There are N squares arranged in a row from left to right. The height of the
i-th square from the left is H_i.
For each square, you will perform either of the following operations once:
* Decrease the height of the square by 1.
* Do nothing.
Determine if it is possible to perform the operations so that the heights of
the squares are non-decreasing from left to right. | [{"input": "5\n 1 2 1 1 3", "output": "Yes\n \n\nYou can achieve the objective by decreasing the height of only the second\nsquare from the left by 1.\n\n* * *"}, {"input": "4\n 1 3 2 1", "output": "No\n \n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "Yes\n \n\n* * *"}, {"input": "1\n 1000000000", "output": "Yes"}] |
If it is possible to perform the operations so that the heights of the squares
are non-decreasing from left to right, print `Yes`; otherwise, print `No`.
* * * | s587056025 | Wrong Answer | p02953 | Input is given from Standard Input in the following format:
N
H_1 H_2 ... H_N | print("Yes")
| Statement
There are N squares arranged in a row from left to right. The height of the
i-th square from the left is H_i.
For each square, you will perform either of the following operations once:
* Decrease the height of the square by 1.
* Do nothing.
Determine if it is possible to perform the operations so that the heights of
the squares are non-decreasing from left to right. | [{"input": "5\n 1 2 1 1 3", "output": "Yes\n \n\nYou can achieve the objective by decreasing the height of only the second\nsquare from the left by 1.\n\n* * *"}, {"input": "4\n 1 3 2 1", "output": "No\n \n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "Yes\n \n\n* * *"}, {"input": "1\n 1000000000", "output": "Yes"}] |
If it is possible to perform the operations so that the heights of the squares
are non-decreasing from left to right, print `Yes`; otherwise, print `No`.
* * * | s931263932 | Wrong Answer | p02953 | Input is given from Standard Input in the following format:
N
H_1 H_2 ... H_N | print("No")
| Statement
There are N squares arranged in a row from left to right. The height of the
i-th square from the left is H_i.
For each square, you will perform either of the following operations once:
* Decrease the height of the square by 1.
* Do nothing.
Determine if it is possible to perform the operations so that the heights of
the squares are non-decreasing from left to right. | [{"input": "5\n 1 2 1 1 3", "output": "Yes\n \n\nYou can achieve the objective by decreasing the height of only the second\nsquare from the left by 1.\n\n* * *"}, {"input": "4\n 1 3 2 1", "output": "No\n \n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "Yes\n \n\n* * *"}, {"input": "1\n 1000000000", "output": "Yes"}] |
If it is possible to perform the operations so that the heights of the squares
are non-decreasing from left to right, print `Yes`; otherwise, print `No`.
* * * | s883387157 | Accepted | p02953 | Input is given from Standard Input in the following format:
N
H_1 H_2 ... H_N | test = False
def solve_a(test=False, a=0, b=0, c=0):
if not test:
a, b, c = map(int, input().split())
d = max(c - (a - b), 0)
print(d)
def solve_b(test=False, n=0):
if not test:
n = int(input())
res = 0
for i in range(1, n + 1):
if len(str(i)) % 2 == 1:
res += 1
print(res)
def solve_c(test=False, n=0, h_list=[]):
if not test:
n = int(input())
h_list = list(map(int, input().split()))
res = "Yes"
new_h_list = [0] * n
new_h_list[0] = h_list[0] - 1
for i in range(1, n):
# cannot down
if h_list[i] == new_h_list[i - 1]:
new_h_list[i] = h_list[i]
elif h_list[i] < new_h_list[i - 1]:
new_h_list[i] = h_list[i]
res = "No"
else:
new_h_list[i] = h_list[i] - 1
print(res)
if __name__ == "__main__":
if test:
solve_a(test=True, a=6, b=4, c=3)
solve_a(test=True, a=8, b=3, c=9)
solve_a(test=True, a=12, b=3, c=7)
solve_b(test=True, n=11)
solve_b(test=True, n=136)
solve_b(test=True, n=100000)
solve_c(test=True, n=5, h_list=[1, 2, 1, 1, 3])
solve_c(test=True, n=4, h_list=[1, 3, 2, 1])
solve_c(test=True, n=5, h_list=[1, 2, 3, 4, 5])
solve_c(test=True, n=1, h_list=[1000000000])
else:
# solve_a()
# solve_b()
solve_c()
| Statement
There are N squares arranged in a row from left to right. The height of the
i-th square from the left is H_i.
For each square, you will perform either of the following operations once:
* Decrease the height of the square by 1.
* Do nothing.
Determine if it is possible to perform the operations so that the heights of
the squares are non-decreasing from left to right. | [{"input": "5\n 1 2 1 1 3", "output": "Yes\n \n\nYou can achieve the objective by decreasing the height of only the second\nsquare from the left by 1.\n\n* * *"}, {"input": "4\n 1 3 2 1", "output": "No\n \n\n* * *"}, {"input": "5\n 1 2 3 4 5", "output": "Yes\n \n\n* * *"}, {"input": "1\n 1000000000", "output": "Yes"}] |
Print the lexicographically smallest permutation, with spaces in between.
* * * | s102178802 | Runtime Error | p03641 | Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N | class segment_tree:
def __init__(self, N, operator_M, e_M):
self.op_M = operator_M
self.e_M = e_M
self.N0 = 1 << (N - 1).bit_length()
self.dat = [self.e_M] * (2 * self.N0)
# 長さNの配列 initial で初期化
def build(self, initial):
self.dat[self.N0 : self.N0 + len(initial)] = initial[:]
for k in range(self.N0 - 1, 0, -1):
self.dat[k] = self.op_M(self.dat[2 * k], self.dat[2 * k + 1])
# a_k の値を x に更新
def update(self, k, x):
k += self.N0
self.dat[k] = x
k //= 2
while k:
self.dat[k] = self.op_M(self.dat[2 * k], self.dat[2 * k + 1])
k //= 2
# 区間[L,R]をopでまとめる
def query(self, L, R):
L += self.N0
R += self.N0 + 1
sl = sr = self.e_M
while L < R:
if R & 1:
R -= 1
sr = self.op_M(self.dat[R], sr)
if L & 1:
sl = self.op_M(sl, self.dat[L])
L += 1
L >>= 1
R >>= 1
return self.op_M(sl, sr)
def get(self, k): # k番目の値を取得。query[k,k]と同じ
return self.dat[k + self.N0]
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline
n, *p = map(int, read().split())
INF = 1 << 30
p += [INF, INF]
def argmin(i, j):
if p[i] < p[j]:
return i
else:
return j
even = segment_tree(n // 2 + 1, argmin, n)
odd = segment_tree(n // 2 + 1, argmin, n + 1)
# print(p[::2],n//2)
even.build(range(0, n + 2, 2))
odd.build(range(1, n + 2, 2))
# print(p)
# print(even.dat)
# print(odd.dat)
def merge(a, b):
la, lb = len(a), len(b)
res = [0] * (la + lb)
ia = ib = 0
for i in range(la + lb):
if ib == lb:
res[i:] = a[ia:]
return res
if ia == la:
res[i:] = b[ib:]
return res
if a[ia] < b[ib]:
res[i] = a[ia]
ia += 1
else:
res[i] = b[ib]
ib += 1
return res
def merge3(l0, l1, l2):
if len(l0) < len(l1):
if len(l1) < len(l2):
return merge(merge(l0, l1), l2)
else:
return merge(merge(l0, l2), l1)
else:
if len(l0) < len(l2):
return merge(merge(l0, l1), l2)
else:
return merge(merge(l1, l2), l0)
def f(i, j):
if i >= j:
return []
if i + 1 == j:
return [(p[i], p[j])]
k = odd.query(i // 2, (j - 1) // 2) if i % 2 else even.query(i // 2, (j - 1) // 2)
l = even.query((k + 1) // 2, j // 2) if i % 2 else odd.query((k + 1) // 2, j // 2)
res = [(p[k], p[l])] + merge3(f(i, k - 1), f(k + 1, l - 1), f(l + 1, j))
# print(f"i={i},j={j},k={k},l={l},{res}")
return res
# print(p)
r = f(0, n - 1)
ans = []
for lst in r:
ans += [lst[0], lst[1]]
print(*ans)
| Statement
Let N be a positive even number.
We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is
constructing another permutation of (1, 2, ..., N), q, following the procedure
below.
First, let q be an empty sequence. Then, perform the following operation until
p becomes empty:
* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.
When p becomes empty, q will be a permutation of (1, 2, ..., N).
Find the lexicographically smallest permutation that can be obtained as q. | [{"input": "4\n 3 2 4 1", "output": "3 1 2 4\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(3, 2, 4, 1) | () \n\u2193 | \u2193 \n(3, 1) | (2, 4) \n\u2193 | \u2193 \n() | (3, 1, 2, 4) \n \n* * *"}, {"input": "2\n 1 2", "output": "1 2\n \n\n* * *"}, {"input": "8\n 4 6 3 2 8 5 7 1", "output": "3 1 2 7 4 6 8 5\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(4, 6, 3, 2, 8, 5, 7, 1) | () \n\u2193 | \u2193 \n(4, 6, 3, 2, 7, 1) | (8, 5) \n\u2193 | \u2193 \n(3, 2, 7, 1) | (4, 6, 8, 5) \n\u2193 | \u2193 \n(3, 1) | (2, 7, 4, 6, 8, 5) \n\u2193 | \u2193 \n() | (3, 1, 2, 7, 4, 6, 8, 5)"}] |
Print the lexicographically smallest permutation, with spaces in between.
* * * | s802870296 | Wrong Answer | p03641 | Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N | #####segfunc#####
def segfunc(x, y):
if y[0] > x[0]:
return x
else:
return y
#################
#####ide_ele#####
ide_ele = (float("inf"), -1)
#################
class SegTree:
"""
init(init_val, ide_ele): 配列init_valで初期化 O(N)
update(k, x): k番目の値をxに更新 O(logN)
query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
"""
def __init__(self, init_val, segfunc, ide_ele):
"""
init_val: 配列の初期値
segfunc: 区間にしたい操作
ide_ele: 単位元
n: 要素数
num: n以上の最小の2のべき乗
tree: セグメント木(1-index)
"""
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = ide_ele
self.num = 1 << (n - 1).bit_length()
self.tree = [ide_ele] * 2 * self.num
# 配列の値を葉にセット
for i in range(n):
self.tree[self.num + i] = init_val[i]
# 構築していく
for i in range(self.num - 1, 0, -1):
self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
def update(self, k, x):
"""
k番目の値をxに更新
k: index(0-index)
x: update value
"""
k += self.num
self.tree[k] = x
while k > 1:
self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
k >>= 1
def query(self, l, r):
"""
[l, r)のsegfuncしたものを得る
l: index(0-index)
r: index(0-index)
"""
res = self.ide_ele
l += self.num
r += self.num
while l < r:
if l & 1:
res = self.segfunc(res, self.tree[l])
l += 1
if r & 1:
res = self.segfunc(res, self.tree[r - 1])
l >>= 1
r >>= 1
return res
import heapq
N = int(input())
p = list(map(int, input().split()))
n = N // 2
op = [(p[2 * i + 1], 2 * i + 1) for i in range(n)]
ep = [(p[2 * i], 2 * i) for i in range(n)]
oseg = SegTree(op, segfunc, ide_ele)
eseg = SegTree(ep, segfunc, ide_ele)
def first(l, r):
if l >= r:
return (-1, -1, -1, -1)
if l % 2 == 0:
val, index = eseg.query(l // 2, r // 2)
val2, index2 = oseg.query(index // 2, r // 2)
return (val, val2, index, index2)
else:
val, index = oseg.query(l // 2, r // 2)
val2, index2 = eseg.query(index // 2 + 1, r // 2 + 1)
return (val, val2, index, index2)
val, val2, index, index2 = first(0, N)
que = [((val, val2), 0, N)]
heapq.heapify(que)
ans = []
while que:
tuple, l, r = heapq.heappop(que)
ans.append(tuple[0])
ans.append(tuple[1])
val, val2, index, index2 = first(l, r)
val, val2, l, r = first(l, index)
if val != -1:
heapq.heappush(que, ((val, val2), l, index))
val, val2, l, r = first(index + 1, index2)
if val != -1:
heapq.heappush(que, ((val, val2), index + 1, index2))
val, val2, l, r = first(index2 + 1, r)
if val != -1:
heapq.heappush(que, ((val, val2), index2 + 1, r))
print(*ans)
| Statement
Let N be a positive even number.
We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is
constructing another permutation of (1, 2, ..., N), q, following the procedure
below.
First, let q be an empty sequence. Then, perform the following operation until
p becomes empty:
* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.
When p becomes empty, q will be a permutation of (1, 2, ..., N).
Find the lexicographically smallest permutation that can be obtained as q. | [{"input": "4\n 3 2 4 1", "output": "3 1 2 4\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(3, 2, 4, 1) | () \n\u2193 | \u2193 \n(3, 1) | (2, 4) \n\u2193 | \u2193 \n() | (3, 1, 2, 4) \n \n* * *"}, {"input": "2\n 1 2", "output": "1 2\n \n\n* * *"}, {"input": "8\n 4 6 3 2 8 5 7 1", "output": "3 1 2 7 4 6 8 5\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(4, 6, 3, 2, 8, 5, 7, 1) | () \n\u2193 | \u2193 \n(4, 6, 3, 2, 7, 1) | (8, 5) \n\u2193 | \u2193 \n(3, 2, 7, 1) | (4, 6, 8, 5) \n\u2193 | \u2193 \n(3, 1) | (2, 7, 4, 6, 8, 5) \n\u2193 | \u2193 \n() | (3, 1, 2, 7, 4, 6, 8, 5)"}] |
Print the lexicographically smallest permutation, with spaces in between.
* * * | s769288435 | Runtime Error | p03641 | Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()
#define dump(x) cerr << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; }
template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; }
template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os<<"("<<p.first<<","<<p.second<<")";
return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os<<"{";
rep(i, v.size()) {
if (i) os<<",";
os<<v[i];
}
os<<"}";
return os;
}
const int SZ = 1 << 18;
const int INF = TEN(9);
struct segtree {
vector<pii> dat;
const pii inf = mp(INF, INF);
void init() {
dat.resize(SZ * 2);
rep(i, SZ * 2) dat[i] = inf;
}
void update(int pos, int x) {
dat[pos + SZ - 1] = mp(x, pos);
pos += SZ - 1;
while (pos > 0) {
pos = (pos - 1) / 2;
dat[pos] = min(dat[pos * 2 + 1], dat[pos * 2 + 2]);
}
}
pii get(int a, int b, int k = 0, int l = 0, int r = SZ) {
if (b <= l || r <= a) return inf;
if (a <= l && r <= b) return dat[k];
return min(get(a, b, k * 2 + 1, l, (l + r) / 2), get(a, b, k * 2 + 2, (l + r) / 2, r));
}
} seg[2];
int main() {
seg[0].init();
seg[1].init();
int N; cin >> N;
V<int> p(N);
rep(i, N) {
cin >> p[i];
seg[i % 2].update(i, p[i]);
}
using Data = pair<pii, pii>;
priority_queue<Data, vector<Data>, greater<Data>> que;
auto add = [&](int l, int r) {
auto p = seg[l%2].get(l, r);
if (l >= r || p.fi == INF) return ;
que.push(mp(seg[l%2].get(l, r), mp(l, r)));
};
add(0, N);
V<int> ans;
while (!que.empty()) {
auto p = que.top(); que.pop();
int val = p.fi.fi, pos = p.fi.se;
int l = p.se.fi, r = p.se.se;
auto nx = seg[1-l%2].get(pos + 1, r);
ans.pb(val);
ans.pb(nx.fi);
add(l, pos);
add(pos + 1, nx.se);
add(nx.se + 1, r);
}
rep(i, N) printf("%d%c", ans[i], i == N - 1 ? '\n' : ' ');
return 0;
} | Statement
Let N be a positive even number.
We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is
constructing another permutation of (1, 2, ..., N), q, following the procedure
below.
First, let q be an empty sequence. Then, perform the following operation until
p becomes empty:
* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.
When p becomes empty, q will be a permutation of (1, 2, ..., N).
Find the lexicographically smallest permutation that can be obtained as q. | [{"input": "4\n 3 2 4 1", "output": "3 1 2 4\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(3, 2, 4, 1) | () \n\u2193 | \u2193 \n(3, 1) | (2, 4) \n\u2193 | \u2193 \n() | (3, 1, 2, 4) \n \n* * *"}, {"input": "2\n 1 2", "output": "1 2\n \n\n* * *"}, {"input": "8\n 4 6 3 2 8 5 7 1", "output": "3 1 2 7 4 6 8 5\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(4, 6, 3, 2, 8, 5, 7, 1) | () \n\u2193 | \u2193 \n(4, 6, 3, 2, 7, 1) | (8, 5) \n\u2193 | \u2193 \n(3, 2, 7, 1) | (4, 6, 8, 5) \n\u2193 | \u2193 \n(3, 1) | (2, 7, 4, 6, 8, 5) \n\u2193 | \u2193 \n() | (3, 1, 2, 7, 4, 6, 8, 5)"}] |
Print the lexicographically smallest permutation, with spaces in between.
* * * | s899827372 | Runtime Error | p03641 | Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N | import sys
sys.setrecursionlimit(10**6)
def input():
return sys.stdin.readline()[:-1]
n = int(input())
a = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for _ in range(n - 1):
s, t = map(int, input().split())
adj[s - 1].append(t - 1)
adj[t - 1].append(s - 1)
par = [-1 for _ in range(n)]
aff = [0 for _ in range(n)]
def dfs(x, p):
for v in adj[x]:
if v == p:
continue
aff[x] += 1
par[v] = x
dfs(v, x)
return
dfs(0, -1)
af_lis = [[] for _ in range(n)]
zero = [i for i in range(n) if aff[i] == 0]
def check(x):
if af_lis[x] == []:
af_lis[par[x]].append(a[x])
aff[par[x]] -= 1
if aff[par[x]] == 0:
zero.append(par[x])
return
s, ma = sum(af_lis[x]), max(af_lis[x])
if ma * 2 > s:
s_mi = ma
else:
s_mi = (s + 1) // 2
if s_mi <= a[x] <= s:
if par[x] == -1:
if 2 * a[x] != s:
print("NO")
sys.exit()
else:
return
af_lis[par[x]].append(2 * a[x] - s)
aff[par[x]] -= 1
if aff[par[x]] == 0:
zero.append(par[x])
return
else:
print("NO")
sys.exit()
return
while zero:
check(zero.pop())
print("YES")
| Statement
Let N be a positive even number.
We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is
constructing another permutation of (1, 2, ..., N), q, following the procedure
below.
First, let q be an empty sequence. Then, perform the following operation until
p becomes empty:
* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.
When p becomes empty, q will be a permutation of (1, 2, ..., N).
Find the lexicographically smallest permutation that can be obtained as q. | [{"input": "4\n 3 2 4 1", "output": "3 1 2 4\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(3, 2, 4, 1) | () \n\u2193 | \u2193 \n(3, 1) | (2, 4) \n\u2193 | \u2193 \n() | (3, 1, 2, 4) \n \n* * *"}, {"input": "2\n 1 2", "output": "1 2\n \n\n* * *"}, {"input": "8\n 4 6 3 2 8 5 7 1", "output": "3 1 2 7 4 6 8 5\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(4, 6, 3, 2, 8, 5, 7, 1) | () \n\u2193 | \u2193 \n(4, 6, 3, 2, 7, 1) | (8, 5) \n\u2193 | \u2193 \n(3, 2, 7, 1) | (4, 6, 8, 5) \n\u2193 | \u2193 \n(3, 1) | (2, 7, 4, 6, 8, 5) \n\u2193 | \u2193 \n() | (3, 1, 2, 7, 4, 6, 8, 5)"}] |
Print the lexicographically smallest permutation, with spaces in between.
* * * | s735519394 | Runtime Error | p03641 | Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N | import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**10
mod = 10**9 + 7
def LI():
return [int(x) for x in sys.stdin.readline().split()]
def LI_():
return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF():
return [float(x) for x in sys.stdin.readline().split()]
def LS():
return sys.stdin.readline().split()
def I():
return int(sys.stdin.readline())
def F():
return float(sys.stdin.readline())
def S():
return input()
class Seg:
def __init__(self, na, default, func):
if isinstance(na, list):
n = len(na)
else:
n = na
i = math.ceil(math.log2(n))
self.D = default
self.H = i
self.N = 2**i
if isinstance(na, list):
self.A = [default] * (self.N - 1) + na + [default] * (self.N - n)
for i in range(self.N - 2, -1, -1):
self.A[i] = func(self.A[i * 2 + 1], self.A[i * 2 + 2])
else:
self.A = [default] * (self.N * 2 - 1)
self.F = func
def find(self, i):
return self.A[i + self.N - 1]
def update(self, i, x):
i += self.N - 1
self.A[i] = x
while i > 0:
i = (i - 1) // 2
self.A[i] = self.merge(self.A[i * 2 + 1], self.A[i * 2 + 2])
def merge(self, a, b):
return self.F(a, b)
def total(self):
return self.A[0]
def query(self, a, b):
b += 1
def _query(k, l, r):
if r <= a or b <= l:
return self.D
if a <= l and r <= b:
return self.A[k]
m = (l + r) // 2
vl = _query(k * 2 + 1, l, m)
vr = _query(k * 2 + 2, m, r)
return self.merge(vl, vr)
return _query(0, 0, self.N)
def main():
n = I()
a = LI()
def sf(a, b):
if a < b:
return a
return b
s1 = Seg([c if i % 2 == 0 else inf for c, i in zip(a, range(n))], inf, sf)
s2 = Seg([c if i % 2 == 1 else inf for c, i in zip(a, range(n))], inf, sf)
d = {}
for i in range(n):
d[a[i]] = i
def f(l, r):
if l % 2 == 0:
t = s1.query(l, r)
ti = d[t]
u = s2.query(ti + 1, r)
else:
t = s2.query(l, r)
ti = d[t]
u = s1.query(ti + 1, r)
ui = d[u]
nl = []
if l < ti:
nl.append((l, ti - 1))
if ui - ti > 1:
nl.append((ti + 1, ui - 1))
if ui < r:
nl.append((ui + 1, r))
return ((t, u), nl)
q = []
heapq.heappush(q, f(0, n - 1))
r = []
while q:
t, nl = heapq.heappop(q)
r.append(t[0])
r.append(t[1])
for t, u in nl:
heapq.heappush(q, f(t, u))
return " ".join(map(str, r))
print(main())
| Statement
Let N be a positive even number.
We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is
constructing another permutation of (1, 2, ..., N), q, following the procedure
below.
First, let q be an empty sequence. Then, perform the following operation until
p becomes empty:
* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.
When p becomes empty, q will be a permutation of (1, 2, ..., N).
Find the lexicographically smallest permutation that can be obtained as q. | [{"input": "4\n 3 2 4 1", "output": "3 1 2 4\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(3, 2, 4, 1) | () \n\u2193 | \u2193 \n(3, 1) | (2, 4) \n\u2193 | \u2193 \n() | (3, 1, 2, 4) \n \n* * *"}, {"input": "2\n 1 2", "output": "1 2\n \n\n* * *"}, {"input": "8\n 4 6 3 2 8 5 7 1", "output": "3 1 2 7 4 6 8 5\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(4, 6, 3, 2, 8, 5, 7, 1) | () \n\u2193 | \u2193 \n(4, 6, 3, 2, 7, 1) | (8, 5) \n\u2193 | \u2193 \n(3, 2, 7, 1) | (4, 6, 8, 5) \n\u2193 | \u2193 \n(3, 1) | (2, 7, 4, 6, 8, 5) \n\u2193 | \u2193 \n() | (3, 1, 2, 7, 4, 6, 8, 5)"}] |
Print the lexicographically smallest permutation, with spaces in between.
* * * | s722963013 | Runtime Error | p03641 | Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N | def main():
n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
pos = [j for i, j in sorted([(j, i) for i, j in enumerate(p)])]
class segtree:
def __init__(self, base, monoid, ini): # self,モノイド,元の配列
# ini:上限(下限),base:元の配列,monoid:モノイド,tree:セグ木,depth:treeの深さ,
# basesize:baseの要素数,treesize:treeの要素数,num:treesize-basesize
# 初期値(上限とか下限)を決定
self.ini = ini
# セグ木の要素数(num)および深さ(depth)を計算
self.basesize = len(base)
num, depth, basesize = 1, 1, self.basesize
while num < basesize:
num *= 2
depth += 1
num -= 1
self.num, self.depth = num, depth
# 関数(モノイド、今回はmin)を設定
self.monoid = monoid
# セグ木を構築
self.treesize = num * 2 + 1
self.tree = [None] * self.treesize
tree = self.tree
# segtree[m]以降に代入したい配列を入力していく
for i in range(num, num + basesize):
tree[i] = base[i - num]
for i in range(num + basesize, self.treesize):
tree[i] = ini
# segtree[m]以降の情報を使って、0に向かってセグ木を構築していく
for i in range(num - 1, -1, -1):
tree[i] = monoid(tree[2 * i + 1], tree[2 * i + 2])
# 半開区間[lower,upper)の最小値(とか)を探す関数
def search(self, lower, upper):
# 返り値を初期化
ret, num = self.ini, self.num
l, r, monoid, tree = lower + num, upper + num, self.monoid, self.tree
while l < r:
if r % 2 == 0:
r -= 1
ret = monoid(ret, tree[r])
if l % 2 == 0:
ret = monoid(ret, tree[l])
l += 1
l //= 2
r //= 2
return ret
seg_even = segtree(p[::2], min, n + 1)
seg_odd = segtree(p[1::2], min, n + 1)
g = dict()
d = dict()
q = [(0, n - 1)]
qap = q.append
while q:
m, M = q.pop()
g[(m, M)] = []
gap = g[(m, M)].append
if m % 2 == 0:
i1, j1 = m // 2, M // 2 + 1
even = seg_even.search(i1, j1)
odd = seg_odd.search(i1, j1)
even_idx, odd_idx = pos[even], pos[odd]
d[(m, M)] = (even + 1, odd + 1)
if m != even_idx:
qap((m, even_idx - 1))
gap((m, even_idx - 1))
if M != odd_idx:
qap((odd_idx + 1, M))
gap((odd_idx + 1, M))
if even_idx + 1 != odd_idx:
qap((even_idx + 1, odd_idx - 1))
gap((even_idx + 1, odd_idx - 1))
else:
i1, j1 = m // 2, M // 2
odd = seg_odd.search(i1, j1)
even = seg_even.search(i1 + 1, j1 + 1)
even_idx, odd_idx = pos[even], pos[odd]
d[(m, M)] = (odd + 1, even + 1)
if m != odd_idx:
qap((m, odd_idx - 1))
gap((m, odd_idx - 1))
if M != even_idx:
qap((even_idx + 1, M))
gap((even_idx + 1, M))
if odd_idx + 1 != even_idx:
qap((odd_idx + 1, even_idx - 1))
gap((odd_idx + 1, even_idx - 1))
import heapq
x, y = d[(0, n - 1)]
h = [(x, y, 0, n - 1)]
heapq.heapify(h)
ans = []
while h:
x, y, t1, t2 = heapq.heappop(h)
i, j = d[(t1, t2)]
ans += [x, y]
for a, b in g[(t1, t2)]:
e, f = d[(a, b)]
heapq.heappush(h, (e, f, a, b))
print(*ans)
main()
| Statement
Let N be a positive even number.
We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is
constructing another permutation of (1, 2, ..., N), q, following the procedure
below.
First, let q be an empty sequence. Then, perform the following operation until
p becomes empty:
* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.
When p becomes empty, q will be a permutation of (1, 2, ..., N).
Find the lexicographically smallest permutation that can be obtained as q. | [{"input": "4\n 3 2 4 1", "output": "3 1 2 4\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(3, 2, 4, 1) | () \n\u2193 | \u2193 \n(3, 1) | (2, 4) \n\u2193 | \u2193 \n() | (3, 1, 2, 4) \n \n* * *"}, {"input": "2\n 1 2", "output": "1 2\n \n\n* * *"}, {"input": "8\n 4 6 3 2 8 5 7 1", "output": "3 1 2 7 4 6 8 5\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(4, 6, 3, 2, 8, 5, 7, 1) | () \n\u2193 | \u2193 \n(4, 6, 3, 2, 7, 1) | (8, 5) \n\u2193 | \u2193 \n(3, 2, 7, 1) | (4, 6, 8, 5) \n\u2193 | \u2193 \n(3, 1) | (2, 7, 4, 6, 8, 5) \n\u2193 | \u2193 \n() | (3, 1, 2, 7, 4, 6, 8, 5)"}] |
Print the lexicographically smallest permutation, with spaces in between.
* * * | s586004882 | Runtime Error | p03641 | Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N | # AOJ DSL_2_A "Range Minimum Query"
# SegmantTreeの実装
import sys
input = sys.stdin.readline
# 初期化最大値
INF = (1 << 31) - 1
class SegmentTree:
def __init__(self, N):
self.N = 2 ** (N - 1).bit_length()
self.data = [[INF, -1] for _ in range(2 * self.N - 1)]
# k番目の値(0-indexed)をaに変更
def update(self, k, a):
self.data[k + self.N - 1] = [a, k]
k += self.N - 1
while k > 0:
k = (k - 1) // 2
if self.data[2 * k + 1][0] < self.data[2 * k + 2][0]:
self.data[k] = self.data[2 * k + 1][:]
else:
self.data[k] = self.data[2 * k + 2][:]
# [l, r)の最小値取得
# kがNodeの番号、対応する区間が[a, b)
def query_min(self, l, r):
L = l + self.N
R = r + self.N
s = [INF, -1]
while L < R:
if R & 1:
R -= 1
if s[0] > self.data[R - 1][0]:
s = self.data[R - 1]
if L & 1:
if s[0] > self.data[L - 1][0]:
s = self.data[L - 1]
L += 1
L >>= 1
R >>= 1
return s
import heapq as hp
N = int(input())
A = list(map(int, input().split()))
ST1 = SegmentTree(N // 2)
ST2 = SegmentTree(N // 2)
for i, a in enumerate(A):
if i % 2 == 0:
ST1.update(i // 2, a)
else:
ST2.update(i // 2, a)
ans = []
q = []
v, k = ST1.query_min(0, N // 2)
hp.heappush(q, (v, k, 0, N // 2, True))
for _ in range(N // 2):
valuea, ka, l, r, is1 = hp.heappop(q)
ans.append(valuea)
if is1:
valueb, kb = ST2.query_min(ka, r)
if ka < kb:
m2, nk2 = ST2.query_min(ka, kb)
hp.heappush(q, (m2, nk2, ka, kb + 1, False))
if l < ka:
m1, nk1 = ST1.query_min(l, ka)
hp.heappush(q, (m1, nk1, l, ka, True))
if kb + 1 < r:
m3, nk3 = ST1.query_min(kb + 1, r)
hp.heappush(q, (m3, nk3, kb + 1, r, True))
else:
valueb, kb = ST1.query_min(ka + 1, r)
if ka + 1 < kb:
m1, nk1 = ST1.query_min(ka + 1, kb)
hp.heappush(q, (m1, nk1, ka + 1, kb, True))
if l < ka:
m2, nk2 = ST2.query_min(l, ka)
hp.heappush(q, (m2, nk2, l, ka, False))
if kb < r - 1:
m3, nk3 = ST2.query_min(kb, r - 1)
hp.heappush(q, (m3, nk3, kb, r - 1, False))
ans.append(valueb)
print(*ans)
| Statement
Let N be a positive even number.
We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is
constructing another permutation of (1, 2, ..., N), q, following the procedure
below.
First, let q be an empty sequence. Then, perform the following operation until
p becomes empty:
* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.
When p becomes empty, q will be a permutation of (1, 2, ..., N).
Find the lexicographically smallest permutation that can be obtained as q. | [{"input": "4\n 3 2 4 1", "output": "3 1 2 4\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(3, 2, 4, 1) | () \n\u2193 | \u2193 \n(3, 1) | (2, 4) \n\u2193 | \u2193 \n() | (3, 1, 2, 4) \n \n* * *"}, {"input": "2\n 1 2", "output": "1 2\n \n\n* * *"}, {"input": "8\n 4 6 3 2 8 5 7 1", "output": "3 1 2 7 4 6 8 5\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(4, 6, 3, 2, 8, 5, 7, 1) | () \n\u2193 | \u2193 \n(4, 6, 3, 2, 7, 1) | (8, 5) \n\u2193 | \u2193 \n(3, 2, 7, 1) | (4, 6, 8, 5) \n\u2193 | \u2193 \n(3, 1) | (2, 7, 4, 6, 8, 5) \n\u2193 | \u2193 \n() | (3, 1, 2, 7, 4, 6, 8, 5)"}] |
Print the lexicographically smallest permutation, with spaces in between.
* * * | s267688793 | Runtime Error | p03641 | Input is given from Standard Input in the following format:
N
p_1 p_2 ... p_N | from heapq import *
import sys
sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
class SegtreeMin:
def __init__(self, n):
self.inf = 10**16
tree_width = 1 << (n - 1).bit_length()
self.tree_width = tree_width
self.tree = [self.inf] * (tree_width * 2 - 1)
def update(self, i, a):
seg_i = self.tree_width - 1 + i
self.tree[seg_i] = a
while seg_i != 0:
seg_i = (seg_i - 1) // 2
self.tree[seg_i] = min(self.tree[seg_i * 2 + 1], self.tree[seg_i * 2 + 2])
def refresh(self):
for seg_i in range(self.tree_width - 2, -1, -1):
self.tree[seg_i] = min(self.tree[seg_i * 2 + 1], self.tree[seg_i * 2 + 2])
# [l,r)の最小値
def min(self, l, r, seg_i=0, segL=0, segR=-1):
if segR == -1:
segR = self.tree_width
if r <= segL or segR <= l:
return self.inf
if l <= segL and segR <= r:
return self.tree[seg_i]
segM = (segL + segR) // 2
ret0 = self.min(l, r, seg_i * 2 + 1, segL, segM)
ret1 = self.min(l, r, seg_i * 2 + 2, segM, segR)
return min(ret0, ret1)
def main():
n = int(input())
pp = LI()
seg_odd = SegtreeMin(n // 2)
seg_even = SegtreeMin(n // 2)
ptoi = [0] + [i for i, p in sorted(enumerate(pp), key=lambda x: x[1])]
seg_odd.tree[seg_odd.tree_width - 1 : seg_odd.tree_width - 1 + n // 2] = pp[1::2]
seg_even.tree[seg_even.tree_width - 1 : seg_even.tree_width - 1 + n // 2] = pp[::2]
seg_odd.refresh()
seg_even.refresh()
# 区間偶数番目の最小値と[l,r)をヒープに
hp = []
heappush(hp, [seg_even.min(0, n // 2), 0, n])
ans = []
for _ in range(n // 2):
x, l, r = heappop(hp)
if l + 2 == r:
ans += [pp[l], pp[l + 1]]
continue
xi = ptoi[x]
if l % 2:
y = seg_even.min((xi + 1) // 2, r // 2 + 1)
else:
y = seg_odd.min((xi + 1) // 2, r // 2)
yi = ptoi[y]
if xi - l == 2:
heappush(hp, [pp[l], l, xi])
elif xi - l > 2:
minp = (
seg_odd.min(l // 2, xi // 2)
if l % 2
else seg_even.min(l // 2, xi // 2 + 1)
)
heappush(hp, [minp, l, xi])
if yi - xi - 1 == 2:
heappush(hp, [pp[xi + 1], xi + 1, yi])
elif yi - xi - 1 > 2:
minp = (
seg_odd.min((xi + 1) // 2, yi // 2)
if (xi + 1) % 2
else seg_even.min((xi + 1) // 2, yi // 2 + 1)
)
heappush(hp, [minp, xi + 1, yi])
if r - yi - 1 == 2:
heappush(hp, [pp[yi + 1], yi + 1, r])
elif r - yi - 1 > 2:
minp = (
seg_odd.min((yi + 1) // 2, r // 2)
if (yi + 1) % 2
else seg_even.min((yi + 1) // 2, r // 2 + 1)
)
heappush(hp, [minp, yi + 1, r])
ans += [x, y]
print(*ans)
main()
| Statement
Let N be a positive even number.
We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Snuke is
constructing another permutation of (1, 2, ..., N), q, following the procedure
below.
First, let q be an empty sequence. Then, perform the following operation until
p becomes empty:
* Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.
When p becomes empty, q will be a permutation of (1, 2, ..., N).
Find the lexicographically smallest permutation that can be obtained as q. | [{"input": "4\n 3 2 4 1", "output": "3 1 2 4\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(3, 2, 4, 1) | () \n\u2193 | \u2193 \n(3, 1) | (2, 4) \n\u2193 | \u2193 \n() | (3, 1, 2, 4) \n \n* * *"}, {"input": "2\n 1 2", "output": "1 2\n \n\n* * *"}, {"input": "8\n 4 6 3 2 8 5 7 1", "output": "3 1 2 7 4 6 8 5\n \n\nThe solution above is obtained as follows:\n\np | q \n---|--- \n(4, 6, 3, 2, 8, 5, 7, 1) | () \n\u2193 | \u2193 \n(4, 6, 3, 2, 7, 1) | (8, 5) \n\u2193 | \u2193 \n(3, 2, 7, 1) | (4, 6, 8, 5) \n\u2193 | \u2193 \n(3, 1) | (2, 7, 4, 6, 8, 5) \n\u2193 | \u2193 \n() | (3, 1, 2, 7, 4, 6, 8, 5)"}] |
Print the minimum possible difference between X and 753.
* * * | s208107338 | Accepted | p03211 | Input is given from Standard Input in the following format:
S | # abc114_b.py
# https://atcoder.jp/contests/abc114/tasks/abc114_b
# B - 754 /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 200点
# 問題文
# 数字 1, 2, ..., 9 からなる文字列 S があります。
# ダックスフンドのルンルンは、S から連続する 3 個の数字を取り出し、 1 つの整数 Xとしてご主人様の元に持っていきます。
# (数字の順番を変えることはできません。)
# ご主人様が大好きな数は 753で、これに近い数ほど好きです。 X と 753の差(の絶対値)は最小でいくつになるでしょうか?
# 制約
# Sは長さ 4 以上 10以下の文字列である。
# Sの各文字は 1, 2, ..., 9 のいずれかである。
# 入力
# 入力は以下の形式で標準入力から与えられる。
# S
# 出力
# Xと 753の差としてありうる最小値を出力せよ。
# 入力例 1
# 1234567876
# 出力例 1
# 34
# 7文字目から 9 文字目までを取り出すと X=787 となり、これと 753 との差は 787−753=34 です。
# Xをどこから取り出しても、差をより小さくすることはできません。
# なお、数字の順番を変えることはできません。例えば、567 を取り出して 765 に並び変えてはいけません。
# また、Sから連続していない 3 文字を取り出すこともできません。
# 例えば、7 文字目の 7、9 文字目の 7 と 10文字目の 6 を取り出して 776 としてはいけません。
# 入力例 2
# 35753
# 出力例 2
# 0
# 753 そのものを取り出すことができる場合、答えは 0です。
# 入力例 3
# 1111111111
# 出力例 3
# 642
# どこから 3文字を取り出しても X=111 となり、差は 753−111=642 です。
def calculation(lines):
X = lines[0]
# X = int(lines[0])
# N, M = list(map(int, lines[0].split()))
dif = None
for i in range(len(X) - 2):
tmp = abs(int(X[i : i + 3]) - 753)
# print(f'tmp=[{tmp}]')
# print(f'dif=[{dif}]')
if dif is None:
dif = tmp
elif dif > tmp:
dif = tmp
return [dif]
# 引数を取得
def get_input_lines(lines_count):
lines = list()
for _ in range(lines_count):
lines.append(input())
return lines
# テストデータ
def get_testdata(pattern):
if pattern == 1:
lines_input = ["1234567876"]
lines_export = [34]
if pattern == 2:
lines_input = ["35753"]
lines_export = [0]
if pattern == 3:
lines_input = ["1111111111"]
lines_export = [642]
return lines_input, lines_export
# 動作モード判別
def get_mode():
import sys
args = sys.argv
if len(args) == 1:
mode = 0
else:
mode = int(args[1])
return mode
# 主処理
def main():
import time
started = time.time()
mode = get_mode()
if mode == 0:
lines_input = get_input_lines(1)
else:
lines_input, lines_export = get_testdata(mode)
lines_result = calculation(lines_input)
for line_result in lines_result:
print(line_result)
# if mode > 0:
# print(f'lines_input=[{lines_input}]')
# print(f'lines_export=[{lines_export}]')
# print(f'lines_result=[{lines_result}]')
# if lines_result == lines_export:
# print('OK')
# else:
# print('NG')
# finished = time.time()
# duration = finished - started
# print(f'duration=[{duration}]')
# 起動処理
if __name__ == "__main__":
main()
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s017648369 | Accepted | p03211 | Input is given from Standard Input in the following format:
S | import math
from collections import deque
from collections import defaultdict
# 自作関数群
def readInt():
return int(input())
def readInts():
return list(map(int, input().split()))
def readChar():
return input()
def readChars():
return input().split()
def factorization(n):
res = []
if n % 2 == 0:
res.append(2)
for i in range(3, math.floor(n // 2) + 1, 2):
if n % i == 0:
c = 0
for j in res:
if i % j == 0:
c = 1
if c == 0:
res.append(i)
return res
def fact2(n):
p = factorization(n)
res = []
for i in p:
c = 0
z = n
while 1:
if z % i == 0:
c += 1
z /= i
else:
break
res.append([i, c])
return res
def fact(n): # 階乗
ans = 1
m = n
for _i in range(n - 1):
ans *= m
m -= 1
return ans
def comb(n, r): # コンビネーション
if n < r:
return 0
l = min(r, n - r)
m = n
u = 1
for _i in range(l):
u *= m
m -= 1
return u // fact(l)
def combmod(n, r, mod):
return (fact(n) / fact(n - r) * pow(fact(r), mod - 2, mod)) % mod
def printQueue(q):
r = copyQueue(q)
ans = [0] * r.qsize()
for i in range(r.qsize() - 1, -1, -1):
ans[i] = r.get()
print(ans)
class UnionFind:
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x): # root
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -1 * self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x): # much time
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()} # 1~n
def bitArr(n): # ビット全探索
x = 1
zero = "0" * n
ans = []
ans.append([0] * n)
for i in range(2**n - 1):
ans.append(list(map(lambda x: int(x), list((zero + bin(x)[2:])[-1 * n :]))))
x += 1
return ans
def arrsSum(a1, a2):
for i in range(len(a1)):
a1[i] += a2[i]
return a1
def maxValue(a, b, v):
v2 = v
for i in range(v2, -1, -1):
for j in range(v2 // a + 1): # j:aの個数
k = i - a * j
if k % b == 0:
return i
return -1
def copyQueue(q):
nq = queue.Queue()
n = q.qsize()
for i in range(n):
x = q.get()
q.put(x)
nq.put(x)
return nq
s = readChar()
ans = float("inf")
for i in range(len(s)):
for j in range(i, len(s) - 2):
ans = min(ans, abs(753 - int(s[i : i + 3])))
print(ans)
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s269905932 | Accepted | p03211 | Input is given from Standard Input in the following format:
S | import sys
import math
import collections
import itertools
import array
import inspect
# Set max recursion limit
sys.setrecursionlimit(1000000)
# Debug output
def chkprint(*args):
names = {id(v): k for k, v in inspect.currentframe().f_back.f_locals.items()}
print(", ".join(names.get(id(arg), "???") + " = " + repr(arg) for arg in args))
# Binary converter
def to_bin(x):
return bin(x)[2:]
def li_input():
return [int(_) for _ in input().split()]
def gcd(n, m):
if n % m == 0:
return m
else:
return gcd(m, n % m)
def gcd_list(L):
v = L[0]
for i in range(1, len(L)):
v = gcd(v, L[i])
return v
def lcm(n, m):
return (n * m) // gcd(n, m)
def lcm_list(L):
v = L[0]
for i in range(1, len(L)):
v = lcm(v, L[i])
return v
# Width First Search (+ Distance)
def wfs_d(D, N, K):
"""
D: 隣接行列(距離付き)
N: ノード数
K: 始点ノード
"""
dfk = [-1] * (N + 1)
dfk[K] = 0
cps = [(K, 0)]
r = [False] * (N + 1)
r[K] = True
while len(cps) != 0:
n_cps = []
for cp, cd in cps:
for i, dfcp in enumerate(D[cp]):
if dfcp != -1 and not r[i]:
dfk[i] = cd + dfcp
n_cps.append((i, cd + dfcp))
r[i] = True
cps = n_cps[:]
return dfk
# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
"""
v: 現在のノード
pre: 1つ前のノード
dist: 現在の距離
以下は別途用意する
D: 隣接リスト(行列ではない)
D_dfs_d: dfs_d関数で用いる,始点ノードから見た距離リスト
"""
global D
global D_dfs_d
D_dfs_d[v] = dist
for next_v, d in D[v]:
if next_v != pre:
dfs_d(next_v, v, dist + d)
return
def sigma(N):
ans = 0
for i in range(1, N + 1):
ans += i
return ans
def comb(n, r):
if n - r < r:
r = n - r
if r == 0:
return 1
if r == 1:
return n
numerator = [n - r + k + 1 for k in range(r)]
denominator = [k + 1 for k in range(r)]
for p in range(2, r + 1):
pivot = denominator[p - 1]
if pivot > 1:
offset = (n - r) % p
for k in range(p - 1, r, p):
numerator[k - offset] /= pivot
denominator[k] /= pivot
result = 1
for k in range(r):
if numerator[k] > 1:
result *= int(numerator[k])
return result
def bisearch(L, target):
low = 0
high = len(L) - 1
while low <= high:
mid = (low + high) // 2
guess = L[mid]
if guess == target:
return True
elif guess < target:
low = mid + 1
elif guess > target:
high = mid - 1
if guess != target:
return False
# --------------------------------------------
dp = None
def main():
S = input()
ans = 99999
for i in range(0, len(S) - 2):
N = int(S[i : i + 3])
ans = min(ans, abs(N - 753))
print(ans)
main()
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s289749583 | Accepted | p03211 | Input is given from Standard Input in the following format:
S | import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input():
return sys.stdin.readline().strip()
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST():
return list(map(int, input().split()))
def ZIP(n):
return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10**9)
INF = float("inf")
mod = 10**9 + 7
S = input()
a = abs(int(S[0]) * 100 + int(S[1]) * 10 + int(S[2]) - 753)
for i in range(1, len(S) - 2):
b = abs(int(S[i]) * 100 + int(S[i + 1]) * 10 + int(S[i + 2]) - 753)
a = min(a, b)
print(a)
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s324677913 | Wrong Answer | p03211 | Input is given from Standard Input in the following format:
S | s = list(input())
t = [int(a) for a in s]
n = int(len(t))
ans = 0
b = 0
list1 = []
s = 1000
for i in range(n - 2):
u = [t[i], t[i + 1], t[i + 2]]
v = "".join(map(str, u))
w = int(v) - 753
list1.append(w)
u = int(len(list1))
for i in range(u - 1):
if abs(list1[i]) - abs(s) < 0:
s = list1[i]
else:
pass
print(abs(s))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s800534393 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | S = input()
N = len(S)
ans = "inf"
for i in range(N-2):
tmp = abs(753 - int(S[i:i+2])
ans = min(ans,tmp)
print(ans) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s912769667 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | 1 S = input()
2 print(min(abs(int(S[i:i+3]) - 753) for i in range(len(S) - 3)))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s219553485 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | a=input()
s=a[i:i+3] for i in range(len(a))
print(min(abs(s-753))) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s248421490 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | 1 S = input()
2 print(min(abs(int(S[i:i+3]) - 753) for i in range(len(S) - 2))) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s687831246 | Accepted | p03211 | Input is given from Standard Input in the following format:
S | a = list(input())
b = 0
for i in range(2, len(a)):
x = int(a[i - 2] + a[i - 1] + a[i])
if abs(753 - b) > abs(753 - x):
b = x
print(abs(b - 753))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s540772906 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | S = list(input())
ans = 753
for i in range(len(S)-2):
sa = abs(753-(int(S[i])*100 + int(S[i+1])*10 + int(S[i+2])))
ans = min(sa,ans)
print(ans | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s118927657 | Accepted | p03211 | Input is given from Standard Input in the following format:
S | l = list(input())
l1 = []
for i in range(len(l) - 2):
l1.append(abs(753 - int(l[i]) * 100 - int(l[i + 1]) * 10 - int(l[i + 2])))
print(min(l1))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s078634876 | Accepted | p03211 | Input is given from Standard Input in the following format:
S | # -*- coding: utf-8 -*-
# !/usr/bin/env python
# vim: set fileencoding=utf-8 :
"""
#
# Author: Noname
# URL: https://github.com/pettan0818
# License: MIT License
# Created: 日 12/ 2 22:05:15 2018
# Usage
#
"""
PREFERRED = 753
def parse_3num(string):
"""
>>> parse_3num('1234')
"""
parsed = []
temp = list(string)
counted = len(string) - 2
for i in range(counted):
parsed.append(temp[i : i + 3])
return [int("".join(x)) for x in parsed]
def checker(parsed):
"""
>>> checker([123,234])
"""
return min([abs(PREFERRED - i) for i in parsed])
if __name__ == "__main__":
print(checker(parse_3num(input())))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s495106667 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | s = input()
ans =[]
for i in range(:len(s)-2):
ans.append(int(s[i] +s[i+1] +s[i+2]))
num =[]
for i in range(len(ans()):
num.append(abs(754-ans[i]))
num.sort()
print(num[0] | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s935668304 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | N=int(input())
def dfs(s):
if int(s)>N:
return 0:
ret=1 if all (s.count(c)>0 for c in '753') else 0
for c in '753':
ret+=dfs(s+c)
return ret
print(dfs('0')) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s942160053 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | li = list(input())
num2 = None
if "7" in li:
num = li.index("7")
elif "8" in li:
num = li.index("8")
if "6" in li:
num2 = li.index("6")
elif "6" in li:
num = li.index("6")
elif "9" in li:
num = li.index("9")
if "5" in li:
num2 = li.index("5")
elif "5" in li:
num = li.index("5")
elif "4" in li:
num = li.index("4")
elif "3" in li:
num = li.index("3")
elif "2" in li:
num = li.index("2")
elif "1" in li:
num = li.index("1")
if not (num2 == None):
b = int(li[num2] + li[num2 + 1] + li[num2 + 2])
a = int(li[num] + li[num + 1] + li[num + 2])
if num2 == None or a < b:
print(abs(753 - a))
else:
print(abs(753 - b))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s315082189 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | s = input()
listS = list(s)
listA = []
listB = []
for i in range(len(listS)-2):
for j in range(3):
n = int(listS[j+i])
listA.append(n)
Sm =listA[0]*100+listA[1]*10+list{2}
sa= abs(int(753) - Sm)
listB.append(sa)
listA.clear()
print(min(listB))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s180954824 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | S = list(map(int,input().split()))
def abs(m):
if m >= 0
return m
else:
return (-1)*m
a=[]
for i in range(len(S)-2):
x = 100*S[i]+10*S[i+1]+S[i+2]-753
x = abs(x)
a.append(x)
print(min(a))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s356239907 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | in = input()
result = 999
for i in range(len(in)-2):
if i == len(in) - 3:
num = in[i:]
else:
num = in[i:i+3]
sa = abs(753 - int(num))
if sa < result:
result = sa
print(result)
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s082055218 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | S = input()
diff = 1000
for i in range(len(S)-2):
S_int = int(S[i:i+3])
diff = min(diff, abs(753-S_int)
print(diff)
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s215936017 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | s = input()
mins = 1000
for i in renge(len(s)-2):
mins = min(mins,abs(753-((int(s[i])+int(s[i+1])+int(s[i+2])))
print(mins) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s652579463 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | x = 753
s = int(input())
ans=100
for i in range(len(s)-2):
if(abs(x-int(s[i:i+3]))<ans):
ans=abs(x-int(s[i:i+3))
print(ans) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s693360422 | Wrong Answer | p03211 | Input is given from Standard Input in the following format:
S | s = list(input())
d = []
for a in range(len(s) - 2):
b = s[a : a + 3]
c = "".join(b)
e = 754 - int(c)
d.append(e)
print(abs(min(d)))
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s360631340 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | s = input()
g = float('inf')
for i in range(len(s)-2):
if g > abs(int(s[i:i+3])-753):
g = abs(int(s[i:i+3])-753)
print(g) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s318697393 | Wrong Answer | p03211 | Input is given from Standard Input in the following format:
S | data = input()
l = list()
for i in range(len(data) - 2):
l.append(abs(753 - int(data[i : i + 3])))
print(l[-1])
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s303752219 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | s = int(input())
print(min(abs(int(s[i:i+3]-753) for i in range(len(s)-2))) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s395955763 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | S=input()
ans=1000
for i in range(len(S)-3):
ans=min(ans,abs(753-int(S[i]+S[i+1]+S[i+2]))
print(ans)
| Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s148861988 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | S = input()
print(min(abs(int(S[i:i+3]) - 753)) for i in range(len(S) - 2) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Print the minimum possible difference between X and 753.
* * * | s487918415 | Runtime Error | p03211 | Input is given from Standard Input in the following format:
S | s=input()
n=len(s)
print(min((abs(753-int(s[i:i+3])) for i in range(n-2))) | Statement
There is a string S consisting of digits `1`, `2`, ..., `9`. Lunlun, the
Dachshund, will take out three consecutive digits from S, treat them as a
single integer X and bring it to her master. (She cannot rearrange the
digits.)
The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753? | [{"input": "1234567876", "output": "34\n \n\nTaking out the seventh to ninth characters results in X = 787, and the\ndifference between this and 753 is 787 - 753 = 34. The difference cannot be\nmade smaller, no matter where X is taken from.\n\nNote that the digits cannot be rearranged. For example, taking out `567` and\nrearranging it to `765` is not allowed.\n\nWe cannot take out three digits that are not consecutive from S, either. For\nexample, taking out the seventh digit `7`, the ninth digit `7` and the tenth\ndigit `6` to obtain `776` is not allowed.\n\n* * *"}, {"input": "35753", "output": "0\n \n\nIf `753` itself can be taken out, the answer is 0.\n\n* * *"}, {"input": "1111111111", "output": "642\n \n\nNo matter where X is taken from, X = 111, with the difference 753 - 111 = 642."}] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.