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 maximum number of apple pies we can make with what we have.
* * * | s023213677 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | x, y = input().split()
x = int(x)
y = int(y)
z = int((x * 3 + y) / 2)
print(z)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s280509730 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | A = int(input("enter no. of apples:"))
print("number of apple pies =", 3 * A / 2)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s733402440 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | print(eval(input().replace(" ", "*3+")) // 2)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s730278368 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | 0
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s309095021 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | a, p = [int(i) for i in input().split()]
x = 3 * a + p
num = x // 2
print(num)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s640904181 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | n = int(input())
print(180 * (n - 2))
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s318298852 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | import sys
input = sys.stdin.readline
inf = float("inf")
mod = 10**9 + 7
def INT_(n):
return int(n) - 1
def MI():
return map(int, input().split())
def MF():
return map(float, input().split())
def MI_():
return map(INT_, input().split())
def LI():
return list(MI())
def LI_():
return [int(x) - 1 for x in input().split()]
def LF():
return list(MF())
def LIN(n: int):
return [input() for _ in range(n)]
def LLIN(n: int):
return [LI() for _ in range(n)]
def LLIN_(n: int):
return [LI_() for _ in range(n)]
def LLI():
return [list(map(int, l.split())) for l in input()]
def I():
return int(input())
def F():
return float(input())
def ST():
return input().replace("\n", "")
def main():
A, P = MI()
print((A * 3 + P) // 2)
if __name__ == "__main__":
main()
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s636045549 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | import sys
from operator import itemgetter
inputs = sys.stdin.readline
N, Q = map(int, inputs().split())
def main():
E = [None] * 2 * N
for n in range(N):
a, b, c = map(int, inputs().split())
E[2 * n] = (a - c, 1, c)
E[2 * n + 1] = (b - c, -1, c)
E.sort(key=itemgetter(0))
tuple(tuple(E))
mini = 1000000001
kouho = {1000000001}
my_add = kouho.add
my_discard = kouho.discard
q = 0
d = int(inputs())
flag = False
cont = False
for e in E:
while e[0] > d:
if cont == True:
if mini < 1000000001:
print(mini)
else:
print(-1)
else:
mini = min(kouho)
if mini < 1000000001:
print(mini)
else:
print(-1)
cont = True
if q < Q - 1:
q = q + 1
d = int(inputs())
else:
flag = True
q = q + 1
break
if flag == True:
break
if e[1] == 1:
my_add(e[2])
if mini > e[2]:
mini = e[2]
elif e[1] == -1:
my_discard(e[2])
cont = False
for t in [None] * (Q - q):
print(-1)
def sub():
V = [list(map(int, inputs().split())) for p in [0] * N]
M = [int(inputs()) for p in [0] * Q]
V.sort(key=lambda x: (x[2], x[0], x[1]))
for v in V:
v[0] -= v[2]
v[1] -= v[2] + 1
for m in M:
for n in range(N):
if V[n][0] <= m and m <= V[n][1]:
print(V[n][2])
break
else:
print(-1)
if N > 200000:
sub()
else:
main()
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s941402460 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | n, m = map(int, input().split())
s = []
k = []
p = []
for i in range(m):
q = input().split()
k.append(int(q[0]))
q.remove(q[0])
s.append(q)
# print(q)
for j in range(k[i]):
s[i][j] = int(q[j])
# print(s)
q = input().split()
for j in range(m):
p.append(int(q[j]))
count = 0
for i in range(pow(2, n)):
list = []
for o in range(n):
list.append(i % pow(2, o + 1))
i = i // pow(2, o + 1)
# print(list)
for j in range(m):
wa = 0
for l in range(k[j]):
wa += list[s[j][l] - 1]
if wa == p[j] * 2:
count += 1
print(count)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s297465704 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | # -*- coding: utf-8 -*-
# スペース区切りの整数の入力
import sys
s = sys.stdin.readlines()
N, Q = map(int, s[0].split())
x_dict = {}
max_x = 0
for e in s[1:N]:
S, T, X = map(int, e.split())
if X in x_dict:
for i in range(S - 1, T):
x_dict[X].add(i + 0.5)
else:
x_dict[X] = {S - 0.5}
for i in range(S, T):
x_dict[X].add(i + 0.5)
if max_x < X:
max_x = X
for e in s[N + 1 :]:
D = int(e)
place = 0
inf_flg = 1
for i in range(max_x + 1):
time = D + i + 0.5
if place in x_dict:
if time in x_dict[place]:
print(place)
inf_flg = 0
break
place += 1
if inf_flg == 1:
print(-1)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s704483990 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | # N,M = 2, 2
N, M = map(int, input().split())
# kの値は特に必要ないので最初から取得しない
# S = [[1, 2], [2]]
# p = [0, 1]
S = [list(map(int, input().split()))[1:] for i in range(M)]
p = list(map(int, input().split()))
ans = 0
for i in range(1 << N): # スイッチのオンオフ状態(0, 1, 2, 3)
count_2 = -1
bit = list(str(bin(i))) # 0b0, 0b1, 0b10...
fail = 0
# 桁埋め処理
ketaume = (1 << N) // 2 + 2 - len(bit)
if ketaume > 0:
for i in range(ketaume):
bit.insert(2, "0")
# print("search target={}".format(bit))
for s in S: # [1, 2], [2]の階層
count_2 += 1 # 何番目のSかを数えたい
count = 0
for t in s: # 1, 2の階層
# print("bit = {}".format(bit[-t]))
if int(bit[-t]) == 1:
count += 1 # ついているスイッチの数を数える
# print("{}-> number of switch is {}".format(s, count))
# ついているスイッチが偶数か奇数かを判定
# print("if mod is {}, OK".format(p[count_2]))
# print("")
if not count % 2 == p[count_2]:
fail = 1 # 望ましくなければ,失敗フラグを立ててブレイク
break
if fail == 0:
# print("ans += 1")
ans += 1
print(ans)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s255341407 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | parents = list(map(int, input().split(" ")))
print((parents[0] * 3 + parents[1]) // 2)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s661473876 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | inp = input()
A = int(inp.split()[0])
P = int(inp.split()[1])
result = int((A * 3 + P) / 2)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s636108084 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | a, p = list(map(int, input().split()))
ap = a * 3 + p
print(ap // 2)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s833352171 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | li = list(map(int, input().split()))
print(int((li[0] * 3 + li[1]) / 2))
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s194441007 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | N = list(map(int, input().split()))
sums = (N[0] * 3 + N[1]) / 2
print(int(sums))
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s794685879 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | A, B = map(int, input())
print((A * 3 + B) // 2)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s713713201 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | n, a, b, c, d = map(int, input().split())
s = input()
if c < d:
s = list(s)
h = s[b:d]
for i in range(1, len(h)):
if "##" in h:
print("No")
break
else:
hh = s[a:c]
for i in range(1, len(hh)):
if "##" in hh:
print("No")
break
else:
print("Yes")
else:
if "..." in s[a:d]:
h = s[a:c]
for i in range(1, len(h)):
if h[i] == h[i - 1] and h[i] == "#":
print("No")
break
else:
hh = s[b:d]
for i in range(1, len(hh)):
if hh[i] == hh[i - 1] and hh[i] == "#":
print("No")
break
else:
print("Yes")
else:
s = list(s)
h = s[b:d]
for i in range(1, len(h)):
if h[i] == h[i - 1] and h[i] == "#":
print("No")
break
else:
print("No")
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s868813775 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | N, K = map(int, input().split())
d = list(map(int, input().split()))
left = [0]
left_m = []
right = [0]
right_m = []
for i, l, r in zip(range(K), d, d[::-1]):
left.append(left[i] + l)
left_m.append(l if l < 0 else 0)
right.append(right[i] + r)
right_m.append(r if r < 0 else 0)
point = 0
for i in range(K):
for j in range(K - i):
p = K - i - j
minus = list(sorted(left_m[:i] + right_m[:j]))
point = max(point, left[i] + right[j] - sum(minus[:p]))
print(point)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s102702387 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | N, M = map(int, input().split())
n_switch = []
switch = []
for i in range(M):
s = list(map(int, input().split()))
n_switch.append(s[0])
switch.append(s[1:])
p = list(map(int, input().split()))
ans = 0
for i in range(2**N):
x = 1
b = bin(i)[2:].rjust(N, "0")
for j in range(M):
n_light = 0
for k in range(n_switch[j]):
if b[switch[j][k] - 1] == "1":
n_light += 1
if n_light % 2 != p[j]:
x *= 0
break
if x == 1:
ans += 1
print(ans)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s754538705 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | lst = list()
for idx in range(int(input())):
k, v = input().split()
lst.append((k, int(v), idx + 1))
lst.sort(key=lambda t: (t[0], -t[1]), reverse=False)
for t in lst:
print(t[2])
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s920694020 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | N, M = map(int, input().split())
S = [list(map(int, input().split()))[1:] for _ in range(M)]
P = list(map(int, input().split()))
a = 0
for n in range(2**N):
s_count = 0
for j, s in enumerate(S):
count = 0
for t in s:
if n & (1 << t):
count |= 1 << t
s_count ^= count
if bin(s_count).count("1") % 2 != P[j]:
break
else:
a += 1
print(a)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s045559650 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | n, k = map(int, input().split())
a = list(map(int, input().split()))
g = 0
for i in range(0, min(n, k)):
for j in range(0, min(k - i, n - i)):
s = sorted(a[:i] + a[n - j :])
p = sum(s)
t = i + j
c = 0
while len(s) >= c + 1 and t + 1 <= k:
if s[c] < 0:
p -= s[c]
c += 1
t += 1
else:
break
g = max(g, p)
print(g)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s864915033 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | print((lambda x, y: (x * 3 + y) // 3)(*map(int, input().split())))
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s920966639 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | n = input()
print(int((int(n[0]) * 3 + int(n[2])) / 2))
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s169209995 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | n, p = map(int, input().split())
print((p // 2) + ((p % 2) + n) // 2)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s895594487 | Accepted | p03029 | Input is given from Standard Input in the following format:
A P | a, p = list(map(int, input().split()))
ans = a * 3
ans += p
print(int(ans / 2))
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s802754562 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | apple, peace = map(int, input().split())
print(apple * 3 + peace // 3)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s461207103 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | a, p = [*map(int, input().split())]
print("{}".format((a * 3 + p) // 2))
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s572191605 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | i = list(map(int, input().split()))
Apple = 3 * i[0] + i[1]
Apple // 2
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s298192331 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | i = list(map(int, input().split()))
A = i[0]
P = i[1]
S = (A / 3 + P) // 2
print(S)
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s265782013 | Wrong Answer | p03029 | Input is given from Standard Input in the following format:
A P | def pi(a, p):
return (a * 3 + p) // 2
| Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the maximum number of apple pies we can make with what we have.
* * * | s885902897 | Runtime Error | p03029 | Input is given from Standard Input in the following format:
A P | A,P=map(int,input().split())
B=(3A+P)//2
print(B) | Statement
We have A apples and P pieces of apple.
We can cut an apple into three pieces of apple, and make one apple pie by
simmering two pieces of apple in a pan.
Find the maximum number of apple pies we can make with what we have now. | [{"input": "1 3", "output": "3\n \n\nWe can first make one apple pie by simmering two of the three pieces of apple.\nThen, we can make two more by simmering the remaining piece and three more\npieces obtained by cutting the whole apple.\n\n* * *"}, {"input": "0 1", "output": "0\n \n\nWe cannot make an apple pie in this case, unfortunately.\n\n* * *"}, {"input": "32 21", "output": "58"}] |
Print the answer.
* * * | s287666932 | Accepted | p03880 | The input is given from Standard Input in the following format:
N
a_1
:
a_N | def solve(n, a):
"""
References
----------
[1]. 足跡-sokuseki-, CODE FESTIVAL 2016 Grand Final C - Cheating Nim, http://wk1080id.hatenablog.com/entry/2018/08/17/234023
[2]. iroiro, CODE FESTIVAL 2016 Grand Final C - Cheating Nim, http://xxxasdfghjk999.hatenablog.jp/entry/2018/11/01/235259
[3]. Yang33, 競プロにおけるNim、Grundy数とNimK, http://yang33-kassa.hatenablog.com/entry/2017/12/21/202812
"""
x = 0
for i in range(n):
x ^= a[i]
b = [a[i] ^ (a[i] - 1) for i in range(n)]
mapping = {2**k - 1: 0 for k in range(31)}
for y in b:
mapping[y] += 1
ans = 0
for k in range(30, 0, -1):
y = 2**k - 1
z = 2 ** (k - 1)
if (x & z) > 0 and (mapping[y] > 0):
ans += 1
mapping[y] -= 1
x ^= y
return ans if x == 0 else -1
n = int(input())
a = [int(input()) for i in range(n)]
print(solve(n, a))
| Statement
A cheetah and a cheater are going to play the game of Nim. In this game they
use N piles of stones. Initially the i-th pile contains a_i stones. The
players take turns alternately, and the cheetah plays first. In each turn, the
player chooses one of the piles, and takes one or more stones from the pile.
The player who can't make a move loses.
However, before the game starts, the cheater wants to cheat a bit to make sure
that he can win regardless of the moves by the cheetah. From each pile, the
cheater takes zero or one stone and eats it before the game. In case there are
multiple ways to guarantee his winning, he wants to minimize the number of
stones he eats.
Compute the number of stones the cheater will eat. In case there is no way for
the cheater to win the game even with the cheating, print `-1` instead. | [{"input": "3\n 2\n 3\n 4", "output": "3\n \n\nThe only way for the cheater to win the game is to take stones from all piles\nand eat them.\n\n* * *"}, {"input": "3\n 100\n 100\n 100", "output": "-1"}] |
Print the answer.
* * * | s547721386 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | N = int(input())
for i in range(6):
if 10**i <= N < 10 ** (i + 1):
a = i + 1
break
b = N // (10 ** (a - 1))
m = 0
if a >= 3:
for j in range(a - 2):
m += 10**j
table = [[0 + m] * 9 for _ in range(9)]
if a == 2:
for i in range(0, 9):
for j in range(0, 9):
if (i + 1) * 10 + j <= N:
table[i][j] += 1
if a >= 2:
for i in range(0, 9):
table[i][i] += 1
else:
for i in range(0, 9):
if i + 1 <= N:
table[i][i] += 1
if a >= 3:
for i in range(0, b - 1):
for j in range(0, 9):
table[i][j] += 10 ** (a - 2)
for j in range(0, 9):
if (N - (b * 10 ** (a - 1) + j + 1)) // 10 >= 1:
table[b - 1][j] += (N - (b * 10 ** (a - 1) + j + 1)) // 10
A = 0
for i in range(0, 9):
for j in range(i, 9):
A += table[i][j] * table[j][i]
B = 0
for i in range(0, 9):
B += table[i][i] * table[i][i]
print((A - B) * 2 + B)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s143774236 | Runtime Error | p02792 | Input is given from Standard Input in the following format:
N | # -*- coding: utf-8 -*-
import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [[c] * b for i in range(a)]
def list3d(a, b, c, d):
return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
sys.setrecursionlimit(10**9)
INF = 10**18
MOD = 10**9 + 7
class BIT:
"""Binary Indexed Tree"""
def __init__(self, n):
# 0-indexed
n += 1
nv = 1
while nv < n:
nv *= 2
self.size = nv
self.tree = [0] * nv
def sum(self, i):
"""[0, i]を合計する"""
s = 0
i += 1
while i > 0:
s += self.tree[i - 1]
i -= i & -i
return s
def add(self, i, x):
"""値の追加:添字i, 値x"""
i += 1
while i <= self.size:
self.tree[i - 1] += x
i += i & -i
def get(self, l, r=None):
"""区間和の取得 [l, r)"""
# 引数が1つなら一点の値を取得
if r is None:
r = l + 1
res = 0
if r:
res += self.sum(r - 1)
if l:
res -= self.sum(l - 1)
return res
N = INT()
A = LIST()
bit = BIT(N)
ans = 0
for i in range(N):
# 自分より左にある、自分より小さな数の個数
if bit.sum(A[i]) == 0:
ans += 1
bit.add(A[i], 1)
print(ans)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s711339541 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | n = int(input())
a = 10
b = 0
while n // a != 0:
a = a * 10
b += 1
c = (n * 10) // a
d = n % 10
print(c, d)
if b == 0:
print(n)
elif b == 1:
m = 9
if d == 0:
c += -1
d = 10
if c > d:
m += (c - 2) * (c - 1) + (2 * d)
m += 3 * (c - 1)
print(m)
else:
m += c * (c - 1)
m += 3 * c
print(m)
elif b == 2:
m = 108
e = (n % 100) // 10
if c > d:
m += 5 * ((c - 1) * 10 + e)
m += 2 * (8 * ((c - 1) * 10 + e)) + 2 * d
print(m)
else:
m += 5 * ((c - 1) * 10 + e + 1)
m += 2 * (8 * ((c - 1) * 10 + e)) + 2 * (d - 1)
print(m)
elif b == 3:
m = 1998
e = (n % 100) // 10
if c > d:
m += 25 * ((c - 1) * 10 + e)
m += 22 * (8 * ((c - 1) * 10 + e)) + 22 * d
print(m)
else:
m += 25 * ((c - 1) * 10 + e + 1)
m += 22 * (8 * ((c - 1) * 10 + e)) + 22 * (d - 1)
print(m)
elif b == 4:
m = 20088
e = (n % 100) // 10
if c > d:
m += 225 * ((c - 1) * 10 + e)
m += 225 * (8 * ((c - 1) * 10 + e)) + 225 * d
print(m)
else:
m += 225 * ((c - 1) * 10 + e + 1)
m += 225 * (8 * ((c - 1) * 10 + e)) + 225 * (d - 1)
print(m)
elif b == 5:
m = 202338
e = (n % 100) // 10
if c > d:
m += 2225 * ((c - 1) * 10 + e)
m += 2222 * (8 * ((c - 1) * 10 + e)) + 2225 * d
print(m)
else:
m += 2225 * ((c - 1) * 10 + e + 1)
m += 2225 * (8 * ((c - 1) * 10 + e)) + 2225 * (d - 1)
print(m)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s892346965 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | N = int(input())
print((N // 10) ** 2 + N % 5 + 8)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s384133441 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | A = int(input())
c = A // 10
if A % 10 == 0:
print(c**2 + 8)
elif A < 10:
print(A)
else:
n = A % 10
t = len(str(A))
b = A // 10 ** (t - 1)
if n >= b:
print(c**2 + 2 * c + 9)
else:
print(c**2 + 2 * c + 6)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s871082151 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | # Lmao
n = int(input())
print(
17
if n == 25
else (
1
if n == 1
else (
108
if n == 100
else 40812 if n == 2020 else 400000008 if n == 200000 else 200
)
)
)
# XD:)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s486266946 | Runtime Error | p02792 | Input is given from Standard Input in the following format:
N | x = int(input())
x = x + 1
for i in range(x, 9999):
i = str(i)
if i[0] in i[1:]:
continue
elif i[1] in i[2:]:
continue
elif i[2] in i[3]:
continue
else:
print(i)
break
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s337298700 | Runtime Error | p02792 | Input is given from Standard Input in the following format:
N | def resolve():
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-(n**0.5) // 1)) + 1):
if temp % i == 0:
cnt = 0
while temp % i == 0:
cnt += 1
temp //= i
arr.append([i, cnt])
if temp != 1:
arr.append([temp, 1])
if arr == []:
arr.append([n, 1])
return arr
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
import copy
gcddict = {}
adiv = []
for i in range(n):
primes = factorization(a[i])
adiv.append(primes)
for j in range(len(primes)):
if primes[j][0] in gcddict:
gcddict[primes[j][0]] = max(gcddict[primes[j][0]], primes[j][1])
else:
gcddict[primes[j][0]] = primes[j][1]
ans = 0
for i in range(n):
val = 1
test = copy.deepcopy(gcddict)
primes = adiv[i]
for j in range(len(primes)):
test[primes[j][0]] -= primes[j][1]
for k in test.keys():
val *= pow(k, test[k], mod)
ans += val
print(ans % mod)
resolve()
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s097730957 | Accepted | p02792 | Input is given from Standard Input in the following format:
N | N = int(input())
# N以下で先頭と末尾がkのやつ
TTS = [1, 1, 10, 100, 1000, 10000, 100000]
TTW = [0, 1, 10, 100, 1000, 10000, 100000]
def ttk(N, k):
c = 0
Nd = len(str(N))
# print("Nd",Nd)
for i in range(Nd - 1):
# print(TTS[i])
c += TTS[i]
if Nd >= 3:
for i in range(10 ** (Nd - 2)):
a = int(str(k) + (Nd - 2 - len(str(i))) * "0" + str(i) + str(k))
# print("a=",a)
if a <= N:
c += 1
else:
break
elif Nd == 2:
a = int(str(k) * 2)
if a <= N:
c += 1
elif Nd == 1:
a = k
if a <= N:
c += 1
return c
# (i)先頭末尾が同じもの
"""
y = 0
for i in range(1,10):
y += ttk(N,i)**2
print(y)
"""
def ttpq(N, p, q):
c = 0
Nd = len(str(N))
# print("Nd",Nd)
for i in range(Nd - 1):
# print(TTW[i])
c += TTW[i]
if Nd >= 3:
for i in range(10 ** (Nd - 2)):
a = int(str(p) + (Nd - 2 - len(str(i))) * "0" + str(i) + str(q))
# print(a)
if a <= N:
c += 1
else:
break
elif Nd == 2:
a = int(str(p) + str(q))
if a <= N:
c += 1
elif Nd == 1:
c = 0
return c
# (ii)先頭末尾が違うもの
y = 0
for i in range(1, 10):
for j in range(i, 10):
# print("i:{},j,{}".format(i,j))
if i == j:
# print(" added",ttk(N,i)**2)
y += ttk(N, i) ** 2
else:
# print(" added",ttpq(N,i,j)**2)
y += ttpq(N, i, j) * ttpq(N, j, i) * 2
# print(y)
print(y)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s760132959 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | n = int(input())
x = n // 10
basenum = x * x + 8
addnum = 0
# print("{0} {1}".format(x* 10 + 1, n))
hassame = False
for i in range(x * 10 + 1, n + 1):
# print(i)
for j in range(1, n + 1):
# print(j)
if str(i)[0] == str(j)[-1] and str(i)[-1] == str(j)[0]:
addnum += 1
if i == j:
hassame = True
# print("{0},{1}".format(i, j))
res = basenum + addnum * 2
if hassame:
res -= 1
# print("n={0}, x={1}, basenum={2}, addnum={3} res ={4}".format(n,x,basenum,addnum,res))
print(res)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s707424777 | Accepted | p02792 | Input is given from Standard Input in the following format:
N | import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip() # ignore trailing spaces
n = ni()
ct = 0
for i in range(1, n + 1):
head = i % 10
if head == 0:
continue
k = i
tail = 0
while k > 0:
tail = k % 10
k //= 10
if head == tail:
if head <= n:
ct += 1
# (head)...(tail)
d = 10
while d <= n:
# head*d + tail
if n >= (head + 1) * d:
ct += d // 10
elif n < head * d:
pass
else:
ct += (n - head * d - tail + 10) // 10
d *= 10
print(ct)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s039960746 | Accepted | p02792 | Input is given from Standard Input in the following format:
N | N = int(input())
sn = str(N)
ans = 0
dp = [[[0] * 2 for i in range(10)] for j in range(10)]
dp[0][0][0] = 1
for nketa in range(len(str(N))):
ndp = [[[0] * 2 for i in range(10)] for j in range(10)]
for top in range(10):
for app in range(10):
for fl in range(2):
for nadd in range(10):
if top == 0 and fl == 0 and int(sn[nketa]) == nadd:
ndp[nadd][nadd][0] += dp[top][app][fl]
if top == 0 and fl == 0 and int(sn[nketa]) > nadd:
ndp[nadd][nadd][1] += dp[top][app][fl]
if top == 0 and fl == 1:
ndp[nadd][nadd][1] += dp[top][app][fl]
if top != 0 and fl == 0 and int(sn[nketa]) == nadd:
ndp[top][nadd][0] += dp[top][app][fl]
if top != 0 and fl == 0 and int(sn[nketa]) > nadd:
ndp[top][nadd][1] += dp[top][app][fl]
if top != 0 and fl == 1:
ndp[top][nadd][fl] += dp[top][app][fl]
dp = ndp
# print (dp[0][0][1])
# print (dp[9][9][1])
ans = 0
for i in range(9):
i += 1
for j in range(9):
j += 1
ans += sum(dp[i][j]) * sum(dp[j][i])
print(ans)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s540589956 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | N = int(input())
total = 0
for i in [idx + 1 for idx in range(N)]:
a = int(str(i)[0])
b = int(str(i)[-1])
if b != 0:
if a == b:
if N < 10:
if a <= N:
total += 1
elif N < 100:
total += 1
if a * 10 + a <= N:
total += 1
elif N < 1000:
total += 1
total += 1
if a * 100 + 90 + a <= N:
total += 10
elif a * 100 + a >= N:
if N % 10 < a:
total += int(str(N)[1]) - 1
else:
total += int(str(N)[1])
elif N < 10000:
total += 1
total += 1
total += 10
if a * 1000 + 990 + a <= N:
total += 100
elif a * 1000 + a >= N:
if N % 10 < a:
total += int(str(N)[1]) * 10 + int(str(N)[2]) - 1
else:
total += int(str(N)[1]) * 10 + int(str(N)[2])
elif N < 100000:
total += 1
total += 1
total += 10
total += 100
if a * 10000 + 9990 + a <= N:
total += 1000
elif a * 10000 + a >= N:
if N % 10 < a:
total += (
int(str(N)[1]) * 100
+ int(str(N)[2]) * 10
+ int(str(N)[3])
- 1
)
else:
total += (
int(str(N)[1]) * 100 + int(str(N)[2]) * 10 + int(str(N)[3])
)
elif N < 1000000:
total += 1
total += 1
total += 10
total += 100
total += 1000
if a * 100000 + 99990 + a <= N:
total += 10000
elif a * 100000 + a >= N:
if N % 10 < a:
total += (
int(str(N)[1]) * 1000
+ int(str(N)[2]) * 100
+ int(str(N)[3]) * 10
+ int(str(N)[4])
- 1
)
else:
total += (
int(str(N)[1]) * 1000
+ int(str(N)[2]) * 100
+ int(str(N)[3]) * 10
+ int(str(N)[4])
)
else:
if N < 100:
if b * 10 + a <= N:
if N % 10 < a:
total += int(str(N)[1]) - 1
else:
total += int(str(N)[1])
elif N < 1000:
total += 1
if b * 100 + 90 + a <= N:
total += 10
elif b * 100 + a >= N:
if N % 10 < a:
total += int(str(N)[1]) * 10 + int(str(N)[2]) - 1
else:
total += int(str(N)[1]) * 10 + int(str(N)[2])
elif N < 10000:
total += 1
total += 10
if b * 1000 + 990 + a <= N:
total += 100
elif b * 1000 + a >= N:
if N % 10 < a:
total += int(str(N)[1]) * 10 + int(str(N)[2]) - 1
else:
total += int(str(N)[1]) * 10 + int(str(N)[2])
elif N < 100000:
total += 1
total += 10
total += 100
if b * 10000 + 9990 + a <= N:
total += 1000
elif b * 10000 + a >= N:
if N % 10 < a:
total += (
int(str(N)[1]) * 100
+ int(str(N)[2]) * 10
+ int(str(N)[3])
- 1
)
else:
total += (
int(str(N)[1]) * 100 + int(str(N)[2]) * 10 + int(str(N)[3])
)
elif N < 1000000:
total += 1
total += 10
total += 100
total += 1000
if b * 100000 + 99990 + a <= N:
total += 10000
elif b * 100000 + a >= N:
total += (
int(str(N)[1]) * 1000
+ int(str(N)[2]) * 100
+ int(str(N)[3]) * 10
+ int(str(N)[4])
)
print(total)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s774526615 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | # -*- coding: utf-8 -*-
"""
Created on Sun Jan 19 21:23:54 2020
@author: over-
"""
N = input()
L = len(N)
c = 0
for i in range(1, int(N) + 1):
x = int(str(i)[0])
y = int(str(i)[-1])
if x == y:
if L == 1:
d = 1
if L == 2:
d = 1
if int(N) >= int("{}{}".format(x, x)):
d += 1
if L == 3:
d = 1 + 1
if int(N) >= int("{}9{}".format(x, x)):
d += 10
elif int(N) >= int("{}0{}".format(x, x)):
d += int(N[1:-1]) + 1
if L == 4:
d = 1 + 1 + 10
if int(N) >= int("{}99{}".format(x, x)):
d += 100
elif int(N) >= int("{}00{}".format(x, x)):
d += int(N[1:-1]) + 1
if L == 5:
d = 1 + 1 + 10 + 100
if int(N) >= int("{}999{}".format(x, x)):
d += 1000
elif int(N) >= int("{}000{}".format(x, x)):
d += int(N[1:-1]) + 1
if L == 6:
d = 1 + 1 + 10 + 100 + 1000
if int(N) >= int("{}9999{}".format(x, x)):
d += 10000
elif int(N) >= int("{}0000{}".format(x, x)):
d += int(N[1:-1]) + 1
c += d
else:
if y != 0:
if L == 2:
d = 0
if int(N) >= int("{}{}".format(y, x)):
d += 1
if L == 3:
d = 1
if int(N) >= int("{}9{}".format(y, x)):
d += 10
elif int(N) >= int("{}0{}".format(y, x)):
d += int(N[1:-1]) + 1
if L == 4:
d = 1 + 10
if int(N) >= int("{}99{}".format(y, x)):
d += 100
elif int(N) > int("{}00{}".format(y, x)):
d += int(N[1:-1]) + 1
if L == 5:
d = 1 + 10 + 100
if int(N) >= int("{}999{}".format(y, x)):
d += 1000
elif int(N) >= int("{}000{}".format(y, x)):
d += int(N[1:-1]) + 1
if L == 6:
d = 1 + 10 + 100 + 1000
if int(N) >= int("{}9999{}".format(y, x)):
d += 10000
elif int(N) >= int("{}0000{}".format(y, x)):
d += int(N[1:-1]) + 1
c += d
print(c)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s932503910 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | from sys import stdin
import numpy as np
if __name__ == "__main__":
N = int(stdin.readline().rstrip())
count = 0
c_ij = np.array(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
)
for i in range(1, N):
c_ij[int(str(i)[0]), int(str(i)[-1])] += 1
c_ji = np.array(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
)
for i in range(1, N):
c_ji[int(str(i)[-1]), int(str(i)[0])] += 1
print(np.sum(c_ij * c_ji))
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s893016848 | Wrong Answer | p02792 | Input is given from Standard Input in the following format:
N | print(1)
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s049080478 | Runtime Error | p02792 | Input is given from Standard Input in the following format:
N | def left(text, n):
return text[:n]
def right(text, n):
return text[-n:]
def mid(text, n, m):
return text[n-1:n+m-1]
n = int(input())
rangeSet = [[0]*10 for i in range(10)]
# [1,9][9,1]など先頭+最終の組み合わせを設定
for i in range(1,n+1):
first = int(left(str(i),1))
end = int(right(str(i),1))
rangeSet[first][end] += 1
kumiawase = 0
for k in range(1,10):
for l in range(1,10):
tmp = rangeSet[k][l] * rangeSet[l][k]
kumiawase += tmp
print(kumiawase) | Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print the answer.
* * * | s033741974 | Accepted | p02792 | Input is given from Standard Input in the following format:
N | """
108345237845804
498073591
are an example match, because (1,4) and (4,1)
each integer can be encoded as this kind of pair
if there is also
4768281
then we have 2 matches so far, because the
first matches with the second and third
considering 1 and 11 and 111 and 1111 and 11111
each are encoded as (1,1), so we get 9 matches from just these 5...
solution :
just count how many integers get encoded as a certain pair, then calculate
the number of pairs that would be made from them
eg if (1,4) = 10 and (4,1) = 5 then there are 5*10 = 50 pairs
How many encodings? no leading zero, so also ending zero wont get a match,
so 1 to 9 and 1 to 9, is 9*9=81 encodings
"""
def handstand(N):
# first count how many integers have a given encoding
count_encoding = {(x, y): 0 for x in range(1, 10) for y in range(1, 10)}
for i in range(1, N + 1):
string_convert = str(i)
start_val = int(string_convert[0])
end_val = int(string_convert[-1])
if end_val == 0:
continue
count_encoding[start_val, end_val] += 1
# now count the encoding matches
matches_count = 0
for x in range(1, 10):
for y in range(1, 10):
matches_count += count_encoding[x, y] * count_encoding[y, x]
return matches_count
if __name__ == "__main__":
test1 = "25"
test2 = "1"
test3 = "100"
test4 = "2020"
test5 = "200000"
test6 = "16"
test = test4
# in_val = int(test)
in_val = int(input())
print(handstand(in_val))
| Statement
Given is a positive integer N.
Find the number of pairs (A, B) of positive integers not greater than N that
satisfy the following condition:
* When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B. | [{"input": "25", "output": "17\n \n\nThe following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22),\n(3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21),\n(21,12), (22,2), and (22,22).\n\n* * *"}, {"input": "1", "output": "1\n \n\n* * *"}, {"input": "100", "output": "108\n \n\n* * *"}, {"input": "2020", "output": "40812\n \n\n* * *"}, {"input": "200000", "output": "400000008"}] |
Print C in a line. | s168236709 | Runtime Error | p02268 | In the first line _n_ is given. In the second line, _n_ integers are given. In
the third line _q_ is given. Then, in the fourth line, _q_ integers are given. | import sys
if __name__ == "__main__":
data = []
count = 0
for line in sys.stdin:
data.append(line.split())
n = int(data[0][0])
s = list(map(int, data[1]))
q = int(data[2][0])
t = list(map(int, data[3]))
for i in range(0, q - 1):
head = 0
tail = n
while True:
center = len(s) // 2
if s[center] == t[i]:
count = count + 1
print(t[i])
break
elif s[center] < t[i]:
head = center + 1
else:
tail = center - 1
s = s[head : tail + 1]
if head >= tail:
break
| Search II
You are given a sequence of _n_ integers S and a sequence of different _q_
integers T. Write a program which outputs C, the number of integers in T which
are also in the set S. | [{"input": "5\n 1 2 3 4 5\n 3\n 3 4 1", "output": "3"}, {"input": "3\n 1 2 3\n 1\n 5", "output": "0"}, {"input": "5\n 1 1 2 2 3\n 2\n 1 2", "output": "2"}] |
Print C in a line. | s417367235 | Wrong Answer | p02268 | In the first line _n_ is given. In the second line, _n_ integers are given. In
the third line _q_ is given. Then, in the fourth line, _q_ integers are given. | n = input()
n = int(n)
s = input()
s = s.split()
s = [int(x) for x in s]
q = input()
q = int(q)
t = input()
t = t.split()
t = [int(x) for x in t]
n_unique = len(set(s) - set(t))
n_overlap = len(s) - n_unique
print(n_overlap)
| Search II
You are given a sequence of _n_ integers S and a sequence of different _q_
integers T. Write a program which outputs C, the number of integers in T which
are also in the set S. | [{"input": "5\n 1 2 3 4 5\n 3\n 3 4 1", "output": "3"}, {"input": "3\n 1 2 3\n 1\n 5", "output": "0"}, {"input": "5\n 1 1 2 2 3\n 2\n 1 2", "output": "2"}] |
Print C in a line. | s208561298 | Wrong Answer | p02268 | In the first line _n_ is given. In the second line, _n_ integers are given. In
the third line _q_ is given. Then, in the fourth line, _q_ integers are given. | n = int(input())
s = set(input())
n = int(input())
t = set(input())
print(len(t & s) - 1)
| Search II
You are given a sequence of _n_ integers S and a sequence of different _q_
integers T. Write a program which outputs C, the number of integers in T which
are also in the set S. | [{"input": "5\n 1 2 3 4 5\n 3\n 3 4 1", "output": "3"}, {"input": "3\n 1 2 3\n 1\n 5", "output": "0"}, {"input": "5\n 1 1 2 2 3\n 2\n 1 2", "output": "2"}] |
Print C in a line. | s533389777 | Accepted | p02268 | In the first line _n_ is given. In the second line, _n_ integers are given. In
the third line _q_ is given. Then, in the fourth line, _q_ integers are given. | i = lambda: set(input().split())
i()
s = i()
i()
print(len(s & i()))
| Search II
You are given a sequence of _n_ integers S and a sequence of different _q_
integers T. Write a program which outputs C, the number of integers in T which
are also in the set S. | [{"input": "5\n 1 2 3 4 5\n 3\n 3 4 1", "output": "3"}, {"input": "3\n 1 2 3\n 1\n 5", "output": "0"}, {"input": "5\n 1 1 2 2 3\n 2\n 1 2", "output": "2"}] |
Print C in a line. | s700577391 | Accepted | p02268 | In the first line _n_ is given. In the second line, _n_ integers are given. In
the third line _q_ is given. Then, in the fourth line, _q_ integers are given. | a_len = int(input())
a = {n for n in input().split(" ")}
b_len = int(input())
b = {n for n in input().split(" ")}
print(len(a & b))
| Search II
You are given a sequence of _n_ integers S and a sequence of different _q_
integers T. Write a program which outputs C, the number of integers in T which
are also in the set S. | [{"input": "5\n 1 2 3 4 5\n 3\n 3 4 1", "output": "3"}, {"input": "3\n 1 2 3\n 1\n 5", "output": "0"}, {"input": "5\n 1 1 2 2 3\n 2\n 1 2", "output": "2"}] |
Print C in a line. | s244688601 | Accepted | p02268 | In the first line _n_ is given. In the second line, _n_ integers are given. In
the third line _q_ is given. Then, in the fourth line, _q_ integers are given. | import re
question1 = int(input())
question2 = input()
question3 = int(input())
question5 = input()
S = re.split(r" ", question2)
S = list(map(int, S))
T = re.split(r" ", question5)
T = list(map(int, T))
if (question1 == 0) or (question3 == 0):
print(0)
else:
S = set(S)
T = set(T)
matched_list = list(S & T)
print(len(matched_list))
| Search II
You are given a sequence of _n_ integers S and a sequence of different _q_
integers T. Write a program which outputs C, the number of integers in T which
are also in the set S. | [{"input": "5\n 1 2 3 4 5\n 3\n 3 4 1", "output": "3"}, {"input": "3\n 1 2 3\n 1\n 5", "output": "0"}, {"input": "5\n 1 1 2 2 3\n 2\n 1 2", "output": "2"}] |
For each set, print "Yes" if everyone's dinner can be prepared using blood in
a fridge, "No" otherwise (without quotes). | s433164664 | Accepted | p00605 | Input file consists of a number of data sets. One data set is given in
following format:
_N K_
_S_ 1 _S_ 2 ... _S K_
_B_ 11 _B_ 12 ... _B_ 1 _K_
_B_ 21 _B_ 22 ... _B_ 2 _K_
:
_B_ _N_ 1 _B_ _N_ 2 ... _B_ _NK_
_N_ and _K_ indicate the number of family members and the number of blood
types respectively.
_S i_ is an integer that indicates the amount of blood of the _i_ -th blood
type that is in a fridge.
_B ij_ is an integer that indicates the amount of blood of the _j_ -th blood
type that the _i_ -th vampire uses.
The end of input is indicated by a case where _N_ = _K_ = 0. You should print
nothing for this data set. | while True:
n, k = map(int, input().split())
if n == k == 0:
break
s = [int(x) for x in input().split()]
for _ in range(n):
b = [int(x) for x in input().split()]
for i in range(k):
s[i] -= b[i]
print("Yes" if min(s) >= 0 else "No")
| A: Vampirish Night
There is a vampire family of _N_ members. Vampires are also known as extreme
gourmets. Of course vampires' foods are human blood. However, not all kinds of
blood is acceptable for them. Vampires drink blood that _K_ blood types of
ones are mixed, and each vampire has his/her favorite amount for each blood
type.
You, cook of the family, are looking inside a fridge to prepare dinner. Your
first task is to write a program that judges if all family members' dinner can
be prepared using blood in the fridge. | [{"input": "3\n 5 4 5\n 1 2 3\n 3 2 1\n 3 5\n 1 2 3 4 5\n 0 1 0 1 2\n 0 1 1 2 2\n 1 0 3 1 1\n 0 0", "output": "Yes\n No"}] |
Print the maximum possible score at the end of the game.
* * * | s989766160 | Accepted | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | import math
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
loopid = [-1 for _ in range(N)]
loop_found = 0
for i in range(N):
if loopid[i] != -1:
continue
loopid[i] = loop_found
j = P[i] - 1
while loopid[j] == -1:
loopid[j] = loop_found
j = P[j] - 1
loop_found += 1
loops = [[] for _ in range(loop_found)]
for nodeid, id in enumerate(loopid):
loops[id].append(nodeid)
ans = -math.inf
for loop in loops:
K_tmp = K
score_tmp = 0
cs = []
j = loop[0]
for _ in range(len(loop)):
cs.append(C[j])
j = P[j] - 1
if sum(cs) >= 0:
score_tmp += sum(cs) * max(0, K // len(loop) - 1)
K_tmp %= len(loop)
if K >= len(loop):
K_tmp += len(loop)
max_rest = -math.inf
cs = cs + cs + cs
for i in range(1, len(cs)):
cs[i] += cs[i - 1]
j = -1
tmp_min = math.inf
for i, c in enumerate(cs):
max_rest = max(max_rest, c - tmp_min)
if tmp_min >= c:
tmp_min = c
j = i
if i - j >= K_tmp:
tmp_min = cs[j + 1]
j += 1
x = j
for k, c_ in enumerate(cs[j + 1 : i + 1]):
if tmp_min >= c_:
tmp_min = c_
x = k + j + 1
j = x
score_tmp += max_rest
ans = max(ans, score_tmp)
print(ans)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s982030595 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
INF = 10**10
vis = [0] * (N + 1)
def find_cycle(start):
vis[start] = 1
his = [start]
now = start
cost = []
while 1:
new = P[now - 1]
cost.append(C[new - 1])
if vis[new] == 1:
return cost
his.append(new)
vis[new] = 1
now = new
costs = []
for i in range(N):
if not vis[i + 1]:
cost = find_cycle(i + 1)
costs.append(cost)
def cumsum(array):
new = [0]
for i in range(len(array)):
new.append(array[i] + new[-1])
return new
cumcost = []
for i in range(len(costs)):
cost = costs[i]
cumcost.append(cumsum(cost * 2))
def best_margin_cost(idx, r1):
out = -INF
for r2 in range(1, r1 + 1):
for i in range(len(costs[idx])):
tmp = cumcost[idx][i + r2] - cumcost[idx][i]
out = max(tmp, out)
return out
def opt_cost(idx):
l = len(costs[idx])
SUM = sum(costs[idx])
q, r = divmod(K, l)
if K >= l:
return max(SUM, 0) * q + best_margin_cost(idx, l)
else:
return best_margin_cost(idx, K)
ans = -INF
for i in range(len(costs)):
ans = max(ans, opt_cost(i))
print(ans)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s952028855 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
L = [0] * N
Max = -(10**9)
for i in range(N):
if L[i] != 0:
continue
else:
c = 0
L[i] = 1
c += C[i]
a = P[i] - 1
Cs = [c]
Ds = [c]
l = 1
while a != i:
c += C[a]
Ds.append(C[a])
a = P[a] - 1
L[a] = 1
l += 1
Cs.append(c)
if c > 0:
m = -(10**9)
k = K % l
for i in range(1, k + 1):
for j in range(l):
if j + i >= l:
M = Cs[l - 1] - Cs[j] + Cs[i + j - l]
else:
M = Cs[j + i] - Cs[j]
if M > m:
m = M
x = K // l
max0 = c * x + m
else:
if K >= l:
A = [0] * l
A[0] = Ds[0]
for i in range(1, l):
A[i] = min([A[i - 1] + Ds[i], Ds[i]])
max0 = c - A[l - 1]
if max0 == 0:
max0 = max(Ds)
else:
m = -(10**9)
k = K
for i in range(1, k + 1):
for j in range(l):
if j + i >= l:
M = Cs[l - 1] - Cs[j] + Cs[i + j - l]
else:
M = Cs[j + i] - Cs[j]
if M > m:
m = M
max0 = m
if Max < max0:
Max = max0
print(Max)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s327213431 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | N, K = map(int, input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))
ans = 0
check = set()
for i in range(N):
if i in check:
continue
# create loop
tmpK = K
tmpans = 0
loop_check = set()
tmp_idx = i
loop = []
while tmp_idx not in loop_check:
loop.append(C[tmp_idx])
loop_check.add(tmp_idx)
check.add(tmp_idx)
tmp_idx = P[tmp_idx]
S = sum(loop)
L = len(loop)
if S >= 0:
tmpans = S * (tmpK // L)
tmpK %= L
# 部分最大列を検索
# print('loop',loop)
loop *= 2
for i in range(len(loop) - 1):
loop[i + 1] += loop[i]
n = 2 * L
def init_min(init_min_val):
# set_val
for i in range(n):
seg_min[i + num_min - 1] = init_min_val[i]
# built
for i in range(num_min - 2, -1, -1):
seg_min[i] = min(seg_min[2 * i + 1], seg_min[2 * i + 2])
def query_min(p, q):
if q <= p:
return ide_ele_min
p += num_min - 1
q += num_min - 2
res = ide_ele_min
while q - p > 1:
if p & 1 == 0:
res = min(res, seg_min[p])
if q & 1 == 1:
res = min(res, seg_min[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = min(res, seg_min[p])
else:
res = min(min(res, seg_min[p]), seg_min[q])
return res
#####単位元######
ide_ele_min = 10**15
# num_min:n以上の最小の2のべき乗
num_min = 2 ** (n - 1).bit_length()
seg_min = [ide_ele_min] * 2 * num_min
init_min(loop)
local_max = 0
for i, v in enumerate(loop):
if i >= L:
# print('v,min=',v,query_min(max(0,i-tmpK),i),v-query_min( max(0,i-tmpK),i ))
local_max = max(local_max, v - query_min(max(0, i - tmpK), i))
local_max2 = 0
for i, v in enumerate(loop):
if i >= L:
# print('v,min=',v,query_min(max(0,i-tmpK),i),v-query_min( max(0,i-tmpK),i ))
local_max2 = max(local_max2, v - query_min(max(0, i - K), i))
ans = max(ans, local_max + tmpans, local_max2)
if ans == 0:
print(max(C))
else:
print(ans)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s845542574 | Accepted | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [[c] * b for i in range(a)]
def list3d(a, b, c, d):
return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
sys.setrecursionlimit(10**9)
INF = 10**19
MOD = 10**19 + 7
EPS = 10**-10
N, K = MAP()
A = [a - 1 for a in LIST()]
C = LIST()
MAX = 32
N = len(A)
nxt = list2d(MAX, N, -1)
score = list2d(MAX, N, -1)
mx = list2d(MAX, N, -1)
for i, a in enumerate(A):
nxt[0][i] = a
score[0][i] = C[a]
mx[0][i] = max(C[a], 0)
for k in range(1, MAX):
for i in range(N):
nxt[k][i] = nxt[k - 1][nxt[k - 1][i]]
score[k][i] = score[k - 1][i] + score[k - 1][nxt[k - 1][i]]
mx[k][i] = max(mx[k - 1][i], score[k - 1][i] + mx[k - 1][nxt[k - 1][i]])
ans = -INF
K -= 1
for a in A:
cur = a
sm = C[a]
for k in range(MAX - 1, -1, -1):
if K >> k & 1:
ans = max(ans, sm + mx[k][cur])
sm += score[k][cur]
cur = nxt[k][cur]
ans = max(ans, sm)
print(ans)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s751287465 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | n, k = map(int, input().split())
pos = [int(i) for i in input().split()]
score = [int(i) for i in input().split()]
pos = [0] + pos
score = [0] + score
max_point = -(10**12)
dic = {}
for i in range(1, n + 1):
f_pos = i
# print('f_pos',f_pos)
if f_pos not in dic:
point = 0
loop = 0
count = 0
pos_set = set([f_pos])
while True:
f_pos = pos[f_pos]
point += score[f_pos]
# print('pos', f_pos)
# print(point)
count += 1
if f_pos in pos_set:
loop = count
for j in pos_set:
dic[j] = [loop, point]
break
pos_set.add(f_pos)
else:
loop = dic[f_pos][0]
point = dic[f_pos][1]
# print(loop, point)
loop_c = k // loop
r = k % loop
if point > 0:
point *= loop_c
if point > max_point and loop_c > 0:
max_point = point
else:
point = 0
if loop_c > 0:
r = loop
# print(point)
for _ in range(r):
f_pos = pos[f_pos]
point += score[f_pos]
# print(point)
if point > max_point:
max_point = point
# print('max_point')
print(max_point)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s787914255 | Runtime Error | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | n,k=map(int,input().split())
p=list(map(int, input().split()))
c=list(map(int, input().split()))
s=sum(c)
def f(x,m):
tmp=0
a=0
for i in range(x):
tmp+=c[m]
a=max(a,tmp)
m=p[m]
return a
if s>0:
ans=s*(k//n)
tmp=0
for i in range(n):
tmp=max(tmp, f(i,k%n))
ans+=tmp
else:
ans=0
for i in range(n):
ans=max(ans, f(i,min(n,k)))
print(ans) | Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s396134144 | Accepted | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | from itertools import accumulate
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
def solve():
N, K = map(int, input().split())
Ps = list(map(lambda x: int(x) - 1, input().split()))
Cs = list(map(int, input().split()))
cycless = [[] for _ in range(N)]
useds = [0] * N
for no in range(N):
if useds[no]:
continue
x = no
while True:
useds[x] = 1
cycless[no].append(Cs[x])
x = Ps[x]
if x == no:
break
# for no in range(N):
# print('# no:', no, '/ cycless[no]:', cycless[no], file=sys.stderr)
ans = -float("inf")
for no in range(N):
if not cycless[no]:
continue
L = len(cycless[no])
accCycles = list(accumulate([0] + cycless[no] + cycless[no] + cycless[no]))
sumC = sum(cycless[no])
# print('\n# no:', no, '/ sumC:', sumC, file=sys.stderr)
if sumC > 0:
num = K // L
if num > 1:
num -= 1
score = -float("inf")
# print('# K:', K, file=sys.stderr)
for M in range(0, L + K % L + 1):
for st in range(L):
s = accCycles[st + M] - accCycles[st]
if s > score:
score = s
score += sumC * num
else:
score = -float("inf")
for M in range(1, K + 1):
for st in range(L):
s = accCycles[st + M] - accCycles[st]
if s > score:
score = s
elif sumC == 0:
num = K // L
if num > 1:
score = -float("inf")
for M in range(0, 2 * L + 1):
for st in range(L):
s = accCycles[st + M] - accCycles[st]
if s > score:
score = s
else:
score = -float("inf")
for M in range(1, K + 1):
for st in range(L):
s = accCycles[st + M] - accCycles[st]
if s > score:
score = s
else:
score = -float("inf")
for M in range(1, (K if K <= L else L) + 1):
for st in range(L):
s = accCycles[st + M] - accCycles[st]
if s > score:
score = s
if score > ans:
ans = score
# print('# no:', no, '/ score:', score, file=sys.stderr)
print(ans)
solve()
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s664379120 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | N, K = [int(i) for i in input().split()]
P = [int(i) - 1 for i in input().split()]
C = [int(i) for i in input().split()]
def f(s):
tmp = s
circle_length = 0
circle_score = 0
for i in range(1, N + 1):
tmp = P[tmp]
circle_length += 1
circle_score += C[tmp]
if s == tmp:
return (True, circle_length, circle_score)
if i == K:
return (False, K, -(10**10))
def f1(s, circle_length):
tmp = s
max_score = -(10**10)
tmp_score = 0
for i in range(1, circle_length + 1):
tmp = P[tmp]
tmp_score += C[tmp]
if max_score < tmp_score:
max_score = tmp_score
if tmp_score < 0:
return max_score
if i == K:
return max_score
return max_score
def f2(s, circle_length, circle_score):
max_score = circle_score * (K // circle_length)
tmp = s
tmp_max_score = 0
tmp_score = 0
for i in range(1, (K % circle_length) + 1):
tmp = P[tmp]
tmp_score += C[tmp]
if tmp_max_score < tmp_score:
tmp_max_score = tmp_score
if tmp_score < 0:
return max_score + tmp_max_score
if i == K:
return max_score + tmp_max_score
if K % circle_length == 0:
return max_score
return max_score + tmp_max_score
tot_max = -(10**10)
for i in range(N):
is_circle, circle_length, circle_score = f(i)
max_score = f1(i, circle_length)
if is_circle and circle_score > 0:
max_score2 = f2(i, circle_length, circle_score)
if max_score2 > max_score:
max_score = max_score2
if max_score > tot_max:
tot_max = max_score
print(tot_max)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s505535918 | Accepted | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | INF = float("inf")
n, k = list(map(int, input().split()))
P = list(map(int, input().split()))
P = list(map(lambda x: x - 1, P))
C = list(map(int, input().split()))
def simulate(start, k):
result = -INF
cur = start
score = 0
turn = 0
cycle_check = False
while turn < k:
turn += 1
cur = P[cur]
score += C[cur]
result = max(result, score)
# サイクル時の処理短縮
if cur == start and cycle_check == False:
cycle_check = True
# 1周して減ってるなら回す必要なし
if score <= 0:
break
# 1周して増えるなら最後の1周 + αだけ試す
cycle_cnt = (k // turn) - 1
if cycle_cnt < 1:
cycle_cnt = 0
# break
turn = cycle_cnt * turn
score = cycle_cnt * score
return result
result = -INF
for i in range(n):
# if i != 2:
# continue
result = max(result, simulate(i, k))
print(result)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s148335020 | Accepted | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | import sys
sys.setrecursionlimit(700000)
def s_in():
return input()
def n_in():
return int(input())
def l_in():
return list(map(int, input().split()))
n, k = l_in()
P = l_in()
C = l_in()
s = min(k, n)
res = -(10**10)
for start in range(n):
j = start
current = 0
tmp = -(10**10)
for i in range(1, k + 1):
j = P[j] - 1
current += C[j]
tmp = max(tmp, current)
if j == start:
# print(i)
# print(current)
loop = current
if current > 0:
s = k % i
add = current * (k // i)
j = start
current = 0
for i2 in range(1, s + 1):
j = P[j] - 1
current += C[j]
tmp = max(tmp, current + add)
if k // i >= 2:
add = loop * (k // i - 1) # (p-1)*i
j = start
current = 0
for _ in range(i): # i
j = P[j] - 1
current += C[j]
tmp = max(tmp, current + add)
break
res = max(tmp, res)
print(res)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s863789635 | Accepted | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | import sys
input_methods = ["clipboard", "file", "key"]
using_method = 0
input_method = input_methods[using_method]
tin = lambda: map(int, input().split())
lin = lambda: list(tin())
mod = 1000000007
# +++++
def main():
# a = int(input())
n, k = tin()
# s = input()
al = lin()
cl = lin()
dd = [0] * n
for i in range(n):
if dd[i] != 0:
continue
cc = set()
pos = i
point = 0
n_node = 0
while pos not in cc:
cc.add(pos)
pos = al[pos] - 1
point += cl[pos]
n_node += 1
for p in cc:
dd[p] = (point, n_node)
ret = -mod * mod
for i in range(n):
pi, ni = dd[i]
if pi < 0:
point = 0
pmax = -mod * mod
pos = i
for _ in range(ni):
cc.add(pos)
pos = al[pos] - 1
point += cl[pos]
pmax = max(pmax, point)
ret = max(ret, pmax)
elif k < ni * 2:
point = 0
pmax = -mod * mod
pos = i
for _ in range(k):
cc.add(pos)
pos = al[pos] - 1
point += cl[pos]
pmax = max(pmax, point)
ret = max(ret, pmax)
else:
point = pi * ((k // ni) - 1)
nk = k % (ni) + ni
pos = i
pmax = point
for _ in range(nk):
cc.add(pos)
pos = al[pos] - 1
point += cl[pos]
pmax = max(point, pmax)
ret = max(ret, pmax)
return ret
# +++++
isTest = False
def pa(v):
if isTest:
print(v)
def input_clipboard():
import clipboard
input_text = clipboard.get()
input_l = input_text.splitlines()
for l in input_l:
yield l
if __name__ == "__main__":
if sys.platform == "ios":
if input_method == input_methods[0]:
ic = input_clipboard()
input = lambda: ic.__next__()
elif input_method == input_methods[1]:
sys.stdin = open("inputFile.txt")
else:
pass
isTest = True
else:
pass
# input = sys.stdin.readline
ret = main()
if ret is not None:
print(ret)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s397326610 | Accepted | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | #!/usr/bin/env python3
import sys
def main():
# inf = float('inf') # sys.float_info.max = 1.79e+308
inf = 2**63 - 1 # (for fast JIT compile in PyPy) 9.22e+18
sys.setrecursionlimit(10**6) # 1000 -> 1000000
def input():
return sys.stdin.readline().rstrip()
def ii():
return int(input())
def isp():
return input().split()
def mi():
return map(int, input().split())
def mi_0():
return map(lambda x: int(x) - 1, input().split())
def lmi():
return list(map(int, input().split()))
def lmi_0():
return list(map(lambda x: int(x) - 1, input().split()))
def li():
return list(input())
def debug(x):
print(x, file=sys.stderr)
def calc_score(start):
order = [None] * n
accum = [None] * n
order[start] = 0
accum[start] = 0
place = start
cnt = 0
current_score = 0
M = -inf
while cnt < k:
place = P[place] # 移動
cnt += 1 # 移動回数
current_score += C[place] # 移動後の index でスコアに追加
M = max(
M, current_score
) # [スタートの次点, 現在地] まで任意の場所をゴールに選ぶときのスコア最大値
if order[place] is None:
# 未訪問
order[place] = cnt
accum[place] = current_score
else:
# 訪問ずみ (loop)
head_len = order[place]
loop_len = cnt - head_len
head_score = accum[place]
loop_gain = current_score - accum[place]
if loop_gain <= 0:
# debug('encountered loop, but non-positive loop.')
return M
else:
# debug('encountered positive loop.')
loop_times, res = divmod(k - head_len, loop_len)
# place からスタートして "0 以上" res 以下だけ進むときの
res_score = 0 # 稼ぐ最大スコア
tmp_score = 0 # 現在の累積スコア
tmp_place = place # 現在地
for _ in range(res):
tmp_place = P[tmp_place]
tmp_score += C[tmp_place] # 移動後の index でスコアに追加
res_score = max(res_score, tmp_score)
# 限界まで loop を回した上で 0 ~ res だけ貪欲に稼ぐ vs 限界 - 1 回 loop を回し確実にもう一回好きなところでとめられるようにして稼ぐ
loop_score = head_score + loop_times * loop_gain + res_score
stop_score = M + (loop_times - 1) * loop_gain
return max(loop_score, stop_score)
# debug('never encountered loop.')
return M
n, k = mi()
P = lmi_0()
C = lmi()
ans = -inf
for i in range(n):
score = calc_score(i)
# debug(f'start: {i} ans: {score}')
ans = max(ans, score)
print(ans)
if __name__ == "__main__":
main()
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s818628134 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | from copy import copy
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
itta_list = [0] * N
path_list = []
c_list = []
path = []
c = []
now = 0
while sum(itta_list) != N:
now = P[now] - 1
if itta_list[now] == 0:
path.append(now)
c.append(C[now])
itta_list[now] = 1
else:
path_list.append(path)
for i in range(len(c)):
c.append(c[i])
c_list.append(c)
path = []
c = []
now = itta_list.index(0)
path_list.append(path)
for i in range(len(c)):
c.append(c[i])
c_list.append(c)
ans = []
for i in range(len(path_list)):
path = path_list[i]
c = c_list[i]
N2 = len(path)
score = 0
K2 = copy(K)
if K > N2:
sho = K // N2
yo = K % N2
K2 = yo
if sum(c[0:N2]) > 0:
score += sum(c[0:N2]) * sho
m = -(10**9)
for k in range(K2):
for n in range(N2):
m = max(sum(c[n : n + k + 1]), m)
ans.append(m + score)
print(max(ans))
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s287831512 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | import copy
datas = [int(i) for i in input().split(" ")]
N = datas[0]
K = datas[1]
P = [int(i) for i in input().split(" ")]
C = [int(i) for i in input().split(" ")]
sumData = []
nextData = copy.deepcopy(P)
tempData = []
total = [0] * N
cnt = 0
maxData = -1000000000
while cnt < K:
cnt += 1
for i in range(N):
sumData.append(C[nextData[i] - 1])
tempData.append(nextData[nextData[i] - 1])
total[i] += sumData[i]
nextData = copy.deepcopy(tempData)
sumData = []
tempData = []
print(nextData)
print(total)
print("max:" + str(cnt) + " is " + str(max(total)))
if maxData < max(total):
maxData = max(total)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s439298663 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | def main():
INIT = (
-(10**9) - 1
) # Cから最低一つ選択する必要がある、Cの下限よりも小さい値を暫定値とする
N, K = map(int, input().split())
(*P,) = (int(x) - 1 for x in input().split())
(*C,) = map(int, input().split())
def accumulate(a):
s = 0
yield s
for x in a:
s += x
yield s
ans = INIT
grp = [-1] * N
for gid in range(N):
if ~grp[gid]:
continue
cycl = []
curr = gid
while grp[curr] == -1:
cycl.append(C[curr])
grp[curr] = gid
curr = P[curr]
score = 0
L = len(cycl)
S = sum(cycl)
if S > 0:
score += (K // L) * S
K = (K - 1) % L + 1
(*acc,) = accumulate(cycl)
partial = INIT
for left in range(L):
for right in range(left + 1, min(left + K, L) + 1):
partial = max(partial, acc[right] - acc[left])
for l_right in range(K + 1):
for r_left in range(L, L - (K - l_right) - 1, -1):
if l_right == 0 and r_left == L:
continue
partial = max(partial, acc[l_right] + acc[L] - acc[r_left])
ans = max(ans, score + partial)
# print(f'{gid=}')
# print(f'{grp=}')
# print(f'{cycl=}')
# print(f'{L=}')
# print(f'{S=}')
# print(f'{K=}')
# print(f'{acc=}')
# print(f'{score=}')
# print(f'{partial=}')
print(ans)
if __name__ == "__main__":
main()
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s082601137 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | import sys
si = sys.stdin.readline
def main():
[n, num] = [int(e) for e in si().split()]
pl = [int(e) for e in si().split()]
cl = [int(e) for e in si().split()]
M = -1000000000 * 5000 - 1
for i in range(n):
vis = [0 for _ in range(n)]
val = [0 for _ in range(n)]
flag, ith, k, idx, m, cv, cn = False, 0, num, i, 0, 0, 0
while k > 0:
idx = pl[idx] - 1
ith += 1
if not flag and vis[idx]:
flag = True
cv = m - (val[idx] - cl[idx])
cn = ith - vis[idx]
rest = k // cn
if rest:
m += cv * rest
k -= cn * rest
elif not flag:
vis[idx] = ith
val[idx] = m + cl[idx]
m += cl[idx]
M = max(M, m)
k -= 1
print(M)
if __name__ == "__main__":
main()
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s823854457 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | import sys
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
li = []
n, k = rm()
p = rl()
c = rl()
if all([x < 0 for x in c]):
print(max(c))
exit()
for idx in range(n):
if p[idx] != 0:
tmp = []
next_ = p[idx] - 1
while p[next_] != 0:
tmp.append(c[next_])
p[idx] = 0
idx = next_
next_ = p[next_] - 1
tmp.append(c[next_])
p[idx] = 0
if tmp:
li.append(tmp)
max_ = 0
for lis in li:
s = sum(lis)
lenlis = len(lis)
if k > lenlis and s > 0:
c = k % lenlis
if c == 0:
max_ = max((k // lenlis * s), max_)
else:
lis += lis[:c]
for i in range(lenlis):
max_ = max(max_, sum(lis[i : i + c]) + (k // lenlis * s))
else:
for aaa in range(k):
lis += lis[:aaa]
for i in range(lenlis):
max_ = max(max_, sum(lis[i : i + aaa]))
print(max_)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s000984588 | Runtime Error | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | n, k = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
if (sum(c) <= 0) | (k < n):
ans = 0
else:
ans = sum(c) * (k // n)
x = []
for j in range(1, n + 1):
r_c = []
for i in range(j, n + j):
i = i % n
r_c.append(c[p[i] - 1])
k = k % n
tmp = max([sum(r_c[: i + 1]) for i in range(k - 1)])
if k == n:
tmp = max(sum(r_c), tmp)
x.append(tmp)
print(ans + max(x))
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s754395323 | Runtime Error | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | # ABC175 D
import bisect
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
F = [0] * N
def op(n):
C_n = []
P_n = []
m = n
while 1:
C_n.append(C[m - 1])
P_n.append(m)
F[m - 1] = 1
m = P[m - 1]
if m == n:
break
sum_C_n = sum(C_n)
C_n = C_n[:] + C_n[:]
n = len(P_n)
res = []
for k in range(1, min(n + 1, K)):
res_k = []
for i in range(n):
res_k.append(sum(C_n[i : i + k]))
p = max(res_k)
if K - k >= n and sum_C_n >= 0:
p += sum_C_n * (K - k) // n
res.append(p)
return max(res)
res = -float("inf")
while 0 in F:
n = bisect.bisect_right(F, 0)
res = max(op(n), res)
print(res)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s211122864 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def list2d(a, b, c):
return [[c] * b for i in range(a)]
def list3d(a, b, c, d):
return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1):
return int(-(-x // y))
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**18
MOD = 10**9 + 7
Ri = lambda: [int(x) for x in sys.stdin.readline().split()]
ri = lambda: sys.stdin.readline().strip()
n, k = Ri()
a = Ri()
a = [i - 1 for i in a]
c = Ri()
lis = []
vis = [False] * n
for i in range(n):
if not vis[i]:
temp = []
nex = i
while True:
temp.append(c[nex])
vis[nex] = True
nex = a[nex]
if vis[nex]:
break
lis.append(temp)
summ = []
# print(lis)
for i in range(len(lis)):
temp = [0] * (len(lis[i]) + 1)
for j in range(len(lis[i])):
temp[j + 1] = temp[j] + lis[i][j]
summ.append(temp[:])
# print(summ)
maxxans = -INF
for i in range(len(lis)):
actualcircle = k // len(lis[i])
# print(actualcircle)
# maxxans = max(maxxans, actualcircle*summ[i][-1])
for krem in range(1, min(n - 1, k) + 1):
cnt = len(lis[i])
rem = krem % cnt
for j in range(len(lis[i])):
tot = summ[i][-1]
tempans = tot * (krem // cnt)
if j + rem - 1 >= cnt:
temp = summ[i][-1] - summ[i][j]
totrem = rem - ((cnt - 1) - j + 1)
temp += summ[i][totrem]
maxxans = max(maxxans, temp + tempans)
else:
maxxans = max(maxxans, tempans + summ[i][j + rem] - summ[i][j])
if actualcircle > 0:
for krem in range(0, k % len(lis[i]) + 1):
cnt = len(lis[i])
rem = krem % cnt
for j in range(len(lis[i])):
tot = summ[i][-1]
tempans = tot * actualcircle
if j + rem - 1 >= cnt:
temp = summ[i][-1] - summ[i][j]
totrem = rem - ((cnt - 1) - j + 1)
temp += summ[i][totrem]
maxxans = max(maxxans, temp + tempans)
else:
maxxans = max(maxxans, tempans + summ[i][j + rem] - summ[i][j])
# print(maxxans)
print(maxxans)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s938311149 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | (n, k), p, c = [[*map(int, t.split())] for t in open(0)]
a = -9e9
for i in range(n):
s = t = f = m = 0
x, *l = (i,)
while ~f:
x = p[x] - 1
l += (c[x],)
s += c[x]
m += 1
f -= x == i
for j in range(min(m, k)):
t += l[j]
a = max(a, t + (~j + k) // m * s)
print(a)
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s690106053 | Accepted | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | class UnionFind:
n = 1
par = [0]
rnk = [0]
def __init__(self, size):
self.n = size
self.par = [i for i in range(self.n)]
self.rnk = [0 for i in range(self.n)]
def find(self, vertex1):
if self.par[vertex1] == vertex1:
return vertex1
else:
self.par[vertex1] = self.find(self.par[vertex1])
return self.par[vertex1]
def unite(self, vertex1, vertex2):
vertex1 = self.find(vertex1)
vertex2 = self.find(vertex2)
if vertex1 == vertex2:
return
if self.rnk[vertex1] < self.rnk[vertex2]:
self.par[vertex1] = vertex2
else:
self.par[vertex2] = vertex1
if self.rnk[vertex1] == self.rnk[vertex2]:
self.rnk[vertex1] += 1
def same(self, vetrex1, vertex2):
return self.find(vetrex1) == self.find(vertex2)
N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
C = [int(i) for i in input().split()]
G = UnionFind(N)
for i in range(N):
G.unite(i, P[i])
D = dict()
for i in range(N):
if G.find(i) in D:
D[G.find(i)] += 1
else:
D[G.find(i)] = 1
dp = [[0 for j in range(N)] for i in range(N + 1)]
for j in range(N):
dp[0][j] = j
for i in range(N):
for j in range(N):
dp[i + 1][j] = P[dp[i][j]]
# 1 3 4 0 2
# 0->1->3->0, 2->4->2
score = [[0 for j in range(N)] for i in range(N)]
for i in range(N):
for j in range(N):
score[i][j] = score[i - 1][j] + C[dp[i + 1][j]]
def maxpoint(j, L):
if L <= N:
return max([score[i][j] for i in range(L)])
else:
roop = D[G.find(j)]
cyclepoint = score[roop - 1][j]
if cyclepoint < 0:
return maxpoint(j, roop)
else:
if L % roop != 0:
return maxpoint(j, L % roop) + (L // roop) * cyclepoint
else:
return maxpoint(j, roop) + (-1 + L // roop) * cyclepoint
ans = [maxpoint(j, K) for j in range(N)]
print(max(ans))
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the maximum possible score at the end of the game.
* * * | s491990678 | Wrong Answer | p02585 | Input is given from Standard Input in the following format:
N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N | N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
prev = [0 for i in range(N)]
for i in range(N):
prev[P[i] - 1] = i
DP = [[0 for j in range(N)] for i in range(N)]
start = [[0 for j in range(N)] for i in range(N)]
for j in range(N):
DP[0][j] = C[j]
start[0][j] = j
syuki = [N for j in range(N)]
for i in range(1, N):
for j in range(N):
DP[i][j] = DP[i - 1][prev[j]] + C[j]
start[i][j] = start[i - 1][prev[j]]
if start[i][j] == j:
syuki[j] = min(syuki[j], i)
ite = [max(DP[syuki[j] - 1][j], 0) for j in range(N)]
dist = [-10000000000000000 for j in range(N)]
for i in range(N):
if K == N:
for k in range(K):
dist[i] = max(dist[i], DP[k][i])
elif ite[i] > 0:
for k in range(1, K % syuki[i] + 1):
dist[i] = max(dist[i], K // syuki[i] * ite[i] + DP[k][i] - C[i])
else:
for k in range(K % syuki[i]):
dist[i] = max(dist[i], DP[k][i])
print(max(dist))
| Statement
Takahashi will play a game using a piece on an array of squares numbered 1, 2,
\cdots, N. Square i has an integer C_i written on it. Also, he is given a
permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.
Now, he will choose one square and place the piece on that square. Then, he
will make the following move some number of times between 1 and K (inclusive):
* In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.
Help him by finding the maximum possible score at the end of the game. (The
score is 0 at the beginning of the game.) | [{"input": "5 2\n 2 4 5 1 3\n 3 4 -10 -8 8", "output": "8\n \n\nWhen we start at some square of our choice and make at most two moves, we have\nthe following options:\n\n * If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.\n * If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.\n * If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.\n * If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.\n * If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.\n\nThe maximum score achieved is 8.\n\n* * *"}, {"input": "2 3\n 2 1\n 10 -7", "output": "13\n \n\n* * *"}, {"input": "3 3\n 3 1 2\n -1000 -2000 -3000", "output": "-1000\n \n\nWe have to make at least one move.\n\n* * *"}, {"input": "10 58\n 9 1 6 7 8 4 3 2 10 5\n 695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719", "output": "29507023469\n \n\nThe absolute value of the answer may be enormous."}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s849177221 | Accepted | p04001 | The input is given from Standard Input in the following format:
S | s = input()
m = len(s)
sm = 0
for i in range(2 ** (m - 1)):
a = list(str(bin(i)))[::-1]
b = list(s)
for j in range(len(a) - 1, -1, -1):
if a[j] == "1":
b.insert(-j - 1, "+")
sm += eval("".join(b))
print(sm)
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s146980107 | Accepted | p04001 | The input is given from Standard Input in the following format:
S | s = int(input())
sl = list(str(s))
n = len(sl)
temp = 0
for i in range(n):
for j in range(n - i):
temp += (int(sl[i]) * (10**j)) * (2 ** (i + max(0, (n - i - j - 2))))
print(temp)
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s347877556 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | S=input()
n=len(S)-1
ans=0
dfs dfs(i,f):
if i==n:
return sum(map(int,f.split("+")))
return dfs(i+1,f)+dfs(i+1,f+"+"+S[i+1])
print(def(0,S[0])
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s653716487 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | S=input()
n=len(S)-1
ans=0
def dfs(i,f):
if i==n:
return sum(map(int,f.split("+")))
return dfs(i+1,f)+dfs(i+1,f+"+"+S[i+1])
print(dfs(0,S[0])
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s733009195 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | S=input()
n=len(S)-1
ans=0
def dfs(i,f):
if i==n:
return sum(map(int,f.split("+")))
return dfs(i+1,f)+dfs(i+1,f+"+"+S[i+1])
print(def(0,S[0])
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s582113651 | Wrong Answer | p04001 | The input is given from Standard Input in the following format:
S | S = input()
N = len(S)
nums = []
for i in range(2 ** (N - 1)):
bi = bin(i)[2:].zfill(N - 1)
bi = "1" + bi + "1"
l = 0
for r in range(1, N + 1):
if bi[r] == "1":
nums.append(int(S[l:r]))
l = r
print(sum(nums))
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s960186086 | Wrong Answer | p04001 | The input is given from Standard Input in the following format:
S | S = list(map(int, input()))
s = len(S)
s = s - 1
t = pow(2, s)
l = len(list(format(t - 1, "b")))
x = 0
for i in range(t):
k = list(format(i, "b"))
L = [0] * l
x += S[-1]
v = 1
for i in range(l):
if i + 1 <= len(k):
L[l - i - 1] = k[-1 - i]
if int(L[l - i - 1]) == 0:
v = v * 10
x += v * S[len(S) - 2 - i]
else:
v = 1
x += S[len(S) - 2 - i]
print(x)
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s861226497 | Accepted | p04001 | The input is given from Standard Input in the following format:
S | N = list(input())
n = len(N) - 1
a = ["+"] * n
ev = []
for bit in range(1 << n):
for i in range(n + 1):
ev.append(N[i])
if bit & (1 << i):
ev.append(a[i])
ev.append("+")
ev = "".join(ev)[:-1]
print(eval(ev))
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s567624722 | Wrong Answer | p04001 | The input is given from Standard Input in the following format:
S | n = int(input())
print(1)
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s646977322 | Wrong Answer | p04001 | The input is given from Standard Input in the following format:
S | def total(n):
if n < 1:
return 0
return n + total(n - 1)
print(total(100))
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s355894186 | Accepted | p04001 | The input is given from Standard Input in the following format:
S | li = list(input())
lis = []
for i in range(len(li)):
lis.append(li[i])
lis.append("")
lis.pop(-1)
sum = 0
for i in range(2 ** (len(li) - 1)):
for j in range(len(li) - 1):
if (i >> j) & 1:
lis[2 * j + 1] = "+"
str = "".join(lis)
calc = 0
a = []
# 計算
a = list(str.split("+"))
for j in a:
calc += int(j)
sum += calc
# 戻す
for j in range(len(li) - 1):
lis[2 * j + 1] = ""
print(sum)
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s405848051 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | H, W, N = map(int, input().split())
mat = [[0] * W for _ in range(H)]
for _ in range(N):
a, b = map(int, input().split())
mat[a - 1][b - 1] = 1
dp = [[0] * W for _ in range(H)]
for h in range(H):
cnt = 0
if mat[h][0] == 1:
dp[h][0] = 1
for w in range(1, W):
if mat[h][w] == 1:
dp[h][w] += 1
dp[h][w] += dp[h][w - 1]
ans = [0] * 10
for i in range(10):
tmp = 0
for h in range(H - 3):
for w in range(W - 3):
s = 0
for k in range(3):
s += dp[h + k][w + 2] - dp[h + k][w]
if s == i:
tmp += 1
ans[i] = tmp
for i in ans:
print(i)
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s895114031 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | def dfs(i, f):
if i == n - 1:
return sum(list(map(int, f.split("+"))))
return dfs(i + 1, f + s[i + 1]) + dfs(i + 1, f + "+" + s[i + 1])s
s = input()
n = len(s)
print(dfs(0, s[0]))
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s165271847 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | total = 0
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s681538750 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | from sys import stdin
S = stdin.readline().rstrip()
n = len(S)
def dfs(i, f):
if i = n-1:
return sum(list(map(int, f.split('+'))))
return dfs(i+1, f + S[i + 1]) + dfs(i + 1, f + '+' s[i-1])
print(dfs(0, S[0]))
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s489860525 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | def a(t,i):
if i==l-1:
return sum(map(int,t.split('+'))))
return a(t+s[i+1],i+1)+a(t+'+'+s(i+1),i+1)
s=input()
l = len(s)
print(a(s[0],0))
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Print the sum of the evaluated value over all possible formulas.
* * * | s671611464 | Runtime Error | p04001 | The input is given from Standard Input in the following format:
S | from sys import stdin
S = stdin.readline().rstrip()
n = len(S)
def dfs(i, f):
if i = n-1:
return sum(list(map(int, f.split('+'))))
return dfs(i+1, f + S[i + 1]) + dfs(i + 1, f + '+' s[i-1])
#最初が+なし、次が+あり
print(def(0, s[0]))
| Statement
You are given a string S consisting of digits between `1` and `9`, inclusive.
You can insert the letter `+` into some of the positions (possibly none)
between two letters in this string. Here, `+` must not occur consecutively
after insertion.
All strings that can be obtained in this way can be evaluated as formulas.
Evaluate all possible formulas, and print the sum of the results. | [{"input": "125", "output": "176\n \n\nThere are 4 formulas that can be obtained: `125`, `1+25`, `12+5` and `1+2+5`.\nWhen each formula is evaluated,\n\n * 125\n * 1+25=26\n * 12+5=17\n * 1+2+5=8\n\nThus, the sum is 125+26+17+8=176.\n\n* * *"}, {"input": "9999999999", "output": "12656242944"}] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.