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 |
|---|---|---|---|---|---|---|---|
Print the information of each node in the following format ordered by IDs:
node _id_ : parent = _p_ , depth = _d_ , _type_ , [_c_ 1..._c k_]
_p_ is ID of its parent. If the node does not have a parent, print -1.
_d_ is depth of the node.
_type_ is a type of nodes represented by a string (root, internal node or
leaf). If the root can be considered as a leaf or an internal node, print
root.
_c_ 1..._c k_ is the list of children as a ordered tree.
Please follow the format presented in a sample output below. | s413314174 | Accepted | p02279 | The first line of the input includes an integer _n_ , the number of nodes of
the tree.
In the next _n_ lines, the information of each node _u_ is given in the
following format:
_id_ _k_ _c_ 1 _c_ 2 ... _c k_
where _id_ is the node ID of _u_ , _k_ is the degree of _u_ , _c_ 1 ... _c k_
are node IDs of 1st, ... _k_ th child of _u_. If the node does not have a
child, the _k_ is 0. | class Node:
"""
p: parent node
l: child node
r: sibling node
"""
def __init__(self, p=-1, l=-1, r=-1):
self._p = p
self._l = l
self._r = r
Depth = []
T = []
def rec(node, depth):
global Depth
global T
Depth[node] = depth
child = T[node]._l
while child != -1:
rec(child, depth + 1)
child = T[child]._r
# if T[node]._r != -1:
# rec(T[node]._r, depth)
# if T[node]._l != -1:
# rec(T[node]._l, depth + 1)
def print_node(index):
global Depth
global T
print(
"node %d: parent = %d, depth = %d, " % (index, T[index]._p, Depth[index]),
end="",
)
if T[index]._p == -1:
print("root, [", end="")
elif T[index]._l == -1:
print("leaf, [", end="")
else:
print("internal node, [", end="")
i = 0
node = T[index]._l
while node != -1:
if i:
print(", ", end="")
print(node, end="")
i += 1
node = T[node]._r
print("]")
if __name__ == "__main__":
n = int(input())
for i in range(n):
T.append(Node())
Depth = [-1] * n
for i in range(n):
parent_node, child_cnt, *child_node = map(int, input().split())
for j in range(child_cnt):
if j == 0:
T[parent_node]._l = child_node[j]
else:
T[child_node[j - 1]]._r = child_node[j]
T[child_node[j]]._p = parent_node
root = 0
for i in range(n):
if T[i]._p == -1:
root = i
break
rec(root, 0)
for i in range(n):
print_node(i)
| Rooted Trees
A graph _G_ = (_V_ , _E_) is a data structure where _V_ is a finite set of
vertices and _E_ is a binary relation on _V_ represented by a set of edges.
Fig. 1 illustrates an example of a graph (or graphs).

**Fig. 1**
A free tree is a connnected, acyclic, undirected graph. A rooted tree is a
free tree in which one of the vertices is distinguished from the others. A
vertex of a rooted tree is called "node."
Your task is to write a program which reports the following information for
each node _u_ of a given rooted tree _T_ :
* node ID of _u_
* parent of _u_
* depth of _u_
* node type (root, internal node or leaf)
* a list of chidlren of _u_
If the last edge on the path from the root _r_ of a tree _T_ to a node _x_ is
(_p_ , _x_), then _p_ is the **parent** of _x_ , and _x_ is a **child** of
_p_. The root is the only node in _T_ with no parent.
A node with no children is an **external node** or **leaf**. A nonleaf node is
an **internal node**
The number of children of a node _x_ in a rooted tree _T_ is called the
**degree** of _x_.
The length of the path from the root _r_ to a node _x_ is the **depth** of _x_
in _T_.
Here, the given tree consists of _n_ nodes and evey node has a unique ID from
0 to _n_ -1.
Fig. 2 shows an example of rooted trees where ID of each node is indicated by
a number in a circle (node). The example corresponds to the first sample
input.

**Fig. 2** | [{"input": "13\n 0 3 1 4 10\n 1 2 2 3\n 2 0\n 3 0\n 4 3 5 6 7\n 5 0\n 6 0\n 7 2 8 9\n 8 0\n 9 0\n 10 2 11 12\n 11 0\n 12 0", "output": "node 0: parent = -1, depth = 0, root, [1, 4, 10]\n node 1: parent = 0, depth = 1, internal node, [2, 3]\n node 2: parent = 1, depth = 2, leaf, []\n node 3: parent = 1, depth = 2, leaf, []\n node 4: parent = 0, depth = 1, internal node, [5, 6, 7]\n node 5: parent = 4, depth = 2, leaf, []\n node 6: parent = 4, depth = 2, leaf, []\n node 7: parent = 4, depth = 2, internal node, [8, 9]\n node 8: parent = 7, depth = 3, leaf, []\n node 9: parent = 7, depth = 3, leaf, []\n node 10: parent = 0, depth = 1, internal node, [11, 12]\n node 11: parent = 10, depth = 2, leaf, []\n node 12: parent = 10, depth = 2, leaf, []"}, {"input": "4\n 1 3 3 2 0\n 0 0\n 3 0\n 2 0", "output": "node 0: parent = 1, depth = 1, leaf, []\n node 1: parent = -1, depth = 0, root, [3, 2, 0]\n node 2: parent = 1, depth = 1, leaf, []\n node 3: parent = 1, depth = 1, leaf, []"}] |
Print the information of each node in the following format ordered by IDs:
node _id_ : parent = _p_ , depth = _d_ , _type_ , [_c_ 1..._c k_]
_p_ is ID of its parent. If the node does not have a parent, print -1.
_d_ is depth of the node.
_type_ is a type of nodes represented by a string (root, internal node or
leaf). If the root can be considered as a leaf or an internal node, print
root.
_c_ 1..._c k_ is the list of children as a ordered tree.
Please follow the format presented in a sample output below. | s708055536 | Accepted | p02279 | The first line of the input includes an integer _n_ , the number of nodes of
the tree.
In the next _n_ lines, the information of each node _u_ is given in the
following format:
_id_ _k_ _c_ 1 _c_ 2 ... _c k_
where _id_ is the node ID of _u_ , _k_ is the degree of _u_ , _c_ 1 ... _c k_
are node IDs of 1st, ... _k_ th child of _u_. If the node does not have a
child, the _k_ is 0. | import sys
sys.setrecursionlimit(8388608)
N = int(input())
nodes = [None] * (N + 1)
childs = []
oya = []
class node_tree:
def __init__(self, node):
self.node = node
self.left = None
self.right = None
self.oya = None
for i in range(N):
node, child_num, *child = list(map(int, input().split()))
if nodes[node] is not None:
parent = nodes[node]
else:
parent = node_tree(node)
if child_num >= 1:
if nodes[child[0]] is not None:
tree = nodes[child[0]]
else:
tree = node_tree(child[0])
parent.left = tree
tree.oya = parent
nodes[node] = parent
nodes[child[0]] = tree
for j in range(len(child) - 1):
if nodes[child[j]] is not None:
treesecond = nodes[child[j]]
else:
treesecond = node_tree(child[j])
if nodes[child[j + 1]] is not None:
treenext = nodes[child[j + 1]]
else:
treenext = node_tree(child[j + 1])
treesecond.oya = parent
treenext.oya = parent
treesecond.right = treenext
nodes[child[j]] = treesecond
nodes[child[j + 1]] = treenext
else:
parent.left = nodes[-1]
nodes[node] = parent
def oyasearch(u):
if u.oya is not None:
oyasearch(u.oya)
else:
oya.append(u.node)
oyasearch(nodes[0])
dep = [0] * N
parent_list = [None] * N
parent_list[oya[0]] = -1
child_list = []
def search_depth(u, p):
dep[u.node] = p
if u.right is not None:
parent_list[u.right.node] = parent_list[u.node]
search_depth(u.right, p)
if u.left is not None:
parent_list[u.left.node] = u.node
search_depth(u.left, p + 1)
search_depth(nodes[oya[0]], 0)
def child_search(u, c):
c.append(u.node)
if u.right is not None:
child_search(u.right, c)
for i in range(N):
c = []
if nodes[i].left is not None:
child_search(nodes[i].left, c)
child_list.append(c)
word_list = ["internal node", "leaf"]
for i in range(N):
if i == oya[0]:
print(
(
"node %d: parent = %d, depth = %d, root,"
% (nodes[oya[0]].node, parent_list[oya[0]], dep[oya[0]])
),
child_list[oya[0]],
)
else:
if len(child_list[i]) != 0:
word = word_list[0]
else:
word = word_list[1]
print(
(
"node %d: parent = %d, depth = %d, %s,"
% (nodes[i].node, parent_list[i], dep[i], word)
),
child_list[i],
)
| Rooted Trees
A graph _G_ = (_V_ , _E_) is a data structure where _V_ is a finite set of
vertices and _E_ is a binary relation on _V_ represented by a set of edges.
Fig. 1 illustrates an example of a graph (or graphs).

**Fig. 1**
A free tree is a connnected, acyclic, undirected graph. A rooted tree is a
free tree in which one of the vertices is distinguished from the others. A
vertex of a rooted tree is called "node."
Your task is to write a program which reports the following information for
each node _u_ of a given rooted tree _T_ :
* node ID of _u_
* parent of _u_
* depth of _u_
* node type (root, internal node or leaf)
* a list of chidlren of _u_
If the last edge on the path from the root _r_ of a tree _T_ to a node _x_ is
(_p_ , _x_), then _p_ is the **parent** of _x_ , and _x_ is a **child** of
_p_. The root is the only node in _T_ with no parent.
A node with no children is an **external node** or **leaf**. A nonleaf node is
an **internal node**
The number of children of a node _x_ in a rooted tree _T_ is called the
**degree** of _x_.
The length of the path from the root _r_ to a node _x_ is the **depth** of _x_
in _T_.
Here, the given tree consists of _n_ nodes and evey node has a unique ID from
0 to _n_ -1.
Fig. 2 shows an example of rooted trees where ID of each node is indicated by
a number in a circle (node). The example corresponds to the first sample
input.

**Fig. 2** | [{"input": "13\n 0 3 1 4 10\n 1 2 2 3\n 2 0\n 3 0\n 4 3 5 6 7\n 5 0\n 6 0\n 7 2 8 9\n 8 0\n 9 0\n 10 2 11 12\n 11 0\n 12 0", "output": "node 0: parent = -1, depth = 0, root, [1, 4, 10]\n node 1: parent = 0, depth = 1, internal node, [2, 3]\n node 2: parent = 1, depth = 2, leaf, []\n node 3: parent = 1, depth = 2, leaf, []\n node 4: parent = 0, depth = 1, internal node, [5, 6, 7]\n node 5: parent = 4, depth = 2, leaf, []\n node 6: parent = 4, depth = 2, leaf, []\n node 7: parent = 4, depth = 2, internal node, [8, 9]\n node 8: parent = 7, depth = 3, leaf, []\n node 9: parent = 7, depth = 3, leaf, []\n node 10: parent = 0, depth = 1, internal node, [11, 12]\n node 11: parent = 10, depth = 2, leaf, []\n node 12: parent = 10, depth = 2, leaf, []"}, {"input": "4\n 1 3 3 2 0\n 0 0\n 3 0\n 2 0", "output": "node 0: parent = 1, depth = 1, leaf, []\n node 1: parent = -1, depth = 0, root, [3, 2, 0]\n node 2: parent = 1, depth = 1, leaf, []\n node 3: parent = 1, depth = 1, leaf, []"}] |
For each dataset, you should output the maximum number of parties possible. | s228293034 | Wrong Answer | p01286 | The input consists of multiple datasets. Each dataset has the following
format:
The first line of the input contains 7 non-negative integers _H_ , _W_ , _C_ ,
_M_ , _N W_, _N C_, and _N M_, each of which is less than or equals to 50. The
_i_ -th of the following _W_ lines contains the list of heroes who will be
getting along with the warrior _i_. The list begins with a non-negative
integer _n i_ , less than or equals to _H_. Then the rest of the line should
contain ni positive integers, each of which indicates the ID of a hero getting
along with the warrior _i_.
After these lists, the following _C_ lines contain the lists of warriors
getting along with the clerics in the same manner. The _j_ -th line contains a
list of warriors who will be getting along with the cleric _j_. Then the last
_M_ lines of the input contain the lists of clerics getting along with the
mages, of course in the same manner. The _k_ -th line contains a list of
clerics who will be getting along with the mage _k_.
The last dataset is followed by a line containing seven negative integers.
This line is not a part of any dataset and should not be processed. | import collections
class MaxFlow:
"""Dinic Algorithm: find max-flow
complexity: O(EV^2)
used in GRL6A(AOJ)
"""
class Edge:
def __init__(self, to, cap, rev):
self.to, self.cap, self.rev = to, cap, rev
def __init__(self, V):
"""V: the number of vertexes
E: adjacency list
source: start point
sink: goal point
"""
self.V = V
self.E = [[] for _ in range(V)]
def add_edge(self, fr, to, cap):
self.E[fr].append(self.Edge(to, cap, len(self.E[to])))
self.E[to].append(self.Edge(fr, 0, len(self.E[fr]) - 1))
def run(self, source, sink, INF=10**9):
"""find max-flow"""
maxflow = 0
while True:
self.bfs(source)
if self.level[sink] < 0:
return maxflow
self.itr = [0] * self.V
while True:
flow = self.dfs(source, sink, INF)
if flow > 0:
maxflow += flow
else:
break
def dfs(self, vertex, sink, flow):
"""find augmenting path"""
if vertex == sink:
return flow
for i in range(self.itr[vertex], len(self.E[vertex])):
self.itr[vertex] = i
e = self.E[vertex][i]
if e.cap > 0 and self.level[vertex] < self.level[e.to]:
d = self.dfs(e.to, sink, min(flow, e.cap))
if d > 0:
e.cap -= d
self.E[e.to][e.rev].cap += d
return d
return 0
def bfs(self, start):
"""find shortest path from start"""
que = collections.deque()
self.level = [-1] * self.V
que.append(start)
self.level[start] = 0
while que:
fr = que.popleft()
for e in self.E[fr]:
if e.cap > 0 and self.level[e.to] < 0:
self.level[e.to] = self.level[fr] + 1
que.append(e.to)
while True:
row = list(map(int, input().split()))
H, W, C, M, NW, NC, NM = row
if H == -1:
break
mf = MaxFlow(sum(row) + 2)
source = sum(row)
sink = source + 1
toH = lambda i: i
toW = lambda i: sum(row[:1]) + i
toC = lambda i: sum(row[:2]) + i
toM = lambda i: sum(row[:3]) + i
toNW = lambda i: sum(row[:4]) + i
toNC = lambda i: sum(row[:5]) + i
toNM = lambda i: sum(row[:6]) + i
for i in range(H):
mf.add_edge(source, toH(i), 1)
for j in range(NW):
mf.add_edge(toH(i), toNW(j), 1)
for i in range(W):
_, *friends = map(lambda x: int(x) - 1, input().split())
for friend in friends:
mf.add_edge(toH(friend), toW(i), 1)
for j in range(NC):
mf.add_edge(toW(i), toNC(j), 1)
for i in range(C):
_, *friends = map(lambda x: int(x) - 1, input().split())
for friend in friends:
mf.add_edge(toW(friend), toC(i), 1)
for j in range(NW):
mf.add_edge(toNW(j), toC(i), 1)
for j in range(NM):
mf.add_edge(toC(i), toNM(j), 1)
for i in range(M):
_, *friends = map(lambda x: int(x) - 1, input().split())
for friend in friends:
mf.add_edge(toC(friend), toM(i), 1)
for j in range(NC):
mf.add_edge(toNC(j), toM(i), 1)
mf.add_edge(toM(i), sink, 1)
for i in range(NM):
mf.add_edge(toNC(i), sink, 1)
print(mf.run(source, sink))
| D: Luigi's Tavern
Luigi's Tavern is a thriving tavern in the Kingdom of Nahaila. The owner of
the tavern Luigi supports to organize a party, because the main customers of
the tavern are adventurers. Each adventurer has a job: hero, warrior, cleric
or mage.
Any party should meet the following conditions:
* A party should have a hero.
* The warrior and the hero in a party should get along with each other.
* The cleric and the warrior in a party should get along with each other.
* The mage and the cleric in a party should get along with each other.
* It is recommended that a party has a warrior, a cleric, and a mage, but it is allowed that at most _N W_, _N C_ and _N m_ parties does not have a warrior, a cleric, and a mage respectively.
* A party without a cleric should have a warrior and a mage.
Now, the tavern has _H_ heroes, _W_ warriors, _C_ clerics and _M_ mages. Your
job is to write a program to find the maximum number of parties they can form. | [{"input": "1 1 1 1 1 1\n 1 1\n 1 1\n 1 1\n 1 1 1 1 0 0 0\n 1 1\n 1 1\n 1 1\n 1 0 1 0 1 0 1\n 0\n 1 1 0 1 0 1 0\n 0\n 0\n 1 1 0 1 0 1 0\n 1 1\n 0\n -1 -1 -1 -1 -1 -1 -1", "output": "1\n 1\n 0\n 1"}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s718340538 | Accepted | p02853 | Input is given from Standard Input in the following format:
X Y | a = input().split()
x = 0
if a[0] == "1" and a[1] == "1":
x = 1000000
elif a[0] == "1":
x = x + 300000
elif a[1] == "1":
x = x + 300000
if a[0] == "2":
x = x + 200000
if a[0] == "3":
x = x + 100000
if a[1] == "2":
x = x + 200000
if a[1] == "3":
x = x + 100000
print(x)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s915961242 | Runtime Error | p02853 | Input is given from Standard Input in the following format:
X Y | n = int(input())
ans = -1
num = 0
for i in range(n):
a, b = map(int, input().split())
ans += b
num += a * b
if num > 9:
ans += num // 9
num %= 9
if num == 0:
ans -= 1
num = 9
print(ans)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s163614199 | Accepted | p02853 | Input is given from Standard Input in the following format:
X Y | t = tuple(map(int, input().split()))
r = sum(max(4 - x, 0) * 100000 for x in t) + (sum(t) == 2 and 400000)
print(r)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s863639797 | Accepted | p02853 | Input is given from Standard Input in the following format:
X Y | x, y = [max(4 - int(i), 0) for i in input().split()]
print((x + y + 4 * (x > 2 < y)) * 10**5)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s613189423 | Accepted | p02853 | Input is given from Standard Input in the following format:
X Y | A, B = map(int, input().split())
sc = 0
if A == 1:
sc += 300000
elif A == 2:
sc += 200000
elif A == 3:
sc += 100000
if B == 1:
sc += 300000
if A == 1:
sc += 400000
elif B == 2:
sc += 200000
elif B == 3:
sc += 100000
print(sc)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s861024923 | Accepted | p02853 | Input is given from Standard Input in the following format:
X Y | x, y = [int(i) for i in input().split()]
mon = 0
money = [300000, 200000, 100000]
if x - 1 < 3:
mon += money[x - 1]
if y - 1 < 3:
mon += money[y - 1]
if x == y and x == 1:
mon += 400000
print(mon)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s655576655 | Accepted | p02853 | Input is given from Standard Input in the following format:
X Y | x, y = (max(4 - int(i), 0) for i in input().split())
print((x + y + 4 * (x == y == 3)) * 10**5)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s887776588 | Wrong Answer | p02853 | Input is given from Standard Input in the following format:
X Y | from sys import stdin
input = stdin.readline
m, *t = map(int, open(0).read().split())
def cal(d, c):
if c == 1:
return [0, d]
t = 1 + (d + d >= 10)
a = (d + d >= 10) + (d + d) % 10
ca = cal(a, c // 2)
if c % 2:
return [
(c // 2) * t + ca[0] + 1 + (ca[1] + d >= 10),
(ca[1] + d >= 10) + (ca[1] + d) % 10,
]
else:
return [(c // 2) * t + ca[0], ca[1]]
ans = 0
now = []
for d, c in zip(*[iter(t)] * 2):
ca = cal(d, c)
ans += ca[0]
now.append(ca[1])
pre = 0
for i in now:
pre = pre + i
ans += 1
if pre >= 10:
ans += 1
pre = pre % 10 + 1
print(ans - 1)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the amount of money DISCO-Kun earned, as an integer.
* * * | s215115706 | Runtime Error | p02853 | Input is given from Standard Input in the following format:
X Y | # import math
import bisect
import numpy as np
mod = 10**9 + 7
# http://cav-inet.hateblo.jp/entry/2017/02/18/165141
def binary_search(list, target):
result = -1
left = 0
right = len(list) - 1
while left <= right:
center = (left + right) // 2
if list[center] == target:
result = center
break
elif list[center] < target:
left = center + 1
elif list[center] > target:
right = center - 1
if result == -1:
return False
else:
return True
n = int(input())
a = list(map(int, input().split()))
c = np.cumsum(a)
s = c[n - 1]
if binary_search(c, c[n - 1] / 2):
print(0)
exit()
i = bisect.bisect_left(c, c[n - 1] / 2)
ans = min(2 * c[i] - s, s - c[i - 1] - 1)
print(ans)
| Statement
We held two competitions: Coding Contest and Robot Maneuver.
In each competition, the contestants taking the 3-rd, 2-nd, and 1-st places
receive 100000, 200000, and 300000 yen (the currency of Japan), respectively.
Furthermore, a contestant taking the first place in both competitions receives
an additional 400000 yen.
DISCO-Kun took the X-th place in Coding Contest and the Y-th place in Robot
Maneuver. Find the total amount of money he earned. | [{"input": "1 1", "output": "1000000\n \n\nIn this case, he earned 300000 yen in Coding Contest and another 300000 yen in\nRobot Maneuver. Furthermore, as he won both competitions, he got an additional\n400000 yen. In total, he made 300000 + 300000 + 400000 = 1000000 yen.\n\n* * *"}, {"input": "3 101", "output": "100000\n \n\nIn this case, he earned 100000 yen in Coding Contest.\n\n* * *"}, {"input": "4 4", "output": "0\n \n\nIn this case, unfortunately, he was the highest-ranked contestant without\nprize money in both competitions."}] |
Print the minimum necessary count of operations.
* * * | s585313932 | Accepted | p03741 | Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n | n = int(input())
lst = list(map(int, input().rstrip().split()))
# start with positive number
cur, cnt_1 = 0, 0
for i, j in enumerate(lst):
new = cur + j
if i % 2 == 0 and new <= 0:
cnt_1 += abs(new) + 1
cur = 1
elif i % 2 == 1 and new >= 0:
cnt_1 += abs(new) + 1
cur = -1
else:
cur = new
# start with negative number
cur, cnt_2 = 0, 0
for i, j in enumerate(lst):
new = cur + j
if i % 2 == 0 and new >= 0:
cnt_2 += abs(new) + 1
cur = -1
elif i % 2 == 1 and new <= 0:
cnt_2 += abs(new) + 1
cur = 1
else:
cur = new
print(min(cnt_1, cnt_2))
| Statement
You are given an integer sequence of length N. The i-th term in the sequence
is a_i. In one operation, you can select a term and either increment or
decrement it by one.
At least how many operations are necessary to satisfy the following
conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. | [{"input": "4\n 1 -3 1 0", "output": "4\n \n\nFor example, the given sequence can be transformed into 1, -2, 2, -2 by four\noperations. The sums of the first one, two, three and four terms are 1, -1, 1\nand -1, respectively, which satisfy the conditions.\n\n* * *"}, {"input": "5\n 3 -6 4 -5 7", "output": "0\n \n\nThe given sequence already satisfies the conditions.\n\n* * *"}, {"input": "6\n -1 4 3 2 -5 4", "output": "8"}] |
Print the minimum necessary count of operations.
* * * | s817192008 | Wrong Answer | p03741 | Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n | # coding: utf-8
import array, bisect, collections, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10**7)
INF = 10**20
MOD = 10**9 + 7
def II():
return int(input())
def ILI():
return list(map(int, input().split()))
def IAI(LINE):
return [ILI() for __ in range(LINE)]
def IDI():
return {key: value for key, value in ILI()}
def solve(n, a):
ans = 0
sum = a[0]
for i in range(1, n):
now = a[i]
next_sum = sum + now
if sum > 0:
if next_sum > 0:
ans += next_sum + 1
next_sum = -1
sum = next_sum
continue
elif next_sum < 0:
sum = next_sum
continue
elif next_sum == 0:
next_sum = -1
sum = next_sum
ans += 1
continue
elif sum < 0:
if next_sum > 0:
sum = next_sum
continue
elif next_sum < 0:
ans += abs(next_sum) + 1
next_sum = 1
sum = next_sum
continue
elif next_sum == 0:
next_sum = 1
sum = next_sum
ans += 1
continue
elif sum == 0:
sum = next_sum
continue
return ans
def main():
n = II()
a = ILI()
print(solve(n, a))
if __name__ == "__main__":
main()
| Statement
You are given an integer sequence of length N. The i-th term in the sequence
is a_i. In one operation, you can select a term and either increment or
decrement it by one.
At least how many operations are necessary to satisfy the following
conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. | [{"input": "4\n 1 -3 1 0", "output": "4\n \n\nFor example, the given sequence can be transformed into 1, -2, 2, -2 by four\noperations. The sums of the first one, two, three and four terms are 1, -1, 1\nand -1, respectively, which satisfy the conditions.\n\n* * *"}, {"input": "5\n 3 -6 4 -5 7", "output": "0\n \n\nThe given sequence already satisfies the conditions.\n\n* * *"}, {"input": "6\n -1 4 3 2 -5 4", "output": "8"}] |
Print the minimum necessary count of operations.
* * * | s200458624 | Accepted | p03741 | Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n | N = int(input())
prog = list(map(int, input().split()))
def seq(prog, first_Ev=False):
sign = -1 if first_Ev else 1
if sign * prog[0] > 0:
answer = 0
result = prog[0]
else:
answer = abs(sign - prog[0])
result = sign
for i in prog[1:]:
sign *= -1
result += i
if result * sign <= 0:
answer += abs(sign - result)
result = sign
return answer
print(min(seq(prog), seq(prog, first_Ev=True)))
| Statement
You are given an integer sequence of length N. The i-th term in the sequence
is a_i. In one operation, you can select a term and either increment or
decrement it by one.
At least how many operations are necessary to satisfy the following
conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. | [{"input": "4\n 1 -3 1 0", "output": "4\n \n\nFor example, the given sequence can be transformed into 1, -2, 2, -2 by four\noperations. The sums of the first one, two, three and four terms are 1, -1, 1\nand -1, respectively, which satisfy the conditions.\n\n* * *"}, {"input": "5\n 3 -6 4 -5 7", "output": "0\n \n\nThe given sequence already satisfies the conditions.\n\n* * *"}, {"input": "6\n -1 4 3 2 -5 4", "output": "8"}] |
Print the minimum necessary count of operations.
* * * | s401089883 | Runtime Error | p03741 | Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n | n = int(input())
lis = list(map(int,input().split()))
cou = 0
co = 0
ans1 = 0
ans2 = 0
for i in range(n):
cou += lis[i]
co += lis[i]
if cou <= 0 and i % 2 == 0:
ans1 += (1 -cou)
cou = 1
if cou => 0 and i % 2 == 1:
ans1 += (cou +1)
cou = -1
if co => 0 and i % 2 == 0:
ans2 += (co +1)
co = -1
if co <= 0 and i % 2 == 1:
ans2 += (1 -co)
co = 1
print(min(ans1,ans2)) | Statement
You are given an integer sequence of length N. The i-th term in the sequence
is a_i. In one operation, you can select a term and either increment or
decrement it by one.
At least how many operations are necessary to satisfy the following
conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. | [{"input": "4\n 1 -3 1 0", "output": "4\n \n\nFor example, the given sequence can be transformed into 1, -2, 2, -2 by four\noperations. The sums of the first one, two, three and four terms are 1, -1, 1\nand -1, respectively, which satisfy the conditions.\n\n* * *"}, {"input": "5\n 3 -6 4 -5 7", "output": "0\n \n\nThe given sequence already satisfies the conditions.\n\n* * *"}, {"input": "6\n -1 4 3 2 -5 4", "output": "8"}] |
Print the minimum necessary count of operations.
* * * | s465749561 | Accepted | p03741 | Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n | def ARC072C():
N = int(input())
a = list(map(int, input().split()))
cnt1 = 0
cnt2 = 0
sum1 = a[0]
sum2 = a[0]
if sum1 <= 0:
cnt1 = 1 - a[0]
sum1 = 1
if sum2 >= 0:
cnt2 = abs(-1 - a[0])
sum2 = -1
j = 1
# print(sum1, sum2)
for i in range(1, N):
sum1 += a[i]
sum2 += a[i]
if j % 2 == 0:
if sum1 <= 0:
cnt1 += 1 - sum1
sum1 = 1
if sum2 >= 0:
cnt2 += abs(-1 - sum2)
sum2 = -1
else:
if sum1 >= 0:
cnt1 += abs(-1 - sum1)
sum1 = -1
if sum2 <= 0:
cnt2 += 1 - sum2
sum2 = 1
j += 1
# print(sum1, sum2)
# print(cnt1, cnt2)
print(min(cnt1, cnt2))
ARC072C()
| Statement
You are given an integer sequence of length N. The i-th term in the sequence
is a_i. In one operation, you can select a term and either increment or
decrement it by one.
At least how many operations are necessary to satisfy the following
conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. | [{"input": "4\n 1 -3 1 0", "output": "4\n \n\nFor example, the given sequence can be transformed into 1, -2, 2, -2 by four\noperations. The sums of the first one, two, three and four terms are 1, -1, 1\nand -1, respectively, which satisfy the conditions.\n\n* * *"}, {"input": "5\n 3 -6 4 -5 7", "output": "0\n \n\nThe given sequence already satisfies the conditions.\n\n* * *"}, {"input": "6\n -1 4 3 2 -5 4", "output": "8"}] |
Print the minimum necessary count of operations.
* * * | s895934085 | Accepted | p03741 | Input is given from Standard Input in the following format:
n
a_1 a_2 ... a_n | # -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
class Scanner:
@staticmethod
def int():
return int(sys.stdin.readline().rstrip())
@staticmethod
def string():
return sys.stdin.readline().rstrip()
@staticmethod
def map_int():
return [int(x) for x in Scanner.string().split()]
@staticmethod
def string_list(n):
return [input() for i in range(n)]
@staticmethod
def int_list_list(n):
return [Scanner.map_int() for i in range(n)]
@staticmethod
def int_cols_list(n):
return [int(input()) for i in range(n)]
class Math:
@staticmethod
def gcd(a, b):
if b == 0:
return a
return Math.gcd(b, a % b)
@staticmethod
def lcm(a, b):
return (a * b) // Math.gcd(a, b)
@staticmethod
def roundUp(a, b):
return -(-a // b)
@staticmethod
def toUpperMultiple(a, x):
return Math.roundUp(a, x) * x
@staticmethod
def toLowerMultiple(a, x):
return (a // x) * x
@staticmethod
def nearPow2(n):
if n <= 0:
return 0
if n & (n - 1) == 0:
return n
ret = 1
while n > 0:
ret <<= 1
n >>= 1
return ret
@staticmethod
def sign(n):
if n == 0:
return 0
if n < 0:
return -1
return 1
@staticmethod
def isPrime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
d = int(n**0.5) + 1
for i in range(3, d + 1, 2):
if n % i == 0:
return False
return True
class PriorityQueue:
def __init__(self, l=[]):
self.__q = l
heapq.heapify(self.__q)
return
def push(self, n):
heapq.heappush(self.__q, n)
return
def pop(self):
return heapq.heappop(self.__q)
MOD = int(1e09) + 7
INF = int(1e15)
def main():
# sys.stdin = open("sample.txt")
N = Scanner.int()
A = Scanner.map_int()
s1, s2 = 0, 0
c1, c2 = 0, 0
for i in range(N):
if i % 2 == 0:
if s1 + A[i] > 0:
s1 += A[i]
else:
c1 += abs(s1 + A[i]) + 1
s1 = 1
if s2 + A[i] < 0:
s2 += A[i]
else:
c2 += abs(s2 + A[i]) + 1
s2 = -1
else:
if s1 + A[i] < 0:
s1 += A[i]
else:
c1 += abs(s1 + A[i]) + 1
s1 = -1
if s2 + A[i] > 0:
s2 += A[i]
else:
c2 += abs(s2 + A[i]) + 1
s2 = 1
print(min(c1, c2))
return
if __name__ == "__main__":
main()
| Statement
You are given an integer sequence of length N. The i-th term in the sequence
is a_i. In one operation, you can select a term and either increment or
decrement it by one.
At least how many operations are necessary to satisfy the following
conditions?
* For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.
* For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. | [{"input": "4\n 1 -3 1 0", "output": "4\n \n\nFor example, the given sequence can be transformed into 1, -2, 2, -2 by four\noperations. The sums of the first one, two, three and four terms are 1, -1, 1\nand -1, respectively, which satisfy the conditions.\n\n* * *"}, {"input": "5\n 3 -6 4 -5 7", "output": "0\n \n\nThe given sequence already satisfies the conditions.\n\n* * *"}, {"input": "6\n -1 4 3 2 -5 4", "output": "8"}] |
Print the minimum possible sadness of Snuke.
* * * | s646554556 | Accepted | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | _, a = open(0)
a = sorted(int(x) - i for i, x in enumerate(a.split()))
print(sum(abs(i - a[len(a) // 2]) for i in a))
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s143415528 | Accepted | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians
sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9 + 7
def lcm(x, y):
return x * y // gcd(x, y)
def lgcd(l):
return reduce(gcd, l)
def llcm(l):
return reduce(lcm, l)
def powmod(n, i, mod=MOD):
return pow(n, mod - 1 + i, mod) if i < 0 else pow(n, i, mod)
def div2(x):
return x.bit_length()
def div10(x):
return len(str(x)) - (x == 0)
def intput():
return int(input())
def mint():
return map(int, input().split())
def lint():
return list(map(int, input().split()))
def ilint():
return int(input()), list(map(int, input().split()))
def judge(x, l=["Yes", "No"]):
print(l[0] if x else l[1])
def lprint(l, sep="\n"):
for x in l:
print(x, end=sep)
def ston(c, c0="a"):
return ord(c) - ord(c0)
def ntos(x, c0="a"):
return chr(x + ord(c0))
class counter(dict):
def __init__(self, *args):
super().__init__(args)
def add(self, x, d=1):
self.setdefault(x, 0)
self[x] += d
def list(self):
l = []
for k in self:
l.extend([k] * self[k])
return l
class comb:
def __init__(self, n, mod=None):
self.l = [1]
self.n = n
self.mod = mod
def get(self, k):
l, n, mod = self.l, self.n, self.mod
k = n - k if k > n // 2 else k
while len(l) <= k:
i = len(l)
l.append(
l[i - 1] * (n + 1 - i) // i
if mod == None
else (l[i - 1] * (n + 1 - i) * powmod(i, -1, mod)) % mod
)
return l[k]
def pf(x, mode="counter"):
C = counter()
p = 2
while x > 1:
k = 0
while x % p == 0:
x //= p
k += 1
if k > 0:
C.add(p, k)
p = p + 2 - (p == 2) if p * p < x else x
if mode == "counter":
return C
S = set([1])
for k in C:
T = deepcopy(S)
for x in T:
for i in range(1, C[k] + 1):
S.add(x * (k**i))
if mode == "set":
return S
if mode == "list":
return sorted(list(S))
######################################################
N, A = ilint()
A = [A[i] - (i + 1) for i in range(N)]
A.sort()
k = A[N // 2]
ans = 0
for a in A:
ans += abs(a - k)
print(ans)
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s745836075 | Runtime Error | p03311 | 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()))
aa = [ai - i-1 for i,ai in enumerate(a)]
def tri_search(f:"f(x:float)->float", left, right, iter=100)->float:
for _ in range(iter):
ml = (left*2 + right) / 3
mr = (left + right*2) / 3
if f(ml) < f(mr):
right = mr
else:
left = ml
# print(left, right)
return (right + left) / 2
def f(x):
ret = 0
x = int(x+0.5)
for aai in aa:
ret += abs(aai - x)
return ret
b = tri_search(f, -10**9, 10**9)
print(min(f(b), f(b)+1, f(b)-1)
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s050252812 | Runtime Error | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | def total(inlist,k):
print(sum([ abs(i-k) for i in inlist ]))
return
n = int(input())
a_list = [ int(v) for v in input().split() ]
a_list = [ v-i for i, v in enumerate(a_list,1) ]
a_list.sort()
total(a_list,a_list[n//2])v | Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s044680149 | Runtime Error | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | n = ir()
a = lr()
total = 0
for i, num in enumerate(a):
a[i] = num - (i + 1)
mean = sum(a) // n - 1
ans = [0, 0, 0]
for i in range(mean, mean + 3):
tmp = i % 3
for num in a:
ans[tmp] += abs(num - i)
print(min(ans))
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s957456343 | Runtime Error | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | def slove():
import sys
input = sys.stdin.readline
n = int(input().rstrip('\n'))
a = list(map(int, input().rstrip('\n').split()))
b = []
for i, v in enumerate(a):
b.append(v - (i + 1))
b.sort()
t = b[len(b) // 2]
s = 0
for i, v in enumerate(a):
s += abs(v - (t + (i + 1)))
print(s)
if __name__ == '__main__':
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s339724438 | Runtime Error | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | import sys
def solve():
readline = sys.stdin.buffer.readline
mod = 10 ** 9 + 7
n = int(readline())
a = list(map(int, readline().split()))
for i in range(n):
a[i] -= i + 1
a.sort()
t1 = 0
t2 = 0
t2 += abs(a[i] - a[n//2 - 1])
print(min(t1, t2))
if __name__ == '__main__':
solve()
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s182559804 | Wrong Answer | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | n = int(input())
print(sum(map(int, input().split())))
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s968160099 | Runtime Error | p03311 | 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()))
for i in range(len(a)):
a[i] -= i + 1
a.sort()
b = [a[0]]
for i in a[1:]:
b.append(b[-1] + i)
hi = len(a)
lo = 0
def score(index):
return abs((lft * a[index] - b[index]) - (b[-1] - b[index] - (len(a) - index) * a[index]))
for i in range(10000):
lft = (hi + 2 * lo) // 3
rgt = (2 * hi + lo) // 3
ltmp = score(lft)
rtmp = score(rgt)
if ltmp <= rtmp:
lo = lft
else:
hi = rgt
printmin([(score(lo), score(lo + 1)]))
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s414016213 | Runtime Error | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | #include<iostream>
#include<vector>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<set>
#include<map>
#include<cmath>
#include<string>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
typedef long long int llint;
typedef pair<ll, ll> pa;
#define MM 1000000000
#define MOD MM+7
#define MAX 101000
#define MAP 110
#define initial_value -1
#define MAX_T 1001
#define Pair pair<int,int>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
#define INF (1 << 29) //536870912
const long double PI = acos(-1);
const ll DEP = 1e18;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
ll N;
ll a[200002];
vector<ll> sum(0);
int main(){
cin >> N;
for(int i = 0; i < N; i++){
cin >> a[i];
sum.push_back(a[i] - i - 1);
}
ll ans = 0;
sort(sum.begin(),sum.end());
if(N % 2 == 1){
ll mid = a[N/2];
for(int i = 0; i < N; i++){
ans += sum[i] - mid;
}
cout << ans << endl;
return 0;
} else {
ll mid = (a[N/2] + a[N/2-1])/2;
for(int i = 0; i < N; i++){
ans += sum[i] - mid;
}
cout << ans << endl;
return 0;
}
}
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s292114858 | Runtime Error | p03311 | 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()))
from bisect import bisect_left
for i in range(N):
A[i] = A[i]-i
A.sort()
S = [0]*(N+1)
for i in range(N):
S[i+1] = S[i]+A[i]
sumA = sum(A)
maxA,minA = max(A),min(A)
Ans = 10**9
for i in range(3z00):
l = bisect_left(A,minA)
low = abs(minA*l - S[l]) + abs(S[N]-S[l] - minA*(N-l))
h = bisect_left(A,maxA)
high = abs(maxA*h - S[h]) + abs(S[N]-S[h] - maxA*(N-h))
if low==high:
minA += 1
elif low < high:
maxA = (maxA+minA)//2
else:
minA = (maxA+minA)//2
#print(Ans,low,high,minA,maxA)
Ans = min(Ans,low,high)
print(Ans) | Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s508927048 | Runtime Error | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
using namespace std;
typedef long long ll;
const int MAX_N=2e5;
const ll INF=1e12;
int N;
ll A[MAX_N];
int main(){
cin>>N;
REP(i,N){
cin>>A[i];
}
ll lb=-INF,ub=INF;
while(ub-lb>1){
ll mid=(lb+ub)/2;
int c1,c2;
c1=c2=0;
REP(i,N){
if (A[i]>mid+i+1){
c1++;
}else{
c2++;
}
}
if (c1<=c2){
ub=mid;
}else{
lb=mid;
}
}
ll ans=0;
REP(i,N){
ans+=llabs(A[i]-(ub+i+1));
}
cout<<ans<<endl;
return 0;
}
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s034734888 | Runtime Error | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | def func(A,b):
s=0
for i in range(len(A)):
s+=abs(A[i]-(b+i))
return s
N=int(input())
A=[int(i) for i in input().split(' ')]
min=None
for b in range(A[0],A[-1]-len(A)+1):
if min=None:
min=func(A,b)
elif min>func(A,b):
min=func(A,b)
print(min) | Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s918845765 | Wrong Answer | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | while True:
try:
n = int(input())
num = list(map(int, input().split()))
for ind in range(len(num)):
num[ind] -= ind + 1
d = sum(num) // n
print(sum(abs(i - d) for i in num))
except:
break
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s917759726 | Runtime Error | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | i = int
k = input
N = i(k())
l = list(map(i, k().split()))
for j in range(n):
lis[j] -= j + 1
l.sort()
print(sum(list(map(lambda x: abs(x - l[N // 2]), l))))
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s821855858 | Wrong Answer | p03311 | 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()))
b = []
plus = []
minus = []
zero = []
for i in range(n):
tmp = a[i] - (i + 1)
if tmp > 0:
plus.append(tmp)
elif tmp == 0:
zero.append(tmp)
else:
minus.append(tmp)
b.append(a[i] - (i + 1))
kouho = []
kouho.append(0)
if len(plus) == 0:
pass
else:
a_p = round(sum(plus) / len(plus))
for i in range(3):
kouho.append(a_p + i - 1)
if len(minus) == 0:
pass
else:
a_m = round(sum(minus) / len(minus))
for i in range(3):
kouho.append(a_m + i - 1)
s = []
for i in kouho:
s_tmp = 0
for k in b:
s_tmp += abs(k - i)
s.append(s_tmp)
print(min(s))
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s767387816 | Accepted | p03311 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | n = int(input())
an = sorted([int(j) - (i + 1) for i, j in enumerate(input().split())])
print(sum(abs(i - an[len(an) // 2]) for i in an))
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the minimum possible sadness of Snuke.
* * * | s888729804 | Wrong Answer | p03311 | 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()))
A2 = sorted([A[i] - i - 1 for i in range(N)])
if N == 1:
print(A[0] - 1)
elif N == 2:
print(abs((A[0] - 1) - (A[1] - 2)))
else:
mid1 = A2[N // 2]
mid2 = A2[N // 2 + 1]
sum1 = 0
sum2 = 0
for i in range(N):
sum1 += abs(A2[i] - mid1)
sum2 += abs(A2[i] - mid2)
print(min(sum1, sum2))
| Statement
Snuke has an integer sequence A of length N.
He will freely choose an integer b. Here, he will get sad if A_i and b+i are
far from each other. More specifically, the _sadness_ of Snuke is calculated
as follows:
* abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))
Here, abs(x) is a function that returns the absolute value of x.
Find the minimum possible sadness of Snuke. | [{"input": "5\n 2 2 3 5 5", "output": "2\n \n\nIf we choose b=0, the sadness of Snuke would be\nabs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2. Any choice\nof b does not make the sadness of Snuke less than 2, so the answer is 2.\n\n* * *"}, {"input": "9\n 1 2 3 4 5 6 7 8 9", "output": "0\n \n\n* * *"}, {"input": "6\n 6 5 4 3 2 1", "output": "18\n \n\n* * *"}, {"input": "7\n 1 1 1 1 2 3 4", "output": "6"}] |
Print the sum of the satisfaction points Takahashi gained, as an integer.
* * * | s676908410 | Accepted | p02916 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1} | N = int(input())
a = input()
a_ = a__ = [int(i) for i in a.split()]
b = input()
b_ = b__ = [int(i) for i in b.split()]
c = input()
c_ = c__ = [int(i) for i in c.split()]
sat = 0
first = a_[0] - 1
sat = sat + b_[first]
for x in a_:
if x > N:
a_.remove(x)
for x in b_:
if x < 1 or x > 50:
b__ = b_.remove(x)
for x in c_:
if x < 1 or x > 50:
c__ = c_.remove(x)
a__ = list(dict.fromkeys(a_))
if len(a__) == len(a_) and len(c__) == len(c_) and len(b__) == len(b_) and 2 <= N <= 50:
for x in range(1, N):
if a_[x] == 1 + a_[x - 1]:
sat = sat + b_[(a_[x]) - 1] + c_[(a_[x]) - 2]
else:
sat = sat + b_[(a_[x]) - 1]
if sat != 0:
print(sat)
| Statement
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all
of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N
- 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained. | [{"input": "3\n 3 1 2\n 2 5 4\n 3 6", "output": "14\n \n\nTakahashi gained 14 satisfaction points in total, as follows:\n\n * First, he ate Dish 3 and gained 4 satisfaction points.\n * Next, he ate Dish 1 and gained 2 satisfaction points.\n * Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.\n\n* * *"}, {"input": "4\n 2 3 4 1\n 13 5 8 24\n 45 9 15", "output": "74\n \n\n* * *"}, {"input": "2\n 1 2\n 50 50\n 50", "output": "150"}] |
Print the sum of the satisfaction points Takahashi gained, as an integer.
* * * | s342744973 | Accepted | p02916 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1} | f = lambda: list(map(int, input().split()))
n = int(input())
a, b, c = f(), f(), f()
print(sum(b) + sum(c[a[i] - 1] for i in range(n - 1) if a[i + 1] - a[i] == 1))
| Statement
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all
of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N
- 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained. | [{"input": "3\n 3 1 2\n 2 5 4\n 3 6", "output": "14\n \n\nTakahashi gained 14 satisfaction points in total, as follows:\n\n * First, he ate Dish 3 and gained 4 satisfaction points.\n * Next, he ate Dish 1 and gained 2 satisfaction points.\n * Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.\n\n* * *"}, {"input": "4\n 2 3 4 1\n 13 5 8 24\n 45 9 15", "output": "74\n \n\n* * *"}, {"input": "2\n 1 2\n 50 50\n 50", "output": "150"}] |
Print the sum of the satisfaction points Takahashi gained, as an integer.
* * * | s926511736 | Wrong Answer | p02916 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1} | L = [
int(input()),
list(map(int, input().split(" "))),
list(map(int, input().split(" "))),
list(map(int, input().split(" "))),
]
print(
sum(
[
(
L[2][L[1][i] - 1] + L[3][i - 2]
if i > 0 and L[1][i] - L[1][i - 1] == 1
else L[2][L[1][i] - 1]
)
for i in range(L[0])
]
)
)
| Statement
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all
of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N
- 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained. | [{"input": "3\n 3 1 2\n 2 5 4\n 3 6", "output": "14\n \n\nTakahashi gained 14 satisfaction points in total, as follows:\n\n * First, he ate Dish 3 and gained 4 satisfaction points.\n * Next, he ate Dish 1 and gained 2 satisfaction points.\n * Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.\n\n* * *"}, {"input": "4\n 2 3 4 1\n 13 5 8 24\n 45 9 15", "output": "74\n \n\n* * *"}, {"input": "2\n 1 2\n 50 50\n 50", "output": "150"}] |
Print the sum of the satisfaction points Takahashi gained, as an integer.
* * * | s136103801 | Accepted | p02916 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1} | food_number = int(input())
food_history = list(map(int, input().split()))
point_b = list(map(int, input().split()))
point_c = list(map(int, input().split()))
total_point = 0
for x in range(food_number):
total_point += point_b[food_history[x] - 1]
if x >= 1 and food_history[x] - 1 == food_history[x - 1]:
total_point += point_c[food_history[x - 1] - 1]
print("{}".format(total_point))
| Statement
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all
of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N
- 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained. | [{"input": "3\n 3 1 2\n 2 5 4\n 3 6", "output": "14\n \n\nTakahashi gained 14 satisfaction points in total, as follows:\n\n * First, he ate Dish 3 and gained 4 satisfaction points.\n * Next, he ate Dish 1 and gained 2 satisfaction points.\n * Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.\n\n* * *"}, {"input": "4\n 2 3 4 1\n 13 5 8 24\n 45 9 15", "output": "74\n \n\n* * *"}, {"input": "2\n 1 2\n 50 50\n 50", "output": "150"}] |
Print the sum of the satisfaction points Takahashi gained, as an integer.
* * * | s011973570 | Runtime Error | p02916 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1} | n = int(input()) # waiting_for_pudging
a = list(map(int, input().split())) # waiting_for_pudging
b = list(map(int, input().split())) # waiting_for_pudging
c = list(map(int, input().split())) # waiting_for_pudging
summ = 0 # waiting_for_pudging
for i in range(n - 1): # pudging
if a[i] + 1 == a[i + 1]: # pudging
summ += c[a[i]] # pudging
summ += b[a[i]] # pudging
else: # pudging
summ += b[a[i - 1]] # pudging
print(summ) # pudging
# accepted
| Statement
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all
of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N
- 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained. | [{"input": "3\n 3 1 2\n 2 5 4\n 3 6", "output": "14\n \n\nTakahashi gained 14 satisfaction points in total, as follows:\n\n * First, he ate Dish 3 and gained 4 satisfaction points.\n * Next, he ate Dish 1 and gained 2 satisfaction points.\n * Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.\n\n* * *"}, {"input": "4\n 2 3 4 1\n 13 5 8 24\n 45 9 15", "output": "74\n \n\n* * *"}, {"input": "2\n 1 2\n 50 50\n 50", "output": "150"}] |
Print the sum of the satisfaction points Takahashi gained, as an integer.
* * * | s456189251 | Accepted | p02916 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1} | n1 = int(input())
n2 = [int(i) for i in input().split()]
n3 = [int(i) for i in input().split()]
n4 = [int(i) for i in input().split()]
r1 = 0
for i1 in range(n1):
r1 += n3[n2[i1] - 1]
if i1 <= n1 - 2:
if n2[i1] + 1 == n2[i1 + 1]:
r1 += n4[n2[i1] - 1]
print(r1)
| Statement
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all
of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N
- 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained. | [{"input": "3\n 3 1 2\n 2 5 4\n 3 6", "output": "14\n \n\nTakahashi gained 14 satisfaction points in total, as follows:\n\n * First, he ate Dish 3 and gained 4 satisfaction points.\n * Next, he ate Dish 1 and gained 2 satisfaction points.\n * Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.\n\n* * *"}, {"input": "4\n 2 3 4 1\n 13 5 8 24\n 45 9 15", "output": "74\n \n\n* * *"}, {"input": "2\n 1 2\n 50 50\n 50", "output": "150"}] |
Print the sum of the satisfaction points Takahashi gained, as an integer.
* * * | s182013806 | Accepted | p02916 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1} | N = int(input())
lsA = [int(s) for s in input().split()]
lsB = [int(s) for s in input().split()]
lsC = [int(s) for s in input().split()]
sum = lsB[lsA[0] - 1]
for i in range(1, N):
a = lsA[i]
if lsA[i - 1] + 1 == a:
sum += lsB[a - 1] + lsC[a - 2]
else:
sum += lsB[a - 1]
print(sum)
| Statement
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all
of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N
- 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained. | [{"input": "3\n 3 1 2\n 2 5 4\n 3 6", "output": "14\n \n\nTakahashi gained 14 satisfaction points in total, as follows:\n\n * First, he ate Dish 3 and gained 4 satisfaction points.\n * Next, he ate Dish 1 and gained 2 satisfaction points.\n * Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.\n\n* * *"}, {"input": "4\n 2 3 4 1\n 13 5 8 24\n 45 9 15", "output": "74\n \n\n* * *"}, {"input": "2\n 1 2\n 50 50\n 50", "output": "150"}] |
Print the sum of the satisfaction points Takahashi gained, as an integer.
* * * | s505661762 | Runtime Error | p02916 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1} | N = int(input())
listA = list(map(int, input().split()))
listB = list(map(int, input().split()))
listC = list(map(int, input().split()))
sumB = int(sum(listB))
print("sumB", sumB)
for i in range(1, N - 1):
if listA[i] == listA[i - 1] + 1:
sumB = sumB + int(listC[listA[i - 2]])
print(sumB)
| Statement
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all
of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N
- 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained. | [{"input": "3\n 3 1 2\n 2 5 4\n 3 6", "output": "14\n \n\nTakahashi gained 14 satisfaction points in total, as follows:\n\n * First, he ate Dish 3 and gained 4 satisfaction points.\n * Next, he ate Dish 1 and gained 2 satisfaction points.\n * Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.\n\n* * *"}, {"input": "4\n 2 3 4 1\n 13 5 8 24\n 45 9 15", "output": "74\n \n\n* * *"}, {"input": "2\n 1 2\n 50 50\n 50", "output": "150"}] |
Print the maximum possible number of happy children.
* * * | s400734176 | Accepted | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | # _*_ coding:utf-8 _*_
# Atcoder_Beginners_Contest-
# Atcoder_Grand_Contest-
# TODO https://atcoder.jp/contests/abc/tasks/abc_
import time
from contextlib import contextmanager
# from time import sleep
@contextmanager
def timer(title):
t0 = time.time()
yield
print("{} - done in {:.0f}s".format(title, time.time() - t0))
def countJoyChild(haveCookie, joyMeterList):
joyMeterList.sort()
childsCount = len(joyMeterList)
sendCookieRangeMax = range(0, childsCount, +1)
sendCookieChilds = 0
for i in sendCookieRangeMax:
haveCookie = haveCookie - joyMeterList[i]
if 0 <= haveCookie:
sendCookieChilds = sendCookieChilds + 1
else:
break
if 0 < haveCookie: # => 最後の一人が多すぎる
sendCookieChilds = sendCookieChilds - 1
answer = sendCookieChilds
return answer
if __name__ == "__main__":
_, X = map(int, input().strip().split(" "))
A = list(map(int, input().strip().split(" ")))
# with timer("countJoyChild"):
solution = countJoyChild(X, A)
print("{}".format(solution))
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s699207776 | Accepted | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | import sys
## io ##
def IS():
return sys.stdin.readline().rstrip()
def II():
return int(IS())
def MII():
return list(map(int, IS().split()))
def MIIZ():
return list(map(lambda x: x - 1, MII()))
## dp ##
def DD2(d1, d2, init=0):
return [[init] * d2 for _ in range(d1)]
def DD3(d1, d2, d3, init=0):
return [DD2(d2, d3, init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str:
return format(x, "b") # rev => int(res, 2)
def to_oct(x: int) -> str:
return format(x, "o") # rev => int(res, 8)
def to_hex(x: int) -> str:
return format(x, "x") # rev => int(res, 16)
MOD = 10**9 + 7
def divc(x, y) -> int:
return -(-x // y)
def divf(x, y) -> int:
return x // y
def gcd(x, y):
while y:
x, y = y, x % y
return x
def lcm(x, y):
return x * y // gcd(x, y)
def enumerate_divs(n):
"""Return a tuple list of divisor of n"""
return [(i, n // i) for i in range(1, int(n**0.5) + 1) if n % i == 0]
def get_primes(MAX_NUM=10**3):
"""Return a list of prime numbers n or less"""
is_prime = [True] * (MAX_NUM + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(MAX_NUM**0.5) + 1):
if not is_prime[i]:
continue
for j in range(i * 2, MAX_NUM + 1, i):
is_prime[j] = False
return [i for i in range(MAX_NUM + 1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
# ======================================================#
def main():
n, x = MII()
aa = MII()
aa.sort()
cnt = 0
if x > sum(aa):
cnt -= 1
for a in aa:
if x >= a:
x -= a
cnt += 1
print(cnt)
if __name__ == "__main__":
main()
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s577329250 | Wrong Answer | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | n, x = map(int, input().split())
a = list(map(int, input().split()))
def score(a, m, x):
ans = x * (n + m)
l = n // m
for i in range(n):
t = (n - 1 - i) // m
if i < m:
ans += ((t + 2) ** 2) * a[i]
else:
ans += ((t + 2) ** 2) * (a[i] - a[i - m])
for i in range(m):
ans += a[n - 1 - i]
return ans
def search(left, right, l_score, r_score, a, x):
tri = (right - left) // 3
u_score = score(a, left + tri, x)
v_score = score(a, left + tri * 2, x)
if right - left <= 3:
return min(u_score, v_score, l_score, r_score)
elif l_score < u_score:
return search(left, left + tri, l_score, u_score, a, x)
elif r_score < v_score:
return search(left + tri * 2, right, v_score, r_score, a, x)
elif u_score < v_score:
return search(left, left + tri * 2, l_score, v_score, a, x)
else:
return search(left + tri, right, u_score, r_score, a, x)
def sub_search(r, a, x):
ans = score(a, r // 2 + 1, x)
# print(ans)
for i in range(r // 2):
j = r // 2 - i
ddd = score(a, j, x)
# print(ddd)
if ddd <= ans:
ans = ddd
else:
break
return ans
# k = search(1, n, score(a, 1, x), score(a, n, x), a, x)
k = sub_search(n, a, x)
print(k)
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s788447621 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | N,x=map(int,input().split())
a=list(map(int,input().split()))
b=sorted(a)
c=0
while True:
if sum(b)=x:
print(x)
break
elif b[c]<=x:
x=x-b[c]
c+=1
else:
break
print(c)
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s005982925 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | N,x=map(int,input().split())
a=list(map(int,input().split()))
b=sorted(a)
c=0
if sum(b)=x:
print(x)
while True:
if b[c]<=x:
x=x-b[c]
c+=1
else:
break
print(c)
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s833224425 | Wrong Answer | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | N, M = [int(i) for i in input().split(" ")]
import sys
from collections import defaultdict
sys.setrecursionlimit(100000)
s = input().strip()
ds = [defaultdict(set), defaultdict(set)]
starts = set()
for ll in sys.stdin:
i, j = ll.strip().split(" ")
i, j = int(i) - 1, int(j) - 1
ds[0 if s[i] == s[j] else 1][str(0 if s[i] == s[j] else 1) + str(i)].add(
str(1 if s[i] == s[j] else 0) + str(j)
)
ds[0 if s[i] == s[j] else 1][str(0 if s[i] == s[j] else 1) + str(j)].add(
str(1 if s[i] == s[j] else 0) + str(i)
)
if s[i] == "A" and s[j] == "A":
starts.add(str(0) + str(i))
checked = set()
path = set()
# path = []
def walk(i, depth):
dd = ds[depth]
checked.add(i)
if len(dd[i]) == 0:
return False
r = False
for item in dd[i]:
if item in path:
return True
if item not in checked:
path.add(item)
# path.append(item)
r = r or walk(item, (depth + 1) % 2)
path.remove(item)
# path.pop()
return r
result = False
for item in starts:
if item not in checked:
path.add(item)
# path.append(item)
result = result or walk(item, 0)
path.remove(item)
# path.pop()
if result:
print("Yes")
else:
print("No")
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s727378974 | Wrong Answer | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | import copy
n, x = list(map(int, input().split()))
a_list = list(map(int, input().split()))
res_count = 0
def rec_count(a_list, ix=0):
global res_count
if len(a_list) == 0 or len(a_list) <= res_count:
return None
sum_a = sum(a_list)
if sum_a == x:
return len(a_list)
if x > sum_a:
return len(a_list) - 1
c_list = [0]
for i in range(ix, len(a_list)):
tmp_a_list = copy.deepcopy(a_list)
del tmp_a_list[ix]
tmp_count = rec_count(tmp_a_list, i)
if tmp_count is not None:
c_list.append(tmp_count)
if tmp_count > res_count:
res_count = tmp_count
return max(c_list)
tmp_count = rec_count(a_list)
if tmp_count > res_count:
res_count = tmp_count
print(res_count)
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s115601588 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | N, x = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
cnt = 0
for ele in arr:
if x < ele:
break
x -= ele
cnt += 1
if cnt = N and x > 0:
cnt -= 1
print(cnt) | Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s217165471 | Wrong Answer | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | # child_count = 3
# candy_sum = 70
# requirement_list = [20, 30, 10]
N = 2 # 入力は2行
# for i in range(N):
# line = input()
# print(type(line)) # str
# print(line) # 1行目だけ
for i in range(N):
line = input() # 入力を1行受け取る
if i == 0: # 1行目
child_count, candy_sum = map(int, line.split())
else: # 2行目
requirement_list = list(map(int, line.split()))
# print(child_count, candy_sum, requirement_list)
satisfied_count = 0
requirement_list.sort() # 昇順に並び替え
requirement_list.reverse() # 降順に並び替え
# print(requirement_list)
# 最後の人を考慮漏れ
for i in range(child_count): # len(requirement_list) == child_count
requirement = requirement_list[i]
# print(i, candy_sum, requirement)
if i == child_count - 1: # 最後の人(残りのお菓子がほしい数と一致するか)
if requirement == candy_sum:
satisfied_count += 1
else: # 1人目からchild_count-1人目まで
if requirement <= candy_sum:
candy_sum -= requirement
satisfied_count += 1
# for requirement in requirement_list:
# if requirement <= candy_sum:
# candy_sum -= requirement
# satisfied_count += 1
# print(requirement, candy_sum)
# # requirement > candy_sumのときは何もしない。
# # candy_sum = 20のケースがあるので、
# # [30, 20, 10]のときに最初が30だからといってbreakもしない
print(satisfied_count)
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s670826177 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | n, x = map(int, input().split())
a = list(map(int, input().split() for i in range(n)))
a_sort = sorted(a)
count = 0
sum = 0
for i in a_sort:
if sum + i <= x:
sum = sum + i
count += 1
else:
break
print(count)
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s046049334 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | N,x = map(int,input().split())
li = list(map(int,input().split()))
j = 0
k = 0
while k < N:
if x > li[k]:
j += 1
x = x - li[k]
k += 1
else:
x = x - li[k]
k += 1
print(str(j))
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s842753685 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | N, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
a = sorted(a)
for i in range(len(a)):
if x >= a[i]:
x -= a[i]
ans += 1
else:
break
print(ans)
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s009194435 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | N,x = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
b = 0
for i in a:
x -= I
if x => 0:
b += 1
else:
break
print(b) | Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s529942650 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N | n, x = map(int, input().split())
child_list = list(map(int, input().split()))
for x>0:
if x - min(child_list) > 0:
please_child +=1
child_list = child_list.pop(min(child_list))
if child_list == []:
break
if x - min(child_list) =< 0:
break
print(please_child) | Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
Print the maximum possible number of happy children.
* * * | s196524941 | Runtime Error | p03254 | Input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N |
#入力した型を数値に変換し、1行のデータを分割
#map()関数
n,x = map(int,input().split())
a = list(map(int,input().split())
#リストを昇順にソート
a.sort()
n = 0
for i in a:
n += 1
x -= i
if x < 0:
n -= 1
break
if x > 0:
n -= 1
print(n)
| Statement
There are N children, numbered 1, 2, ..., N.
Snuke has decided to distribute x sweets among them. He needs to give out all
the x sweets, but some of the children may get zero sweets.
For each i (1 \leq i \leq N), Child i will be _happy_ if he/she gets exactly
a_i sweets. Snuke is trying to maximize the number of happy children by
optimally distributing the sweets. Find the maximum possible number of happy
children. | [{"input": "3 70\n 20 30 10", "output": "2\n \n\nOne optimal way to distribute sweets is (20, 30, 20).\n\n* * *"}, {"input": "3 10\n 20 30 10", "output": "1\n \n\nThe optimal way to distribute sweets is (0, 0, 10).\n\n* * *"}, {"input": "4 1111\n 1 10 100 1000", "output": "4\n \n\nThe optimal way to distribute sweets is (1, 10, 100, 1000).\n\n* * *"}, {"input": "2 10\n 20 20", "output": "0\n \n\nNo children will be happy, no matter how the sweets are distributed."}] |
For each dataset, minimize the maximum number of questions by which every
object is identifiable and output the result. | s799826733 | Runtime Error | p00881 | The input is a sequence of multiple datasets. Each dataset begins with a line
which consists of two integers, _m_ and _n_ : the number of features, and the
number of objects, respectively. You can assume 0 < _m_ ≤ 11 and 0 < _n_ ≤
128\. It is followed by _n_ lines, each of which corresponds to an object.
Each line includes a binary string of length _m_ which represent the value
("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at
most 100 datasets. | from collections import Counter
while True:
m, n = (int(s) for s in input().split())
if not m:
break
objs = [int(input(), 2) for i in range(n)]
f = [[0 for i in range(1 << m)] for i in range(1 << m)]
for mask in reversed(range(1 << m)):
s = Counter(obj & mask for obj in objs)
for mask0 in range(mask + 1):
if s[mask0] > 1:
f[mask][mask0] = n + 1
for b in [1 << k for k in range(m)]:
if not (b & mask):
f[mask][mask0] = min(
f[mask][mask0],
max(f[mask + b][mask0], f[mask + b][mask0 + b]) + 1,
)
print(f[0][0])
| H: Twenty Questions
Consider a closed world and a set of features that are defined for all the
objects in the world. Each feature can be answered with "yes" or "no". Using
those features, we can identify any object from the rest of the objects in the
world. In other words, each object can be represented as a fixed-length
sequence of booleans. Any object is different from other objects by at least
one feature.
You would like to identify an object from others. For this purpose, you can
ask a series of questions to someone who knows what the object is. Every
question you can ask is about one of the features. He/she immediately answers
each question with "yes" or "no" correctly. You can choose the next question
after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you
don' t have surplus money, it is necessary to minimize the number of questions
in the worst case. You don’t know what is the correct answer, but fortunately
know all the objects in the world. Therefore, you can plan an optimal strategy
before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects,
minimize the maximum number of questions by which every object in the set is
identifiable. | [{"input": "1\n 11010101\n 11 4\n 00111001100\n 01001101011\n 01010000011\n 01100110001\n 11 16\n 01000101111\n 01011000000\n 01011111001\n 01101101001\n 01110010111\n 01110100111\n 10000001010\n 10010001000\n 10010110100\n 10100010100\n 10101010110\n 10110100010\n 11001010011\n 11011001001\n 11111000111\n 11111011101\n 11 12\n 10000000000\n 01000000000\n 00100000000\n 00010000000\n 00001000000\n 00000100000\n 00000010000\n 00000001000\n 00000000100\n 00000000010\n 00000000001\n 00000000000\n 9 32\n 001000000\n 000100000\n 000010000\n 000001000\n 000000100\n 000000010\n 000000001\n 000000000\n 011000000\n 010100000\n 010010000\n 010001000\n 010000100\n 010000010\n 010000001\n 010000000\n 101000000\n 100100000\n 100010000\n 100001000\n 100000100\n 100000010\n 100000001\n 100000000\n 111000000\n 110100000\n 110010000\n 110001000\n 110000100\n 110000010\n 110000001\n 110000000\n 0 0", "output": "2\n 4\n 11\n 9"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s993230175 | Accepted | p02852 | Input is given from Standard Input in the following format:
N M
S | def main():
n, m = [int(x) for x in input().split()]
s = input()
c = n
h = []
while True:
d = m
while d > 0:
if c - d >= 0:
if s[c - d] == "0":
break
d -= 1
if d == 0:
print(-1)
break
c -= d
h.append(str(d))
if c == 0:
h.reverse()
print(" ".join(h))
break
main()
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s567749844 | Wrong Answer | p02852 | Input is given from Standard Input in the following format:
N M
S | n, m = map(int, input().split())
sugoroku = input()
cnt = 0
pos = 0
tmp = 0
lst = []
while pos != n:
pos += 1
cnt += 1
if cnt == m:
if sugoroku[pos] == "0":
lst.append(cnt)
cnt = 0
else:
if tmp == 0:
print(-1)
exit()
lst.append(tmp)
cnt -= tmp
tmp = 0
else:
if sugoroku[pos] == "0":
tmp = cnt
if cnt != 0:
lst.append(cnt)
for num in lst:
print(num)
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s471850109 | Runtime Error | p02852 | Input is given from Standard Input in the following format:
N M
S | n = int(input())
a = list(map(int, input().split()))
now = a[0]
a.sort()
res = 0
ans = 1
for i in range(n - 1):
if a[i + 1] != now:
ans += 1
now = a[i + 1]
if ans % 2:
print(ans)
else:
print(ans - 1)
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s281913674 | Accepted | p02852 | Input is given from Standard Input in the following format:
N M
S | """
演算子は要素とセットでモノイドを形成するようなものでなければならない。
すなわち、結合律が成り立ち単位元が存在する必要がある。
equalityはsetを高速化するために使う。めんどい場合はNoneにしてもOK
"""
class SegmentTree:
@classmethod
def all_identity(cls, operator, equality, identity, size):
return cls(
operator, equality, identity, [identity] * (2 << (size - 1).bit_length())
)
@classmethod
def from_initial_data(cls, operator, equality, identity, data):
size = 1 << (len(data) - 1).bit_length()
temp = [identity] * (2 * size)
temp[size : size + len(data)] = data
data = temp
for i in reversed(range(size)):
data[i] = operator(data[2 * i], data[2 * i + 1])
return cls(operator, equality, identity, data)
# これ使わずファクトリーメソッド使いましょうね
def __init__(self, operator, equality, identity, data):
if equality is None:
equality = lambda a, b: False
self.op = operator
self.eq = equality
self.id = identity
self.data = data
self.size = len(data) // 2
def _interval(self, a, b):
a += self.size
b += self.size
ra = self.id
rb = self.id
data = self.data
op = self.op
while a < b:
if a & 1:
ra = op(ra, data[a])
a += 1
if b & 1:
b -= 1
rb = op(data[b], rb)
a >>= 1
b >>= 1
return op(ra, rb)
def __getitem__(self, i):
if isinstance(i, slice):
return self._interval(
0 if i.start is None else i.start,
self.size if i.stop is None else i.stop,
)
elif isinstance(i, int):
return self.data[i + self.size]
def __setitem__(self, i, v):
i += self.size
data = self.data
op = self.op
eq = self.eq
while i and not eq(data[i], v):
data[i] = v
v = op(data[i ^ 1], v) if i & 1 else op(v, data[i ^ 1])
i >>= 1
def __iter__(self):
return iter(self.data[self.size :])
from operator import eq
N, M = map(int, input().split())
N += 1
S = tuple(map(lambda x: x == "0", input()))
dp = SegmentTree.all_identity(min, eq, float("inf"), N)
dp[N - 1] = 0
for i in reversed(range(N - 1)):
if S[i]:
dp[i] = dp[i + 1 : min(N, i + M + 1)] + 1
if dp[0] == float("inf"):
print(-1)
exit()
res = []
i = 0
while i < N - 1:
t = dp[i]
k = 1
while dp[i + k] != t - 1:
k += 1
res.append(k)
i += k
print(*res)
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s432559111 | Wrong Answer | p02852 | Input is given from Standard Input in the following format:
N M
S | """
Beginners Contests 146
F - Sugoroku
https://atcoder.jp/contests/abc146/tasks/abc146_f
"""
from collections import deque
import copy
N, M = map(int, input().split())
S = input()
len_s = len(S)
# initialize
queue = deque([[0]])
ans = []
end_flg = False
distance_to_goal = N
while len(queue) > 0:
m = M
# print("queue contents are {}".format(queue))
here = queue.popleft()
# print(end_flg)
# print("ans:{}".format(ans))
if end_flg and len(ans[0]) == len(here):
answer = ans[-1]
answer.pop(0)
print(" ".join(map(str, answer)))
quit()
# print("this queue is {}".format(here))
# Get distance from start to here
distance_from_start = sum(here)
distance_to_goal = N - distance_from_start
# print("distance_from_start:{0},N:{1},distance_to_goal:{2}".format(distance_from_start, N, distance_to_goal))
# print("m:{}".format(m))
if distance_to_goal < m:
m = distance_to_goal
for i in range(m, 0, -1):
tmp_here = copy.copy(here)
# print("step count: {}".format(i))
# print(S)
# print("here step is {0}, char is {1} ".format(distance_from_start, S[distance_from_start]))
# print("next step is {0} next char is {1}".format(str(distance_from_start + i), S[distance_from_start + i]))
if distance_from_start + i == N:
# When next place to be Goal !!!
# print("answer was found")
tmp_here.append(i)
ans.append(tmp_here)
end_flg = True
# When place not to be game over
elif S[distance_from_start + i] != "1":
# print("tmp_here:{0}, i:{1}".format(tmp_here, i))
tmp_here.append(i)
# print("append next queue:{0}".format(tmp_here))
queue.append(tmp_here)
# else:
# print("next step will be game over X(")
print("-1")
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s950342664 | Runtime Error | p02852 | Input is given from Standard Input in the following format:
N M
S | N, M = map(int, input().split())
S = input()
N0 = 2 ** (N - 1).bit_length()
INF = 2**31 - 1
# k番目の要素をxに更新
def update(k, x, data):
k += N0 - 1
data[k] = x
while k >= 0:
k = (k - 1) // 2
data[k] = min(data[2 * k + 1], data[2 * k + 2])
# [l,r)の最小値
def query(l, r, data):
L = l + N0
R = r + N0
s = INF
while L < R:
if R & 1:
R -= 1
s = min(s, data[R - 1])
if L & 1:
s = min(s, data[L - 1])
L += 1
L >>= 1
R >>= 1
return s
data1 = [INF] * (2 * N0)
update(0, 0, data1)
for n in range(N):
if S[n + 1] == "0":
update(n + 1, query(max(0, n - M + 1), n + 1, data1) + 1, data1)
S = S[::-1]
data2 = [INF] * (2 * N0)
update(0, 0, data2)
for n in range(N):
if S[n + 1] == "0":
update(n + 1, query(max(0, n - M + 1), n + 1, data2) + 1, data2)
cost1 = [INF] * (N + 1)
cost2 = [INF] * (N + 1)
for n in range(N + 1):
cost1[n] = query(n, n + 1, data1)
cost2[n] = query(N - n, N - n + 1, data2)
ans = []
cost = 0
for n in range(N + 1):
if cost != cost1[n] and cost1[n] + cost2[n] == cost1[N]:
ans.append(n)
cost = cost1[n]
ans = [0] + ans
for i in range(len(ans) - 2, -1, -1):
ans[i + 1] = ans[i + 1] - ans[i]
ans = " ".join([str(c) for c in ans[1:]])
if cost2[0] == INF:
ans = "-1"
print(ans)
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s709882341 | Wrong Answer | p02852 | Input is given from Standard Input in the following format:
N M
S | import sys # 追加
sys.setrecursionlimit(10**9) # 追加
n, m = map(int, input().split())
s = list(input())
global min_rireki, ans
min_rireki = float("inf")
ans = []
global get_point
get_point = [float("inf") for i in range(n + 1)]
get_point[0] = 0
def dfs(pos, rireki):
global ans
if pos == n:
if len(rireki) < min_rireki:
ans = rireki
else:
for i in range(1, m + 1)[::-1]:
if pos + i <= n:
if s[pos + i] == "0":
if get_point[pos + i] - 1 > len(rireki):
dfs(pos + i, rireki + [i])
get_point[pos + i] = len(rireki) + 1
dfs(0, [])
if ans == []:
print(-1)
else:
for i in ans:
print(i)
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s572385780 | Accepted | p02852 | Input is given from Standard Input in the following format:
N M
S | class SegTreeIndex(object):
# 区間の最小 or 最大値とその index を取得
# 検証: https://yukicoder.me/submissions/376308
__slots__ = ["elem_size", "tree", "default", "op", "index", "op2"]
def __init__(
self,
a: list,
default=float("inf"),
op=lambda a, b: b if a > b else a,
op2=lambda a, b: a > b,
):
# 同じ場合最左のインデックスを返す
# 最大値・最左 -> -float("inf"), max, lambda a,b:a<=b
from math import ceil, log
real_size = len(a)
self.elem_size = elem_size = 1 << ceil(log(real_size, 2))
self.tree = tree = [default] * (elem_size * 2)
self.index = index = [0] * (elem_size * 2)
tree[elem_size : elem_size + real_size] = a
index[elem_size : elem_size + real_size] = list(range(real_size))
self.default = default
self.op = op
self.op2 = op2
for i in range(elem_size - 1, 0, -1):
v1, v2 = tree[i << 1], tree[(i << 1) + 1]
tree[i] = op(v1, v2)
index[i] = index[(i << 1) + op2(v1, v2)]
def get_value(self, x: int, y: int) -> tuple: # 半開区間
l, r = x + self.elem_size, y + self.elem_size
tree, op, op2, index = self.tree, self.op, self.op2, self.index
result_l = result_r = self.default
idx_l = idx_r = -1
while l < r:
if l & 1:
v1, v2 = result_l, tree[l]
result_l = op(v1, v2)
if op2(v1, v2) == 1:
idx_l = index[l]
l += 1
if r & 1:
r -= 1
v1, v2 = tree[r], result_r
result_r = op(v1, v2)
if op2(v1, v2) == 0:
idx_r = index[r]
l, r = l >> 1, r >> 1
result = op(result_l, result_r)
idx = idx_r if op2(result_l, result_r) else idx_l
return result, idx
def set_value(self, i: int, value: int) -> None:
k = self.elem_size + i
self.tree[k] = value
self.update(k)
def update(self, i: int) -> None:
op, tree, index, op2 = self.op, self.tree, self.index, self.op2
while i > 1:
i >>= 1
v1, v2 = tree[i << 1], tree[(i << 1) + 1]
tree[i] = op(v1, v2)
index[i] = index[(i << 1) + op2(v1, v2)]
N, M = map(int, input().split())
S = list(map(int, input()))
dp = [10**9] * (N + 1)
seg = SegTreeIndex(dp)
from_ = [-10000000] * (N + 1)
seg.set_value(0, 0)
for i, c in enumerate(S[1:], 1):
if c == 1:
continue
v, idx = seg.get_value(max(i - M, 0), i)
if v == 1000000000:
print(-1)
exit()
from_[i] = idx
seg.set_value(i, v + 1)
n = seg.tree[N + seg.elem_size]
# print(seg.tree[seg.elem_size:seg.elem_size+N+1])
# print(from_)
Ans = []
v = N
while v != 0:
v_ = from_[v]
Ans.append(v - v_)
v = v_
Ans.reverse()
print(" ".join(map(str, Ans)))
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s809520493 | Wrong Answer | p02852 | Input is given from Standard Input in the following format:
N M
S | n, m = map(int, input().split())
s = input()
class segtree:
def __init__(self, n, operator, identity):
nb = bin(n)[2:]
bc = sum([int(digit) for digit in nb])
if bc == 1:
self.num_leaves = 2 ** (len(nb) - 1)
else:
self.num_leaves = 2 ** (len(nb))
self.array = [identity for i in range(self.num_leaves * 2)]
self.identity = identity
self.operator = operator
self.n = n
def build(self, init_arr):
for i in range(self.n):
self.array[i + self.num_leaves] = init_arr[i]
for i in range(self.num_leaves):
pt = self.num_leaves - 1 - i
self.array[pt] = self.operator(self.array[pt * 2], self.array[pt * 2 + 1])
def update(self, x, val):
actual_x = x + self.num_leaves
# self.array[actual_x] = self.operator(self.array[actual_x],val)
self.array[actual_x] = val
while actual_x > 0:
actual_x = actual_x // 2
self.array[actual_x] = self.operator(
self.array[actual_x * 2], self.array[actual_x * 2 + 1]
)
def get(self, q_left, q_right, arr_ind=1, leaf_left=0, depth=0):
width_of_floor = self.num_leaves // (2**depth)
leaf_right = leaf_left + width_of_floor - 1
if leaf_left > q_right or leaf_right < q_left:
return self.identity
elif leaf_left >= q_left and leaf_right <= q_right:
return self.array[arr_ind]
else:
val_l = self.get(q_left, q_right, 2 * arr_ind, leaf_left, depth + 1)
val_r = self.get(
q_left,
q_right,
2 * arr_ind + 1,
leaf_left + width_of_floor // 2,
depth + 1,
)
return self.operator(val_l, val_r)
def find(self, l, r):
# findの方が高速?
l += self.num_leaves
r += self.num_leaves + 1
ret = self.identity
while l < r:
if l & 1:
ret = self.operator(ret, self.array[l])
l += 1
if r & 1:
r -= 1
ret = self.operator(ret, self.array[r])
l //= 2
r //= 2
return ret
st = segtree(n + 1, min, 1e10)
arr = [1e9 for i in range(n + 1)]
arr[0] = 0
st.build(arr)
for i in range(1, n + 1):
if s[i] == "1":
continue
else:
left = max(i - m, 0)
right = i - 1
tm = st.find(left, right)
arr[i] = tm + 1
st.update(i, tm + 1)
nowm = arr[-1]
right = []
left = []
if nowm > 1e9:
print(-1)
else:
arr.append(1e8)
nowm = arr[-1]
for i in range(2, n + 3):
if nowm > arr[-i]:
nowm = arr[-i]
right.append(-i)
left.append(-i)
elif nowm == arr[-i]:
left[-1] = -i
# print(arr)
# print(right)
# print(left)
bef = right[0]
ans = []
for i in range(len(left) - 1):
ans.append(bef - left[i + 1])
bef = left[i + 1]
# print(ans)
print(" ".join(map(str, ans[::-1])))
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
If Takahashi can win the game, print the lexicographically smallest sequence
among the shortest sequences of numbers coming up in the roulette in which
Takahashi can win the game, with spaces in between.
If Takahashi cannot win the game, print -1.
* * * | s770522798 | Accepted | p02852 | Input is given from Standard Input in the following format:
N M
S | def main():
n, m = map(int, input().split())
s = input()
masu = n
res = []
while masu > m:
tm = m
while s[masu - tm] == "1":
tm -= 1
if tm == 0:
return -1
res.append(tm)
masu -= tm
res.append(masu)
res.reverse()
return " ".join(map(str, res))
print(main())
| Statement
Takahashi is playing a board game called Sugoroku.
On the board, there are N + 1 squares numbered 0 to N. Takahashi starts at
Square 0, and he has to stop exactly at Square N to win the game.
The game uses a roulette with the M numbers from 1 to M. In each turn,
Takahashi spins the roulette. If the number x comes up when he is at Square s,
he moves to Square s+x. If this makes him go beyond Square N, he loses the
game.
Additionally, some of the squares are Game Over Squares. He also loses the
game if he stops at one of those squares. You are given a string S of length N
+ 1, representing which squares are Game Over Squares. For each i (0 \leq i
\leq N), Square i is a Game Over Square if S[i] = 1 and not if S[i] = 0.
Find the sequence of numbers coming up in the roulette in which Takahashi can
win the game in the fewest number of turns possible. If there are multiple
such sequences, find the lexicographically smallest such sequence. If
Takahashi cannot win the game, print -1. | [{"input": "9 3\n 0001000100", "output": "1 3 2 3\n \n\nIf the numbers 1, 3, 2, 3 come up in this order, Takahashi can reach Square 9\nvia Square 1, 4, and 6. He cannot reach Square 9 in three or fewer turns, and\nthis is the lexicographically smallest sequence in which he reaches Square 9\nin four turns.\n\n* * *"}, {"input": "5 4\n 011110", "output": "-1\n \n\nTakahashi cannot reach Square 5.\n\n* * *"}, {"input": "6 6\n 0101010", "output": "6"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s561169296 | Runtime Error | p03740 | Input is given from Standard Input in the following format:
X Y | a,b=[int(x) for x in input().split()]
if abs(a-b)<=1:
print("Brown):
else:
print("Alice") | Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s046653556 | Runtime Error | p03740 | Input is given from Standard Input in the following format:
X Y | a,b=[int(x) for x in input().split()]
if abs(a-b)<=1:
print("Brown"):
else:
print("Alice") | Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s669088843 | Runtime Error | p03740 | Input is given from Standard Input in the following format:
X Y | x, y = map(int, input().split())
if abs(x - y) <= 1:
print('Brown')
else:
print('Alice')
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s331501349 | Accepted | p03740 | Input is given from Standard Input in the following format:
X Y | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys # {{{
import os
import time
import re
from pydoc import help
import string
import math
from operator import itemgetter
from collections import Counter
from collections import deque
from collections import defaultdict as dd
import fractions
from heapq import heappop, heappush, heapify
import array
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy as dcopy
import itertools
# }}}
# pre-defined{{{
sys.setrecursionlimit(10**7)
INF = 10**20
GOSA = 1.0 / 10**10
MOD = 10**9 + 7
ALPHABETS = [
chr(i) for i in range(ord("a"), ord("z") + 1)
] # can also use string module
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 DP(N, M, first):
return [[first] * M for n in range(N)]
def DP3(N, M, L, first):
return [[[first] * L for n in range(M)] for _ in range(N)]
from inspect import currentframe
def dump(*args):
names = {id(v): k for k, v in currentframe().f_back.f_locals.items()}
print(
", ".join(names.get(id(arg), "???") + " => " + repr(arg) for arg in args),
file=sys.stderr,
)
# }}}
def local_input(): # {{{
from pcm.utils import set_stdin
import sys
from pathlib import Path
parentdir = Path(os.path.dirname(__file__)).parent
inputfile = parentdir.joinpath("test/sample-1.in")
if len(sys.argv) == 1:
set_stdin(inputfile)
# }}}
def solve():
x, y = map(int, input().split())
if abs(x - y) <= 1:
print("Brown")
else:
print("Alice")
return 0
if __name__ == "__main__": # {{{
try:
local_input()
except:
pass
solve()
# vim: set foldmethod=marker:}}}
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s825618179 | Runtime Error | p03740 | Input is given from Standard Input in the following format:
X Y | import sys
AB = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
# S = str(input())
# L = len(S)
# T = str(input())
fin = [[0, 0], [1, 0], [0, 1]]
Alice = True
memo = {}
def Alicewin(state, Alice):
if state in fin and Alice:
return False
elif state in fin and not Alice:
return True
Ap = 2
while Ap <= state[0]:
if Alicewin([state[0] - Ap, state[1] + Ap / 2], not Alice):
return True
Ap += 2
Bp = 2
while Bp <= state[1]:
if Alicewin([state[0] + Bp / 2, state[1] - Bp], not Alice):
return True
Bp += 2
return False
print("Alice" if Alicewin(AB, True) else "Brown")
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s025791247 | Runtime Error | p03740 | Input is given from Standard Input in the following format:
X Y | x, y = = map(int,input().split())
if abs(x-y) < 1:
print('Brown')
else:
print('Alice') | Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s184583134 | Runtime Error | p03740 | Input is given from Standard Input in the following format:
X Y | from collections import deque
n, m = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
tree = [[] for i in range(n)]
for i in range(m):
tree[info[i][0] - 1].append([info[i][0] - 1, info[i][1] - 1, info[i][2], False])
flag = [False]
visited = [-float("inf")] * n
visited[0] = 0
count = [0]
def bfs(here):
q = deque()
for i in tree[here]:
q.append(i)
while q and count[0] <= m * n:
tmp = q.popleft()
here = tmp[0]
go = tmp[1]
cost = tmp[2]
if visited[here] + cost > visited[go]:
visited[go] = cost + visited[here]
for i in range(len(tree[go])):
if not tree[go][i][3]:
q.appendleft(tree[go][i])
tree[go][i][3] = True
else:
q.append(tree[go][i])
if go == n - 1:
print("inf")
exit()
count[0] += 1
bfs(0)
print(visited[n - 1])
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s232862484 | Wrong Answer | p03740 | Input is given from Standard Input in the following format:
X Y | a, b = map(int, input().split())
cnt = 1
if b > a:
a, b = b, a
a = a - b
b = 0
mod_a = a % 3
if a == 3:
print("Alice")
elif mod_a == 2:
print("Alice")
elif mod_a == 1:
print("Brown")
elif (mod_a == 0) and (a != 3):
print("Brown")
# if (mod_a==3)and(mod_b==3):
# print('Alice')
# elif (mod_a==3)and(mod_b==1):
# print('Alice')
# elif (mod_a==1)and(mod_b==3):
# print('Alice')
# elif (mod_a==2)and(mod_b==0):
# print('Alice')
# elif (mod_a==0)and(mod_b==2):
# print('Alice')
# else:
# print('Brown')
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s538533948 | Wrong Answer | p03740 | Input is given from Standard Input in the following format:
X Y | print("Alice")
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s472405954 | Wrong Answer | p03740 | Input is given from Standard Input in the following format:
X Y | X, Y = [int(i) for i in input().split(" ")]
player = ["Alice", "Brown"]
count = 0
while True:
if X <= 1 and Y <= 1:
print(player[1 - count % 2])
break
elif X > 1:
Y += X // 2
X = 0 if X % 2 == 0 else 1
elif Y > 1:
X += Y // 2
Y = 0 if Y % 2 == 0 else 1
count += 1
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s148915375 | Accepted | p03740 | Input is given from Standard Input in the following format:
X Y | X, Y = list(map(int, input().split()))
print("Brown") if abs(X - Y) <= 1 else print("Alice")
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s569444222 | Runtime Error | p03740 | Input is given from Standard Input in the following format:
X Y | ¥X,Y = map(int,input().split())
def judege(Z):
ans = int(Z/2)
re = Z%2
# print(ans,re)
if ans == 0:
if re == 0:
return "Brown"
else:
return "Alice"
else:
return judege(ans+re)
print(judege(X+Y)) | Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s620984135 | Accepted | p03740 | Input is given from Standard Input in the following format:
X Y | print("ABlriocwen"[-2 < eval(input().replace(" ", "-")) < 2 :: 2])
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s389733391 | Accepted | p03740 | Input is given from Standard Input in the following format:
X Y | print("ABlriocwen"[abs(eval(input().replace(" ", "-"))) < 2 :: 2])
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Print the winner: either `Alice` or `Brown`.
* * * | s325741153 | Runtime Error | p03740 | Input is given from Standard Input in the following format:
X Y | x, y = input().split()
print("Alice") if (x + y) > 1 else print("Brown")
| Statement
Alice and Brown loves games. Today, they will play the following game.
In this game, there are two piles initially consisting of X and Y stones,
respectively. Alice and Bob alternately perform the following operation,
starting from Alice:
* Take 2i stones from one of the piles. Then, throw away i of them, and put the remaining i in the other pile. Here, the integer i (1≤i) can be freely chosen as long as there is a sufficient number of stones in the pile.
The player who becomes unable to perform the operation, loses the game.
Given X and Y, determine the winner of the game, assuming that both players
play optimally. | [{"input": "2 1", "output": "Brown\n \n\nAlice can do nothing but taking two stones from the pile containing two\nstones. As a result, the piles consist of zero and two stones, respectively.\nThen, Brown will take the two stones, and the piles will consist of one and\nzero stones, respectively. Alice will be unable to perform the operation\nanymore, which means Brown's victory.\n\n* * *"}, {"input": "5 0", "output": "Alice\n \n\n* * *"}, {"input": "0 0", "output": "Brown\n \n\n* * *"}, {"input": "4 8", "output": "Alice"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s492980293 | Runtime Error | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | def main():
N = int(input())
A = []
data = input().split()
for i in range(N):
A.append(int(data[i]))
S = sum(A)
m = S // 4
B = [[]]
t = 0
part = 0
partsum = 0
while t < N:
if partsum + A[t] > m:
partsum = 0
part += 1
B.append([])
B[part].append(A[t])
partsum += A[t]
t += 1
P = [sum(B[i]) for i in range(4)]
print(max(P) - min(P))
if __name__ == "__main__":
main()
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s690540178 | Accepted | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | # -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
d_in = lambda: int(stdin.readline()) # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split())) # List = ds_in()
def left_diff(array):
"""
array: ordered ndarray, len(array) >= 2
"""
x = (array[-1] + 1) >> 1
mid = np.searchsorted(array, x)
if abs(array[-1] - array[mid] * 2) < abs(array[-1] - array[mid - 1] * 2):
return array[mid], array[-1] - array[mid]
else:
return array[mid - 1], array[-1] - array[mid - 1]
def right_diff(array, offset):
"""
array: ordered ndarray, len(array) >= 2
target array or list
offset: int
offset value for right array
"""
x = (array[-1] - offset + 1) >> 1
mid = np.searchsorted(array, x + offset)
if abs(array[-1] - array[mid] * 2 + offset) < abs(
array[-1] - array[mid - 1] * 2 + offset
):
return array[mid] - offset, array[-1] - array[mid]
else:
return array[mid - 1] - offset, array[-1] - array[mid - 1]
def calc_max_diff(p, q, r, s):
return max(p, q, r, s) - min(p, q, r, s)
if __name__ == "__main__":
N = d_in()
a_array = np.array(ds_in())
cum_a = np.cumsum(a_array)
left_cut_points = (cum_a[1:-2] + 1) // 2
left_mid_points = np.searchsorted(cum_a, left_cut_points)
right_cut_points = (cum_a[-1] + cum_a[1:-2] + 1) // 2
right_mid_points = np.searchsorted(cum_a, right_cut_points)
ans = float("inf")
for i in range(2):
for j in range(2):
P = cum_a[left_mid_points - i]
Q = cum_a[1:-2] - P
R = cum_a[right_mid_points - j] - cum_a[1:-2]
S = cum_a[-1] - cum_a[right_mid_points - j]
MAX = np.maximum(np.maximum(P, Q), np.maximum(R, S))
MIN = np.minimum(np.minimum(P, Q), np.minimum(R, S))
ans = min(ans, (MAX - MIN).min())
print(ans)
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s130422063 | Wrong Answer | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | # 全体の和を求めて平均を出す。
# 左から順に要素を見ていき、平均との差の絶対値が最小になった時点で確定
# ただし、残りN個作る必要があるときに残数がN未満だったらその時点で確定する
import sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
ave = sum(A) / 4
# print(ave)
# aveが目指す数字
ind = 0
data = [0] * 4
for i in range(1, 5):
# print("i",i,"ind",ind)
# i個目のブロックについて探索する
# これが最終ブロックの場合は、すべて採用する
if i == 4:
data[i - 1] = sum(A[ind:])
break
cur = 0
diff = 0
for j in range(ind, len(A) - (4 - i)):
cur += A[j]
# print("cur",cur)
if cur == ave:
# そのまま採用する
ind = j + 1
break
if cur < ave:
diff = abs(cur - ave)
ind = j
# print("diff",diff)
else:
# curがaveを追い抜いたとき
# print("cur",cur,"がave",ave,"を追い抜いた")
new_diff = abs(cur - ave)
# print("new_diff",new_diff,"diff",diff)
if j == ind:
# 最初は巻き戻せないのでそのまま採用
ind = j + 1
elif new_diff <= diff: # 新規の差分のほうが小さいので、そのまま採用
ind = j + 1
# print("採用")
else: # 巻き戻す
# print("巻き戻すcur",cur,"A[j]",A[j])
ind = j
cur -= A[j]
# print("cur",cur)
break
else:
# breakしなかったときはそのまま採用
ind = j + 1
data[i - 1] = cur
# print("data",data,"ind",ind)
# print(data)
print(max(data) - min(data))
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s374537952 | Wrong Answer | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s017420551 | Runtime Error | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | from bisect import *
def resolve():
N = int(input())
A = list(map(int, input().split()))
AR = [0 for i in range(N)]
AR[0] = A[0]
for i in range(1, N):
AR[i] = AR[i - 1] + A[i]
cent = bisect_right(AR, AR[-1] / 2)
lsum = AR[cent]
rsum = AR[-1] - AR[cent]
diff = abs(rsum - lsum)
lc = bisect_right(AR, lsum / 2, hi=cent)
rc = bisect_right(AR, (lsum + AR[-1]) / 2, lo=cent + 1)
XX = []
XX.append([AR[lc], AR[cent] - AR[lc], AR[rc] - AR[cent], AR[-1] - AR[rc]])
if lc != 0:
XX.append(
[AR[lc - 1], AR[cent] - AR[lc - 1], AR[rc] - AR[cent], AR[-1] - AR[rc]]
)
XX.append([AR[lc], AR[cent] - AR[lc], AR[rc - 1] - AR[cent], AR[-1] - AR[rc - 1]])
if lc != 0 and rc > cent + 1:
XX.append(
[
AR[lc - 1],
AR[cent] - AR[lc - 1],
AR[rc - 1] - AR[cent],
AR[-1] - AR[rc - 1],
]
)
cent -= 1
lsum = AR[cent]
rsum = AR[-1] - AR[cent]
lc = bisect_right(AR, lsum / 2, hi=cent)
rc = bisect_right(AR, (lsum + AR[-1]) / 2, lo=cent + 1)
XX.append([AR[lc], AR[cent] - AR[lc], AR[rc] - AR[cent], AR[-1] - AR[rc]])
if lc != 0:
XX.append(
[AR[lc - 1], AR[cent] - AR[lc - 1], AR[rc] - AR[cent], AR[-1] - AR[rc]]
)
XX.append([AR[lc], AR[cent] - AR[lc], AR[rc - 1] - AR[cent], AR[-1] - AR[rc - 1]])
if lc != 0 and rc > cent + 1:
XX.append(
[
AR[lc - 1],
AR[cent] - AR[lc - 1],
AR[rc - 1] - AR[cent],
AR[-1] - AR[rc - 1],
]
)
cent -= 1
lsum = AR[cent]
rsum = AR[-1] - AR[cent]
lc = bisect_right(AR, lsum / 2, hi=cent)
rc = bisect_right(AR, (lsum + AR[-1]) / 2, lo=cent + 1)
XX.append([AR[lc], AR[cent] - AR[lc], AR[rc] - AR[cent], AR[-1] - AR[rc]])
if lc != 0:
XX.append(
[AR[lc - 1], AR[cent] - AR[lc - 1], AR[rc] - AR[cent], AR[-1] - AR[rc]]
)
XX.append([AR[lc], AR[cent] - AR[lc], AR[rc - 1] - AR[cent], AR[-1] - AR[rc - 1]])
if lc != 0 and rc > cent + 1:
XX.append(
[
AR[lc - 1],
AR[cent] - AR[lc - 1],
AR[rc - 1] - AR[cent],
AR[-1] - AR[rc - 1],
]
)
print(min([max(i) - min(i) for i in XX]))
if __name__ == "__main__":
resolve()
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s339555600 | Wrong Answer | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | from sys import stdin
n = int(stdin.readline().rstrip())
arg = list(map(int, stdin.readline().rstrip().split()))
partsum = []
temp = 0
ave = sum(arg) // 4
for i, val in enumerate(arg):
if len(partsum) < 4:
if abs(temp - ave) >= abs((temp + val) - ave):
temp += val
else:
partsum.append(temp)
temp = val
else:
temp += val
if i == n - 1:
partsum.append(temp)
if len(partsum) < 4:
l = len(partsum)
for i in range(4 - l):
partsum.append(ave)
print(abs(max(partsum) - min(partsum)))
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s682480938 | Wrong Answer | p03310 | 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()))
total = sum(a)
memo = 0
for i in range(n):
memo += a[i]
if memo >= total / 2:
# print(sum(a[:i]))
# print(sum(a[:(i+1)]))
if total / 2 - sum(a[:i]) < sum(a[: (i + 1)]) - total / 2:
c = i
else:
c = i + 1
break
a_l = a[:c]
a_r = a[c:]
# print(a_l)
# print(a_r)
total_l = sum(a_l)
memo_l = 0
for i in range(len(a_l)):
memo_l += a_l[i]
if memo_l >= total_l / 2:
if total_l / 2 - sum(a_l[:i]) < sum(a_l[: (i + 1)]) - total_l / 2:
c_l = i
else:
c_l = i + 1
break
p = sum(a_l[:c_l])
q = sum(a_l[c_l:])
total_r = sum(a_r)
memo_r = 0
for i in range(len(a_r)):
memo_r += a_r[i]
if memo_r >= total_r / 2:
if total_r / 2 - sum(a_r[:i]) < sum(a_r[: (i + 1)]) - total_r / 2:
c_r = i
else:
c_r = i + 1
break
r = sum(a_r[:c_r])
s = sum(a_r[c_r:])
pqrs = [p, q, r, s]
print(max(pqrs) - min(pqrs))
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s376606187 | Runtime Error | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | N = int(input())
if N > 10000:
time.sleep(0.5)
print("No")
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s736451836 | Runtime Error | p03310 | 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()))
ans=sum(A)
for a in range(3,N):
for b in range(2,a):
for c in range(1,b):
sum1=sum(A[:c])
sum2=sum(A[c:b])
sum3=sum(A[b:a])
sum4=sum(A[a:])
dif=max(sum1,sum2,sum3,sum4)-min(sum1,sum2,sum3,sum4)
if dif<ans:
ans=dif
if sum2<=sum1:
break
if min(sum1,sum2,sum3)==sum3:
break
if min(sum1,sum2,sum3,sum4)==sum4:
break
print(ans) | Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s880772380 | Runtime Error | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | n = int(input())
A = [int(i) for i in input().split()]
csum = [0]
for a in A:
csum.append(csum[-1] + a)
def csum_(a, b):
return csum[b] - csum[a]
def search(ok, ng, n, m):
if abs(ok-ng) <= 1:
return ok
mid = (ok+ng) // 2
if csum_(m, mid) < csum_(mid, n):
return search(mid, ng, n, m)
else:
return search(ok, mid, n, m)
fans = 10**9
for c in range(2, n-1):
d = search(1, c, c, 0)
f = search(c+1, n, n, c)
ans = []
if abs(csum_(0, d) - csum_(d, c)) > abs(csum_(0, min(d+1, c-1)) - csum_(min(d+1, c-1), c)):
d += 1
if abs(csum_(c, f) - csum_(f, n)) > abs(csum_(c, min(n-1, f+1)) - csum_(min(n-1, f+1), n)):
f += 1
ans.append(csum_(0, d))
ans.append(csum_(d, c))
ans.append(csum_(c, f))
ans.append(csum_(f, n))
fans = min(fans, max(ans) - min(ans)
print(fans) | Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s020621270 | Runtime Error | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | from scipy.special import perm, comb
N = int(input())
li = input().rstrip().split()
le = [0] * N
for i in range(0, N):
le[i] = int(li[i])
# 愚直に解く
n = comb(N - 1, 3, exact=True)
array = [0] * 4
conbi = [[0, 0, 0]] * n
minmin = 10**10
s = 0
for i in range(1, N):
for j in range(i + 1, N):
for k in range(j + 1, N):
conbi[s] = [i, j, k]
s = s + 1
for i in range(0, n):
array[0] = sum(le[0 : conbi[i][0]])
array[1] = sum(le[conbi[i][0] : conbi[i][1]])
array[2] = sum(le[conbi[i][1] : conbi[i][2]])
array[3] = sum(le[conbi[i][2] :])
mx = max(array)
mn = min(array)
u = mx - mn
if minmin > u:
minmin = u
print(minmin)
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s654635728 | Wrong Answer | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | #!/usr/bin/env python
# coding: utf-8
def ri():
return int(input())
def rl():
return list(input().split())
def rli():
return list(map(int, input().split()))
def calc(n):
s = sum(map(int, str(n)))
return n / s
def main():
k = ri()
cand = list(range(1, 10))
last = 9
for n in range(1, 15):
pow10n = pow(10, n)
for d in range(1, 10):
lim = (d + 1) * pow10n
e = 0
while True:
e1 = d * pow10n + e * (last + 1) + last
if e1 >= lim:
break
e2 = e1 + (last + 1)
if calc(e1) > calc(e2):
last = last * 10 + 9
e = 0
continue
cand.append(e1)
e += 1
print("\n".join(map(str, cand[:k])))
if __name__ == "__main__":
main()
"""
1 1.0
2 1.0
3 1.0
4 1.0
5 1.0
6 1.0
7 1.0
8 1.0
9 1.0
19 1.9
29 2.6363636363636362
39 3.25
49 3.769230769230769
59 4.214285714285714
69 4.6
79 4.9375
89 5.235294117647059
99 5.5
199 10.473684210526315
299 14.95
399 19.0
499 22.681818181818183
599 26.043478260869566
699 29.125
799 31.96
899 34.57692307692308
999 37.0
1099 57.8421052631579
1199 59.95
1299 61.857142857142854
1399 63.59090909090909
1499 65.17391304347827
1599 66.625
1699 67.96
1799 69.1923076923077
1899 70.33333333333333
1999 71.39285714285714
2999 103.41379310344827
3999 133.3
4999 161.25806451612902
5999 187.46875
6999 212.0909090909091
7999 235.26470588235293
8999 257.1142857142857
9999 277.75
10999 392.82142857142856
11999 413.7586206896552
12999 433.3
13999 451.5806451612903
14999 468.71875
15999 484.8181818181818
16999 499.97058823529414
17999 514.2571428571429
18999 527.75
19999 540.5135135135135
20999 724.1034482758621
21999 733.3
22999 741.9032258064516
23999 749.96875
24999 757.5454545454545
25999 764.6764705882352
26999 771.4
27999 777.75
28999 783.7567567567568
29999 789.4473684210526
39999 1025.6153846153845
49999 1249.975
59999 1463.3902439024391
69999 1666.642857142857
79999 1860.4418604651162
89999 2045.4318181818182
99999 2222.2
109999 2972.945945945946
119999 3157.8684210526317
129999 3333.3076923076924
139999 3499.975
149999 3658.512195121951
159999 3809.5
169999 3953.4651162790697
179999 4090.8863636363635
189999 4222.2
199999 4347.804347826087
209999 5526.289473684211
219999 5641.0
229999 5749.975
239999 5853.634146341464
249999 5952.357142857143
259999 6046.488372093023
269999 6136.340909090909
279999 6222.2
289999 6304.326086956522
299999 6382.95744680851
309999 7948.692307692308
319999 7999.975
329999 8048.756097560976
339999 8095.214285714285
349999 8139.511627906977
359999 8181.795454545455
369999 8222.2
379999 8260.847826086956
389999 8297.851063829787
399999 8333.3125
499999 10204.061224489797
599999 11999.98
699999 13725.470588235294
799999 15384.596153846154
899999 16981.11320754717
999999 18518.5
"""
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s328064570 | Accepted | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | import statistics
import math
N = int(input())
A = list(map(int, input().split()))
ureshimi = []
k = 0
l = 2
B = 0
C = 0
D = 0
E = 0
sumBC = sum(A[0:2])
sumDE = sum(A) - sumBC
for i in range(2, N - 1):
for j in range(k, i):
if j == i - 1:
k = j
C = sumBC - B
break
elif B + A[j] >= sumBC / 2:
if sumBC / 2 - B > A[j] / 2:
B += A[j]
k = j + 1
C = sumBC - B
break
else:
k = j
C = sumBC - B
break
B += A[j]
for j in range(l, N):
if j == N - 1:
l = j
E = sumDE - D
break
elif D + A[j] >= sumDE / 2:
if sumDE / 2 - D > A[j] / 2:
D += A[j]
l = j + 1
E = sumDE - D
break
else:
l = j
E = sumDE - D
break
D += A[j]
ureshimi.append(max([B, C, D, E]) - min([B, C, D, E]))
sumBC += A[i]
sumDE -= A[i]
D -= A[i]
print(min(ureshimi))
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s924647531 | Accepted | p03310 | Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N | from collections import defaultdict
import sys, heapq, bisect, math, itertools, string, queue, datetime
sys.setrecursionlimit(10**8)
INF = float("inf")
mod = 10**9 + 7
eps = 10**-7
AtoZ = [chr(i) for i in range(65, 65 + 26)]
atoz = [chr(i) for i in range(97, 97 + 26)]
def inpl():
return list(map(int, input().split()))
def inpl_s():
return list(input().split())
N = int(input())
aa = inpl()
SUM = sum(aa)
SUMaa_L = [0]
SUMaa_R = [SUM]
tmp = 0
for a in aa:
tmp += a
SUMaa_L.append(tmp)
SUMaa_R.append(SUM - tmp)
cutL = []
l = 1
SUML = SUMaa_L[l]
for cutM in range(2, N - 1):
SUM = SUMaa_L[cutM]
while SUML < SUM / 2:
l += 1
SUML = SUMaa_L[l]
l1, l2 = SUMaa_L[l - 1], SUMaa_L[l]
r1, r2 = SUM - l1, SUM - l2
if abs(l1 - r1) > abs(l2 - r2):
cutL.append([l2, r2])
else:
cutL.append([l1, r1])
cutR = []
r = N - 1
SUMR = SUMaa_R[r]
for cutM in reversed(range(2, N - 1)):
SUM = SUMaa_R[cutM]
while SUMR < SUM / 2:
r -= 1
SUMR = SUMaa_R[r]
l1, l2 = SUMaa_R[r + 1], SUMaa_R[r]
r1, r2 = SUM - l1, SUM - l2
if abs(l1 - r1) > abs(l2 - r2):
cutR.append([l2, r2])
else:
cutR.append([l1, r1])
ans = INF
for i in range(N - 3):
a, b = cutL[i]
c, d = cutR[N - 4 - i]
ans = min(ans, max(a, b, c, d) - min(a, b, c, d))
print(ans)
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Find the minimum possible absolute difference of the maximum and the minimum
among P,Q,R,S.
* * * | s267206616 | Accepted | p03310 | 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()))
cut_1 = 1
cut_2 = 2
cut_3 = 3
A_0 = A[:cut_1]
S_0 = sum(A_0)
A_1 = A[cut_1:cut_2]
S_1 = sum(A_1)
A_2 = A[cut_2:cut_3]
S_2 = sum(A_2)
A_3 = A[cut_3:]
S_3 = sum(A_3)
def irekae(S, T, i):
if abs(S - T) > abs((S - A[i - 1]) - (T + A[i - 1])):
S = S - A[i - 1]
T = T + A[i - 1]
i = i - 1
elif abs(T - S) > abs((T - A[i]) - (S + A[i])):
# print(T,S,T-S,i)
S = S + A[i]
T = T - A[i]
i = i + 1
# print(T,S,T-S,i)
return (S, T, i)
MIN = []
cut_1b = 0
cut_3b = 0
for cut_2 in range(2, N - 1):
cut_1b = 0
cut_3b = 0
while cut_1b != cut_1:
cut_1b = cut_1
S_0, S_1, cut_1 = irekae(S_0, S_1, cut_1)
# print(cut_1)
while cut_3b != cut_3:
cut_3b = cut_3
S_2, S_3, cut_3 = irekae(S_2, S_3, cut_3)
MIN += [max([S_0, S_1, S_2, S_3]) - min([S_0, S_1, S_2, S_3])]
# print(cut_1,cut_2,cut_3,S_0,S_1,S_2,S_3)
S_1 += A[cut_2]
S_2 -= A[cut_2]
print(min(MIN))
| Statement
Snuke has an integer sequence A of length N.
He will make three cuts in A and divide it into four (non-empty) contiguous
subsequences B, C, D and E. The positions of the cuts can be freely chosen.
Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively. Snuke is
happier when the absolute difference of the maximum and the minimum among
P,Q,R,S is smaller. Find the minimum possible absolute difference of the
maximum and the minimum among P,Q,R,S. | [{"input": "5\n 3 2 4 1 2", "output": "2\n \n\nIf we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3. Here,\nthe maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute\ndifference of 2. We cannot make the absolute difference of the maximum and the\nminimum less than 2, so the answer is 2.\n\n* * *"}, {"input": "10\n 10 71 84 33 6 47 23 25 52 64", "output": "36\n \n\n* * *"}, {"input": "7\n 1 2 3 1000000000 4 5 6", "output": "999999994"}] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.