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 number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive).
* * * | s288012924 | Runtime Error | p03264 | Input is given from Standard Input in the following format:
K | k = int(input)
print((k // 2) ** 2 if k % 2 == 0 else ((k + 1) // 2**2) - k)
| Statement
Find the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive). The order does not
matter. | [{"input": "3", "output": "2\n \n\nTwo pairs can be chosen: (2,1) and (2,3).\n\n* * *"}, {"input": "6", "output": "9\n \n\n* * *"}, {"input": "11", "output": "30\n \n\n* * *"}, {"input": "50", "output": "625"}] |
Print the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive).
* * * | s559515640 | Runtime Error | p03264 | Input is given from Standard Input in the following format:
K | k = int(input())
print(int((k/2)**2)) if k%2 == 0 else print(int(k/2)**2 - 1/4))
| Statement
Find the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive). The order does not
matter. | [{"input": "3", "output": "2\n \n\nTwo pairs can be chosen: (2,1) and (2,3).\n\n* * *"}, {"input": "6", "output": "9\n \n\n* * *"}, {"input": "11", "output": "30\n \n\n* * *"}, {"input": "50", "output": "625"}] |
Print the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive).
* * * | s157356987 | Accepted | p03264 | Input is given from Standard Input in the following format:
K | A = int(input())
if A % 2 == 0:
print(int((A / 2) ** 2))
else:
print(int(int(A / 2) * (int(A / 2) + 1)))
| Statement
Find the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive). The order does not
matter. | [{"input": "3", "output": "2\n \n\nTwo pairs can be chosen: (2,1) and (2,3).\n\n* * *"}, {"input": "6", "output": "9\n \n\n* * *"}, {"input": "11", "output": "30\n \n\n* * *"}, {"input": "50", "output": "625"}] |
Print the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive).
* * * | s341453849 | Accepted | p03264 | Input is given from Standard Input in the following format:
K | i = int(input())
if i % 2 == 0:
answer = int(i * i / 4)
else:
answer = i // 2 * (i // 2 + 1)
print(answer)
| Statement
Find the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive). The order does not
matter. | [{"input": "3", "output": "2\n \n\nTwo pairs can be chosen: (2,1) and (2,3).\n\n* * *"}, {"input": "6", "output": "9\n \n\n* * *"}, {"input": "11", "output": "30\n \n\n* * *"}, {"input": "50", "output": "625"}] |
Print the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive).
* * * | s144270323 | Runtime Error | p03264 | Input is given from Standard Input in the following format:
K | N = int(input())
if N % 2 == 0:
print((N/2)**2)
else:
print( (N // 2)(N // 2 + 1)
| Statement
Find the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive). The order does not
matter. | [{"input": "3", "output": "2\n \n\nTwo pairs can be chosen: (2,1) and (2,3).\n\n* * *"}, {"input": "6", "output": "9\n \n\n* * *"}, {"input": "11", "output": "30\n \n\n* * *"}, {"input": "50", "output": "625"}] |
Print the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive).
* * * | s655626785 | Runtime Error | p03264 | Input is given from Standard Input in the following format:
K | x, y, z, w = map(int, input().split())
a = z - x
b = w - y
print(str(z - b) + " " + str(w + a) + " " + str(x - b) + " " + str(y + a))
| Statement
Find the number of ways to choose a pair of an even number and an odd number
from the positive integers between 1 and K (inclusive). The order does not
matter. | [{"input": "3", "output": "2\n \n\nTwo pairs can be chosen: (2,1) and (2,3).\n\n* * *"}, {"input": "6", "output": "9\n \n\n* * *"}, {"input": "11", "output": "30\n \n\n* * *"}, {"input": "50", "output": "625"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s124413456 | Accepted | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | N = int(input())
S = [[] for i in range(N)]
for i in range(N - 1):
a, b, c = map(int, input().split())
S[a - 1].append([b - 1, c])
S[b - 1].append([a - 1, c])
Q, K = map(int, input().split())
xy = list(list(map(int, input().split())) for i in range(Q))
len_ = [float("inf")] * N
len_[K - 1] = 0
queue = [K - 1]
hoge = []
while queue != []:
for i in queue:
for b, c in S[i]:
if len_[b] > len_[i] + c:
hoge.append(b)
len_[b] = len_[i] + c
queue = hoge
hoge = []
for x, y in xy:
print(len_[x - 1] + len_[y - 1])
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s605520192 | Accepted | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | from heapq import heappop, heappush
icase = 0
if icase == 0:
n = int(input())
g = [[] for i in range(n)]
for i in range(n - 1):
ai, bi, ci = map(int, input().split())
g[ai - 1].append((bi - 1, ci))
g[bi - 1].append((ai - 1, ci))
if icase == 1:
n = 5
g = [[(1, 1), (2, 1)], [(0, 1), (3, 1)], [(0, 1), (4, 1)], [(1, 1)], [(2, 1)]]
if icase == 2:
n = 7
g = [
[(1, 1), (2, 3), (3, 5), (4, 7), (5, 9), (6, 11)],
[(0, 1)],
[(0, 3)],
[(0, 5)],
[(0, 7)],
[(0, 9)],
[(0, 11)],
]
q, k = map(int, input().split())
k = k - 1
hq = []
heappush(hq, (0, k))
inf = float("INF")
dist = [inf] * n
dist[k] = 0
while len(hq) > 0:
dd, state = heappop(hq)
# print("dd:",dd,"state:",state,"q:",q)
for v, dv in g[state]:
# print("v:",v,"dv:",dv,"g[state]:",g[state],"parent[v]:",parent[v])
if dist[v] > dist[state] + dv:
dist[v] = dist[state] + dv
heappush(hq, (dist[v], v))
# print(" state:",state,"v:",v,"dist[v]",dist[v])
for i in range(q):
xi, yi = map(int, input().split())
print(dist[xi - 1] + dist[yi - 1])
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s030427272 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} |
from collections import deque
N=int(input())
T=[[] for _ in range(N)]
for i in range(N-1):
a,b,c=map(int, input().split())
T[a-1].append((b-1,c))
T[b-1].append((a-1,c))
Q,K=map(int, input().split())
que=[(K-1,0)
done=[-1]*N
while que:
t=que.pop(0)
now,nowd=t
done[now]=nowd
for nex in T[now]:
nexnode,nexd=nex
if done[nexnode]!=-1:
continue
else:
que.append((nexnode, nowd+nexd))
#print(done)
for i in range(Q):
x,y=map(int, input().split())
print(done[x-1]+done[y-1]) | Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s326365287 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | 10
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
7 8 1000000000
8 9 1000000000
9 10 1000000000
1 1
9 10 | Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s769623703 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | fron collections import deque
n=int(input())
g=[[] for i in range(n)]
for i in range(n-1):
a,b,c=map(int,input().split())
g[a-1].append([b-1,c])
g[b-1].append([a-1,c])
q,k=map(int,input().split())
qu=deque([[k-1,0]])
v=[0]*n
d=[0]*n
while len(qu)>0:
s=qu.popleft()
v[s[0]]=1
for j in g[s[0]]:
if not v[j[0]]:
v[j[0]]=1
d[j[0]]=s[1]+j[1]
qu+=[[j[0],d[j[0]]]]
for i in range(q):
x,y=map(int,input().split())
print(d[x-1]+d[y-1])
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s372820946 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | import sys
sys.setrecursionlimit(10**6);l=lambda:map(int, input().split());n=int(input()):s=[0for _ in[0]*n];j=[[]for _ in[0]*n]
for _ in range(n-1):a,b,c=l();j[a-1].append([b-1,c]);j[b-1].append([a-1,c])
def f(v,p,d):
s[v]=d
for i,c in j[v]:
if i==p:
continue
f(i,v,d+c)
q,k=l()
dfs(k-1,-1,0)
for _ in [0]*q:x,y=l();print(s[x-1]+s[y-1]) | Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s611877562 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | import heapq
n = int(f.readline())
cost = [[float("inf") for _ in range(n + 1)] for _ in range(n + 1)]
for _ in range(n - 1):
From, To, d = map(int, f.readline().split())
cost[From][To] = d
cost[To][From] = d
q, k = map(int, f.readline().split())
dist = [[float("inf") for _ in range(n + 1)] for _ in range(n + 1)]
used = [False] * (n + 1)
for i in range(n + 1):
dist[i][i] = 0
def dijkstra(s, dist):
next_v = [[0, s]]
while len(next_v):
v = next_v[0][1]
for i in range(n + 1):
if dist[s][i] > dist[s][v] + cost[v][i]:
dist[s][i] = dist[s][v] + cost[v][i]
heapq.heappush(next_v, [dist[s][i], i])
heapq.heappop(next_v)
return dist
dist = dijkstra(k, dist)
for _ in range(q):
x, y = map(int, f.readline().split())
if used[x] == False:
dist = dijkstra(x, dist)
used[x] = True
print(dist[x][k] + dist[k][y])
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s890933834 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int acs(const void *a, const void *b) {
return *(int*)a - *(int*)b;
} /* 1,2,3,4.. */
int des(const void *a, const void *b) {
return *(int*)b - *(int*)a;
} /* 8,7,6,5.. */
int cmp_char(const void *a, const void *b) {
return *(char*)a - *(char*)b;
} /* a,b,c,d.. */
int cmp_str(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
} /* aaa,aab.. */
#define min(a,b) (a < b ? a : b)
#define max(a,b) (a > b ? a : b)
#define rep(i, l, r) for (int i = l; i < r; i++)
#define MAX 100001
#define MOD 1000000007
#define INF 1000000009
typedef long long int lli;
typedef struct list {
int node;
int dist;
struct list *next;
} list;
list *graph[MAX];
void initGraph(void) {
for (int i = 0; i < MAX; i++)
graph[i] = NULL;
}
void addEdge(int node1, int node2, int dist)
{
list *new1 = (list *)malloc(sizeof(list *));
new1->node = node2;
new1->dist = dist;
new1->next = graph[node1];
graph[node1] = new1;
list *new2 = (list *)malloc(sizeof(list *));
new2->node = node1;
new2->dist = dist;
new2->next = graph[node2];
graph[node2] = new2;
}
// 頂点k(根)からの距離
int kyori[MAX];
void dfs(int n, int p) {
list *c = graph[n];
for ( ; c != NULL; c = c->next ) {
int cn = c->node;
if ( cn == p ) continue;
kyori[cn] = kyori[n] + c->dist;
dfs(cn, n);
}
}
int main(void) {
int n;
scanf("%d", &n);
initGraph();
rep(i, 0, n-1) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
addEdge(a, b, c);
}
int q, k;
scanf("%d %d", &q, &k);
int x[MAX], y[MAX];
rep(i, 0, q) scanf("%d %d", &x[i], &y[i]);
// 頂点kが根
kyori[k] = 0;
dfs(k, 0);
rep(i, 0, q) {
printf("%d\n", kyori[x[i]] + kyori[y[i]]);
}
return 0;
}
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s720701785 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | # http://wakabame.hatenablog.com/entry/2017/09/03/213522
# おまじない
import sys
input = sys.stdin.readline
# ノード数
N = int(input())
# 各要素は(元ノード, 先ノード, コスト)のタプルになっている
edges = [list(map(int, input().split())) for i in range(N)]
# クエリ数と経由ノード
Q, K = map(int, input().split())
# クエリ一覧
xy = [list(map(int, input().split())) for i in range(Q)]
# まず経由地から各ノードのへの距離をメモする
def BFS(K, edges, N):
# ノード分のリストを持ったリストの定義
# ノード番号はインデックス-1に対応.つながっている先のノードとそこへのコストのタプルが入る
roots = [ [] for i in range(N) ]
for a, b, c in edges:
roots[a-1] += [(b-1, c)]
roots[b-1] += [(a-1, c)]
# memo
dist = [-1] * N
# たどり着いたノードのリスト
stack = []
stack.append(K)
dist[K] = 0
while stack:
label = stack.pop(-1)
for i, c roots[label]:
# 初到達ならmemoしてstackに追加
# これはグラフが木構造であるから言えること.そうでなければコスト最小値しか採用しては行けない?
if dist[i] == -1:
dist[i] = dist[label] + c
stack += [i]
return dist
distance = BFS(K - 1, edges, N)
for i in range(Q):
print(distance[xy[i][0] - 1] + distance[xy[i][1] - 1])
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s779530897 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | N = int(input())
abc = [[int(n) for n in input().split()] for i in range(N - 1)]
a, b, c = list(zip(*abc))
Q, K = map(int, input().split())
xy = [[int(l) for l in input().split()] for m in range(Q)]
x, y = list(zip(*xy))
l_rin = [[] for o in range(N)]
for m in range(N - 1):
l_rin[a[m] - 1].extend([b[m]])
l_rin[b[m] - 1].extend([a[m]])
def search(top, bottom):
keiro = [[top]]
while len(keiro) > 0:
path = keiro.pop(0)
r = path[len(path) - 1]
if r == bottom:
return path
else:
for p in l_rin[r - 1]:
if p not in path:
new_path = path[:]
new_path.append(p)
keiro.append(new_path)
def distance(fst, scd):
dist = 0
ppath = search(fst, scd)
for pv in range(len(ppath) - 1):
ppath_set = set(ppath[pv : pv + 2])
for pn in range(N - 1):
ab_set = set([a[pn], b[pn]])
if ab_set == ppath_set:
dist = dist + c[pn]
exit()
return dist
for s in range(Q):
print(distance(x[s], K) + distance(K, y[s]))
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s268158289 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | import sys
from heapq import heappush, heappop, heappushpop, heapify, heapreplace
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
INF = 1 << 60
sys.setrecursionlimit(10 ** 6)
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def dijkstra(s,n,w,cost):
#始点sから各頂点への最短距離
#n:頂点数, w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
d = [float("inf")] * n
used = [False] * n
d[s] = 0
while True:
v = -1
#まだ使われてない頂点の中から最小の距離のものを探す
for i in range(n):
if (not used[i]) and (v == -1):
v = i
elif (not used[i]) and d[i] < d[v]:
v = i
if v == -1:
break
used[v] = True
for j in range(n):
d[j] = min(d[j],d[v]+cost[v][j])
return d
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())
cost = [[INF for i in range(n)] for i in range(n)]
for i in range(n - 1):
a, b, c = map(int, input().split())
cost[a - 1][b - 1] = c
cost[b - 1][a - 1] = c
q, k = map(int, input().split())
ans = [0] * q
to_k = [0] * n
to_y = [0] * n
for i in range(n):
to_k[i] = dijkstra(i, n, n - 1, cost)[k - 1]
to_y[i] = dijkstra(k - 1, n, n - 1, cost)[i]
for i in range(q):
x, y = map(int, input().split())
ans[i] = to_k[x - 1] + to_y[y - 1]
for i in ans:
print(i)
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s287143624 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | from collections import deque
def main()
n=int(input())
l=[0]*n
d={}
for i in range(n-1):
a,b,c=map(int,input().split())
a-=1
b-=1
if a in d:
d[a].add((b,c))
else:
d[a]={(b,c)}
if b in d:
d[b].add((a,c))
else:
d[b]={(a,c)}
q,k=map(int,input().split())
k-=1
p=deque([k])
while len(p):
w=p.popleft()
for i in d[w]:
if (l[i[0]]==0 and i[0]!=k) or (l[i[0]]!=0 and l[i[0]]>l[w]+i[1]):
l[i[0]]=l[w]+i[1]
p.append(i[0])
for i in range(q):
x,y=map(int,input().split())
print(l[x-1]+l[y-1])
if __name__ == '__main__':
main()
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s083029653 | Accepted | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | n = int(input())
a = [[int(i) for i in input().split()] for i in range(n - 1)]
q, k = (int(i) for i in input().split())
question = [[int(i) for i in input().split()] for i in range(q)]
x, ans = [[] for i in range(n)], [False for i in range(n)]
for i in a:
x[i[0] - 1].append([i[1], i[2]])
x[i[1] - 1].append([i[0], i[2]])
visit = [False for i in range(n)]
visit[k - 1], now = True, [k]
while now:
p = now.pop()
for i in x[p - 1]:
if not visit[i[0] - 1]:
visit[i[0] - 1], ans[i[0] - 1] = True, ans[p - 1] + i[1]
now.append(i[0])
for i in question:
print(ans[i[0] - 1] + ans[i[1] - 1])
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.
* * * | s877497284 | Runtime Error | p03634 | Input is given from Standard Input in the following format:
N
a_1 b_1 c_1
:
a_{N-1} b_{N-1} c_{N-1}
Q K
x_1 y_1
:
x_{Q} y_{Q} | INF = 1000000000000000
V = int(input())
E = V - 1
graph = {x: [] for x in range(V)}
cost = {}
flag = [0 for i in range(V)]
d = [0 for i in range(V)]
# ****#
def rootLen(v, p, t):
d[v] = t # !
flag[v] = 1
for i in range(V):
if flag[i] == 0 and i in graph[v]:
rootLen(i, v, t + cost[(i, v)])
else:
continue
return
if __name__ == "__main__":
for i in range(E):
nums = [int(x) for x in input().split()]
a, b, c = nums[0] - 1, nums[1] - 1, nums[2]
graph[a].append(b)
graph[b].append(a)
cost[(a, b)] = c
cost[(b, a)] = c
nums = [int(x) for x in input().split()]
Q, K = nums[0], nums[1] - 1
rootLen(K, -1, 0)
for i in range(Q):
nums = [int(x) for x in input().split()]
x, y = nums[0] - 1, nums[1] - 1
print(d[x] + d[y])
| Statement
You are given a tree with N vertices.
Here, a _tree_ is a kind of graph, and more specifically, a connected
undirected graph with N-1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of
c_i.
You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):
* find the length of the shortest path from Vertex x_j and Vertex y_j via Vertex K. | [{"input": "5\n 1 2 1\n 1 3 1\n 2 4 1\n 3 5 1\n 3 1\n 2 4\n 2 3\n 4 5", "output": "3\n 2\n 4\n \n\nThe shortest paths for the three queries are as follows:\n\n * Query 1: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 2 \u2192 Vertex 4 : Length 1+1+1=3\n * Query 2: Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 : Length 1+1=2\n * Query 3: Vertex 4 \u2192 Vertex 2 \u2192 Vertex 1 \u2192 Vertex 3 \u2192 Vertex 5 : Length 1+1+1+1=4\n\n* * *"}, {"input": "7\n 1 2 1\n 1 3 3\n 1 4 5\n 1 5 7\n 1 6 9\n 1 7 11\n 3 2\n 1 3\n 4 5\n 6 7", "output": "5\n 14\n 22\n \n\nThe path for each query must pass Vertex K=2.\n\n* * *"}, {"input": "10\n 1 2 1000000000\n 2 3 1000000000\n 3 4 1000000000\n 4 5 1000000000\n 5 6 1000000000\n 6 7 1000000000\n 7 8 1000000000\n 8 9 1000000000\n 9 10 1000000000\n 1 1\n 9 10", "output": "17000000000"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s976052196 | Accepted | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | def solve():
from math import atan2, degrees, hypot
n = int(input())
txy = []
sx, sy = 0, 0
for i in range(n):
a, b = map(int, input().split())
theta_0 = degrees(atan2(b, a))
sx += a
sy += b
txy.append([theta_0, a, b])
txy.sort()
ans = hypot(sx, sy)
for i in range(n):
for j in range(i + 1, n):
tx, ty = 0, 0
for k in range(i, j):
theta, x, y = txy[k]
tx += x
ty += y
ans = max(ans, hypot(tx, ty))
ans = max(ans, hypot(sx - tx, sy - ty))
print(ans)
solve()
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s168752993 | Wrong Answer | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | N = int(input())
nums = [[0, 0] for _ in range(N)]
sum_x = 0
sum_y = 0
for i in range(N):
x, y = map(int, input().split())
nums[i][0], nums[i][1] = x, y
sum_x += x
sum_y += y
# ans = sum_x**2+sum_y**2
unused = {}
ans_x = sum_x
ans_y = sum_y
for i in range(N):
# temp_x = ans_x
# temp_y = ans_y
pos = -1
diff = 0
for j in range(N):
if not j in unused:
if (ans_x - nums[j][0]) ** 2 + (ans_y - nums[j][1]) ** 2 - (
ans_x**2 + ans_y**2
) > diff:
pos = j
diff = (
ans_x**2
+ ans_y**2
- (ans_x - nums[j][0]) ** 2
+ (ans_y - nums[j][1]) ** 2
)
if pos == -1:
print((ans_x**2 + ans_y**2) ** 0.5)
exit()
else:
ans_x -= nums[pos][0]
ans_y -= nums[pos][1]
unused[pos] = 1
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s989607918 | Wrong Answer | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | def dist0(x, y):
return (x * x + y * y) ** 0.5
N = int(input())
vA = [
(0, 0),
]
for _ in range(N):
vA.append(tuple(map(int, input().split())))
# dp = [[0.0, (0,0)]*(N+1) for _ in range(N+1)]
dp = [[] for _ in range(N + 1)]
dp[0].append([0.0, (0, 0)])
for i in range(1, N + 1):
X, Y = vA[i]
dp[i].append([0.0, (0, 0)])
for pD, (pX, pY) in dp[i - 1]:
newDist = dist0(pX + X, pY + Y)
if newDist > pD:
dp[i].append([newDist, (pX + X, pY + Y)])
else:
dp[i].append([pD, (X, Y)])
res = max(t[0] for t in dp[N])
print("{:.24f}".format(res))
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s795750042 | Wrong Answer | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | n = int(input())
d = list()
mx = -1
for i in range(0, n):
x, y = list(map(int, input().split()))
d.append((x, y))
for i in range(0, n):
x = d[i][0]
y = d[i][1]
ans = pow(x * x + y * y, 0.5)
for j in range(0, n):
if i != j:
g = pow((x + d[j][0]) * (x + d[j][0]) + (y + d[j][1]) * (y + d[j][1]), 0.5)
if g > ans:
ans = g
x = x + d[j][0]
y = y + d[j][1]
mx = max(mx, ans)
print(mx)
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s623082876 | Accepted | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | from math import hypot
N = int(input())
engines = [tuple(map(int, input().split())) for _ in range(N)]
def dot(x1, y1, x2, y2):
return x1 * x2 + y1 * y2
def cross(x1, y1, x2, y2):
return x1 * y2 - y1 * x2
def getDistMax(xBase, yBase):
x1s, y1s, x2s, y2s = [], [], [], []
x, y = 0, 0
for dx, dy in engines:
d = dot(xBase, yBase, dx, dy)
if d > 0:
x, y = x + dx, y + dy
elif d == 0:
c = cross(xBase, yBase, dx, dy)
if c > 0:
x1s.append(dx)
y1s.append(dy)
else:
x2s.append(dx)
y2s.append(dy)
ans = max(
hypot(x, y),
hypot(x + sum(x1s), y + sum(y1s)),
hypot(x + sum(x2s), y + sum(y2s)),
)
return ans
ans = 0
for x, y in engines:
ds = []
ds.append(getDistMax(x, y))
ds.append(getDistMax(-y, x))
ds.append(getDistMax(-x, -y))
ds.append(getDistMax(y, -x))
ans = max(ans, max(ds))
print(ans)
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s433156988 | Runtime Error | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | N = input()
engine = [input().split() for i in N]
from multiprocessing import Pool
p = Pool()
import math
def sqrt(en):
s = math.sqrt(en[0] ** 2 + en[1] ** 2)
if s < 0:
s = 0
return s
s = p.map(sqrt, engine)
s.sort()
ss = [i for i in s if i > 0]
sum(ss)
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s495922984 | Wrong Answer | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | N = int(input())
A = [list(map(int, input().split())) for a in range(N)]
lmax = 0
t = [0, 0]
for a in A:
if a[0] >= 0 and a[1] >= 0:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[0] <= 0 and a[1] >= 0:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[0] >= 0 and a[1] <= 0:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[0] <= 0 and a[1] <= 0:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[0] <= 0:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[0] >= 0:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[1] <= 0:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[1] >= 0:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[1] >= a[0]:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[0] >= a[1]:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[1] >= -a[0]:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if a[0] >= -a[1]:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if -a[1] >= a[0]:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
t = [0, 0]
for a in A:
if -a[0] >= a[1]:
t[0] += a[0]
t[1] += a[1]
lmax = max(lmax, (t[0] ** 2 + t[1] ** 2) ** 0.5)
print(lmax)
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s614170801 | Wrong Answer | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | N = int(input())
XY = [list(map(int, input().split())) for i in range(N)]
a = [[0, 0] for i in range(4)]
xy = [[] for i in range(4)]
for x, y in XY:
if x <= 0:
if y <= 0:
a[2][0] += x
a[2][1] += y
xy[2].append((x, y))
if y >= 0:
a[3][0] += x
a[3][1] += y
xy[3].append((x, y))
if x >= 0:
if y <= 0:
a[1][0] += x
a[1][1] += y
xy[1].append((x, y))
if y >= 0:
a[0][0] += x
a[0][1] += y
xy[0].append((x, y))
rx, ry = 0, 0
def j(x, y, i):
if i == 0:
return 1 if x >= 0 and y >= 0 else 0
if i == 1:
return 1 if x <= 0 and y >= 0 else 0
if i == 2:
return 1 if x <= 0 and y <= 0 else 0
if i == 3:
return 1 if x >= 0 and y <= 0 else 0
from collections import deque
for i in range(4):
q = deque()
for x, y in xy[i - 1] + xy[i - 3]:
if j(x, y, i) == 0:
q.append((x, y))
c = 0
while len(q) > 0 and c < N:
x, y = q.popleft()
if (
j(a[i][0] + x, a[i][1] + y, i)
and (a[i][0] + x) ** 2 + (a[i][1] + y) ** 2 > a[i][0] ** 2 + a[i][1] ** 2
):
a[i][0] += x
a[i][1] += y
c = 0
else:
q.append((x, y))
c += 1
if rx**2 + ry**2 < a[i][0] ** 2 + a[i][1] ** 2:
rx, ry = a[i]
print((rx**2 + ry**2) ** 0.5)
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s763617010 | Wrong Answer | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
px = 0
py = 0
for i in range(0, n):
if a[i][0] >= 0 and a[i][1] >= 0:
px += a[i][0]
py += a[i][1]
p = (px**2 + py**2) ** 0.5
qx = 0
qy = 0
for i in range(0, n):
if a[i][0] >= 0 and a[i][1] <= 0:
px += a[i][0]
py += a[i][1]
q = (qx**2 + qy**2) ** 0.5
rx = 0
ry = 0
for i in range(0, n):
if a[i][0] <= 0 and a[i][1] >= 0:
rx += a[i][0]
ry += a[i][1]
r = (rx**2 + ry**2) ** 0.5
sx = 0
sy = 0
for i in range(0, n):
if a[i][0] <= 0 and a[i][1] <= 0:
sx += a[i][0]
sy += a[i][1]
s = (sx**2 + sy**2) ** 0.5
print(max(p, q, r, s))
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s632496504 | Runtime Error | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | from cp_solver import solve
from atcoder_tasks import tasks
solve(tasks("abc139_f"))
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s704608791 | Wrong Answer | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | N = int(input())
P = sorted(
[tuple(map(int, input().split())) for _ in range(N)],
key=lambda x: x[0] ** 2 + x[1] ** 2,
)[::-1]
ans = 0
for i in range(N):
x, y = P[i]
now = x**2 + y**2
for j in range(i + 1, N):
tx, ty = P[j]
tmp = (x + tx) ** 2 + (y + ty) ** 2
if tmp > now:
now = tmp
x += tx
y += ty
ans = max(ans, now)
print(ans**0.5)
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the
true answer is at most 10^{-10}.
* * * | s582282152 | Wrong Answer | p02926 | Input is given from Standard Input in the following format:
N
x_1 y_1
x_2 y_2
: :
x_N y_N | import math
n = int(input())
src = [list(map(int, input().split())) for _ in range(n)]
a = []
b = []
c = []
d = []
for s in src:
x = s[0]
y = s[1]
if x != 0 or y != 0:
if x >= 0 and y >= 0:
a.append(s)
if x > 0 and y < 0:
b.append(s)
if x < 0 and y > 0:
c.append(s)
if x <= 0 and y <= 0:
d.append(s)
abcd = a + b + c + d
bcd = b + c + d
acd = a + c + d
abd = a + b + d
abc = a + b + c
ab = a + b
ac = a + c
bd = b + d
cd = c + d
ad = a + d
bc = b + c
def kyori(list):
if len(list) == 0:
return 0
xsum = 0
ysum = 0
for l in list:
xsum += l[0]
ysum += l[1]
return math.sqrt(xsum**2 + ysum**2)
print(
max(
kyori(abcd),
kyori(bcd),
kyori(acd),
kyori(abd),
kyori(abc),
kyori(ab),
kyori(ac),
kyori(bd),
kyori(cd),
kyori(ad),
kyori(bc),
kyori(a),
kyori(b),
kyori(c),
kyori(d),
)
)
| Statement
E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.
He has N engines, which can be used as follows:
* When E869120 uses the i-th engine, his X\- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).
* E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.
He wants to go as far as possible from the origin. Let (X, Y) be his final
coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance
from the origin. | [{"input": "3\n 0 10\n 5 -5\n -5 -5", "output": "10.000000000000000000000000000000000000000000000000\n \n\nThe final distance from the origin can be 10 if we use the engines in one of\nthe following three ways:\n\n * Use Engine 1 to move to (0, 10).\n * Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).\n * Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).\n\nThe distance cannot be greater than 10, so the maximum possible distance is\n10.\n\n* * *"}, {"input": "5\n 1 1\n 1 0\n 0 1\n -1 0\n 0 -1", "output": "2.828427124746190097603377448419396157139343750753\n \n\nThe maximum possible final distance is 2 \\sqrt{2} = 2.82842.... One of the\nways to achieve it is:\n\n * Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).\n\n* * *"}, {"input": "5\n 1 1\n 2 2\n 3 3\n 4 4\n 5 5", "output": "21.213203435596425732025330863145471178545078130654\n \n\nIf we use all the engines in the order 1 \\rightarrow 2 \\rightarrow 3\n\\rightarrow 4 \\rightarrow 5, we will end up at (15, 15), with the distance 15\n\\sqrt{2} = 21.2132... from the origin.\n\n* * *"}, {"input": "3\n 0 0\n 0 1\n 1 0", "output": "1.414213562373095048801688724209698078569671875376\n \n\nThere can be useless engines with (x_i, y_i) = (0, 0).\n\n* * *"}, {"input": "1\n 90447 91000", "output": "128303.000000000000000000000000000000000000000000000000\n \n\nNote that there can be only one engine.\n\n* * *"}, {"input": "2\n 96000 -72000\n -72000 54000", "output": "120000.000000000000000000000000000000000000000000000000\n \n\nThere can be only two engines, too.\n\n* * *"}, {"input": "10\n 1 2\n 3 4\n 5 6\n 7 8\n 9 10\n 11 12\n 13 14\n 15 16\n 17 18\n 19 20", "output": "148.660687473185055226120082139313966514489855137208"}] |
Print the answer.
* * * | s750721414 | Accepted | p03321 | Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M | import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(0, -1), (1, 0), (0, 1), (-1, 0)]
ddn = [(0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, -1), (-1, 0), (-1, 1)]
def LI():
return [int(x) for x in sys.stdin.readline().split()]
def LI_():
return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF():
return [float(x) for x in sys.stdin.readline().split()]
def LS():
return sys.stdin.readline().split()
def I():
return int(sys.stdin.readline())
def F():
return float(sys.stdin.readline())
def S():
return input()
def pf(s):
return print(s, flush=True)
def main():
n, m = LI()
a = [LI_() for _ in range(m)]
ss = [set() for _ in range(n)]
for b, c in a:
ss[b].add(c)
ss[c].add(b)
ts = [set(list(range(n))) - set([_]) for _ in range(n)]
for i in range(n):
ts[i] -= ss[i]
d = []
u = set()
for i in range(n):
if i in u:
continue
ab = set([i])
nb = set([i])
ac = set()
nc = set()
f = True
while f:
f = False
k = set()
nc = set()
for j in nb:
nc |= ts[j]
nc -= ac
ac |= nc
nb = set()
for j in nc:
nb |= ts[j]
nb -= ab
ab |= nb
if nb:
f = True
if ab & ac:
return -1
d.append((len(ab), len(ac)))
u |= ab
u |= ac
r = set([0])
for b, c in d:
t = set()
for k in r:
t.add(k + b)
t.add(k + c)
r = t
rr = inf
for t in r:
nt = n - t
if t == 0 or nt == 0:
continue
tr = t * (t - 1) // 2
tr += nt * (nt - 1) // 2
if rr > tr:
rr = tr
return rr
print(main())
| Statement
In the State of Takahashi in AtCoderian Federation, there are N cities,
numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th
road connects City A_i and City B_i. Every road connects two distinct cities.
Also, for any two cities, there is at most one road that **directly** connects
them.
One day, it was decided that the State of Takahashi would be divided into two
states, Taka and Hashi. After the division, each city in Takahashi would
belong to either Taka or Hashi. It is acceptable for all the cities to belong
Taka, or for all the cities to belong Hashi. Here, the following condition
should be satisfied:
* Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the
same state. If it is impossible to divide the cities into Taka and Hashi so
that the condition is satisfied, print `-1`. | [{"input": "5 5\n 1 2\n 1 3\n 3 4\n 3 5\n 4 5", "output": "4\n \n\nFor example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong\nto Hashi, the condition is satisfied. Here, the number of roads whose endpoint\ncities belong to the same state, is 4.\n\n* * *"}, {"input": "5 1\n 1 2", "output": "-1\n \n\nIn this sample, the condition cannot be satisfied regardless of which cities\nbelong to each state.\n\n* * *"}, {"input": "4 3\n 1 2\n 1 3\n 2 3", "output": "3\n \n\n* * *"}, {"input": "10 39\n 7 2\n 7 1\n 5 6\n 5 8\n 9 10\n 2 8\n 8 7\n 3 10\n 10 1\n 8 10\n 2 3\n 7 4\n 3 9\n 4 10\n 3 4\n 6 1\n 6 7\n 9 5\n 9 7\n 6 9\n 9 4\n 4 6\n 7 5\n 8 3\n 2 5\n 9 2\n 10 7\n 8 6\n 8 9\n 7 3\n 5 3\n 4 5\n 6 3\n 2 10\n 5 10\n 4 2\n 6 2\n 8 4\n 10 6", "output": "21"}] |
Print the answer.
* * * | s451397222 | Wrong Answer | p03321 | Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M | n, m = map(int, input().split())
adjList = [[0] * n for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
adjList[a][b] = 1
adjList[b][a] = 1
def applyComp(a):
for i in range(len(a)):
for j in range(len(a[i])):
a[i][j] = 1 - a[i][j]
applyComp(adjList)
visited = [0] * n
def dfs(u, parent=-1, color=1):
# print(u, color)
if visited[u]:
if visited[u] != color:
return None
else:
return 0, 0
visited[u] = color
oneCount = 0
twoCount = 0
if color == 1:
oneCount += 1
else:
twoCount += 1
for v in range(n):
if u == v or parent == v:
continue
if adjList[u][v]:
p = dfs(v, u, 3 - color)
if p is None:
return None
oneCount += p[0]
twoCount += p[1]
return oneCount, twoCount
connSize = []
for i in range(n):
if visited[i]:
continue
p = dfs(i)
if p is None:
print(-1)
exit()
connSize.append(p)
dp = [False] * (n + 1)
dp[0] = True
for c1, c2 in connSize:
for i in range(n, -1, -1):
if dp[i]:
dp[i + c1] = True
dp[i + c2] = True
sz1 = n // 2
while dp[sz1] == False:
sz1 -= 1
sz2 = n - sz1
print(sum(sz * (sz - 1) // 2 for sz in [sz1, sz2]))
| Statement
In the State of Takahashi in AtCoderian Federation, there are N cities,
numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th
road connects City A_i and City B_i. Every road connects two distinct cities.
Also, for any two cities, there is at most one road that **directly** connects
them.
One day, it was decided that the State of Takahashi would be divided into two
states, Taka and Hashi. After the division, each city in Takahashi would
belong to either Taka or Hashi. It is acceptable for all the cities to belong
Taka, or for all the cities to belong Hashi. Here, the following condition
should be satisfied:
* Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the
same state. If it is impossible to divide the cities into Taka and Hashi so
that the condition is satisfied, print `-1`. | [{"input": "5 5\n 1 2\n 1 3\n 3 4\n 3 5\n 4 5", "output": "4\n \n\nFor example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong\nto Hashi, the condition is satisfied. Here, the number of roads whose endpoint\ncities belong to the same state, is 4.\n\n* * *"}, {"input": "5 1\n 1 2", "output": "-1\n \n\nIn this sample, the condition cannot be satisfied regardless of which cities\nbelong to each state.\n\n* * *"}, {"input": "4 3\n 1 2\n 1 3\n 2 3", "output": "3\n \n\n* * *"}, {"input": "10 39\n 7 2\n 7 1\n 5 6\n 5 8\n 9 10\n 2 8\n 8 7\n 3 10\n 10 1\n 8 10\n 2 3\n 7 4\n 3 9\n 4 10\n 3 4\n 6 1\n 6 7\n 9 5\n 9 7\n 6 9\n 9 4\n 4 6\n 7 5\n 8 3\n 2 5\n 9 2\n 10 7\n 8 6\n 8 9\n 7 3\n 5 3\n 4 5\n 6 3\n 2 10\n 5 10\n 4 2\n 6 2\n 8 4\n 10 6", "output": "21"}] |
Print the answer.
* * * | s649700234 | Runtime Error | p03321 | Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M | k = int(input())
if k < 10:
print("\n".join(list(map(str, range(1, k + 1)))))
else:
cnt = 0
for i in range(k):
if i % 9 == 0 and i > 0:
cnt += 1
num = int(str(i % 9 + 1) + "9" * cnt)
print(num)
| Statement
In the State of Takahashi in AtCoderian Federation, there are N cities,
numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th
road connects City A_i and City B_i. Every road connects two distinct cities.
Also, for any two cities, there is at most one road that **directly** connects
them.
One day, it was decided that the State of Takahashi would be divided into two
states, Taka and Hashi. After the division, each city in Takahashi would
belong to either Taka or Hashi. It is acceptable for all the cities to belong
Taka, or for all the cities to belong Hashi. Here, the following condition
should be satisfied:
* Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the
same state. If it is impossible to divide the cities into Taka and Hashi so
that the condition is satisfied, print `-1`. | [{"input": "5 5\n 1 2\n 1 3\n 3 4\n 3 5\n 4 5", "output": "4\n \n\nFor example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong\nto Hashi, the condition is satisfied. Here, the number of roads whose endpoint\ncities belong to the same state, is 4.\n\n* * *"}, {"input": "5 1\n 1 2", "output": "-1\n \n\nIn this sample, the condition cannot be satisfied regardless of which cities\nbelong to each state.\n\n* * *"}, {"input": "4 3\n 1 2\n 1 3\n 2 3", "output": "3\n \n\n* * *"}, {"input": "10 39\n 7 2\n 7 1\n 5 6\n 5 8\n 9 10\n 2 8\n 8 7\n 3 10\n 10 1\n 8 10\n 2 3\n 7 4\n 3 9\n 4 10\n 3 4\n 6 1\n 6 7\n 9 5\n 9 7\n 6 9\n 9 4\n 4 6\n 7 5\n 8 3\n 2 5\n 9 2\n 10 7\n 8 6\n 8 9\n 7 3\n 5 3\n 4 5\n 6 3\n 2 10\n 5 10\n 4 2\n 6 2\n 8 4\n 10 6", "output": "21"}] |
Print the answer.
* * * | s266806277 | Runtime Error | p03321 | Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
template<class T> void chmin(T& a,const T& b){if(a>b)a=b;}
template<class T> void chmax(T& a,const T& b){if(a<b)a=b;}
int e[705][705];
vector<int> g[705];
int col[705];
int cnt[3];
int used[705];
int dp[705][705];
bool binary_graph(int v,int c){
col[v]=c;
cnt[c+1]++;
used[v]=true;
bool ret = true;
for(int i=0;i<g[v].size();i++){
int u = g[v][i];
if(used[u]){
if(col[u]==c){
ret=false;
}
}else{
if(!binary_graph(u,-c))ret=false;
}
}
return ret;
}
int N,M;
int main(){
cin >> N >> M;
for(int i=0;i<N;i++){
e[i][i]=1;
}
for(int i=0;i<M;i++){
int A,B;
cin >> A >> B;
A--;B--;
e[A][B]=e[B][A]=1;
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(e[i][j]==0){
g[i].pb(j);
}
}
}
bool res = true;
vector<int> vec;
for(int i=0;i<N;i++){
if(!used[i]){
cnt[0]=cnt[2]=0;
res &= binary_graph(i,1);
//cout << res << endl;
//cout << cnt[0] << ' ' << cnt[2] << endl;
if(res)vec.pb(abs(cnt[0]-cnt[2]));
}
}
if(!res){
cout << -1 << endl;
return 0;
}
dp[0][0]=1;
for(int i=0;i<vec.size();i++){
for(int j=0;j<=700;j++){
if(dp[i][j]==0)continue;
if(j-vec[i]>=0)dp[i+1][j-vec[i]]=1;
dp[i+1][j+vec[i]]=1;
}
}
int K = -1;
for(int i=0;i<=700;i++){
if(dp[vec.size()][i]==1){
K=i;
break;
}
}
//cout << K << endl;
int C = (N+K)/2,D = (N-K)/2;
cout << C*(C-1)/2+D*(D-1)/2 << endl;
return 0;
} | Statement
In the State of Takahashi in AtCoderian Federation, there are N cities,
numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th
road connects City A_i and City B_i. Every road connects two distinct cities.
Also, for any two cities, there is at most one road that **directly** connects
them.
One day, it was decided that the State of Takahashi would be divided into two
states, Taka and Hashi. After the division, each city in Takahashi would
belong to either Taka or Hashi. It is acceptable for all the cities to belong
Taka, or for all the cities to belong Hashi. Here, the following condition
should be satisfied:
* Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the
same state. If it is impossible to divide the cities into Taka and Hashi so
that the condition is satisfied, print `-1`. | [{"input": "5 5\n 1 2\n 1 3\n 3 4\n 3 5\n 4 5", "output": "4\n \n\nFor example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong\nto Hashi, the condition is satisfied. Here, the number of roads whose endpoint\ncities belong to the same state, is 4.\n\n* * *"}, {"input": "5 1\n 1 2", "output": "-1\n \n\nIn this sample, the condition cannot be satisfied regardless of which cities\nbelong to each state.\n\n* * *"}, {"input": "4 3\n 1 2\n 1 3\n 2 3", "output": "3\n \n\n* * *"}, {"input": "10 39\n 7 2\n 7 1\n 5 6\n 5 8\n 9 10\n 2 8\n 8 7\n 3 10\n 10 1\n 8 10\n 2 3\n 7 4\n 3 9\n 4 10\n 3 4\n 6 1\n 6 7\n 9 5\n 9 7\n 6 9\n 9 4\n 4 6\n 7 5\n 8 3\n 2 5\n 9 2\n 10 7\n 8 6\n 8 9\n 7 3\n 5 3\n 4 5\n 6 3\n 2 10\n 5 10\n 4 2\n 6 2\n 8 4\n 10 6", "output": "21"}] |
Print the answer.
* * * | s286438238 | Accepted | p03321 | Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M | from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect
def read():
return int(input())
def reads():
return [int(x) for x in input().split()]
N, M = reads()
orig_edges = set()
for _ in range(M):
A, B = reads()
A, B = A - 1, B - 1
orig_edges.add((A, B))
orig_edges.add((B, A))
edges = [[] for _ in range(N)]
for A, B in combinations(range(N), 2):
if (A, B) not in orig_edges:
edges[A].append(B)
edges[B].append(A)
remain = set(range(N))
color = [-1] * N
def walk(u, c, ctr, remain):
if u in remain:
remain.remove(u)
if color[u] >= 0:
if color[u] != c:
print(-1)
exit()
return ctr
color[u] = c
ctr[c] += 1
for v in edges[u]:
walk(v, 1 - c, ctr, remain)
return ctr
ps = []
while len(remain) > 0:
s = remain.pop()
p = walk(s, 0, [0, 0], remain)
ps.append(p)
dp = set([0])
for a, b in ps:
d = b - a
dp = {x - d for x in dp} | {x + d for x in dp}
m = min(abs(x) for x in dp)
a, b = (N + m) // 2, (N - m) // 2
print(a * (a - 1) // 2 + b * (b - 1) // 2)
| Statement
In the State of Takahashi in AtCoderian Federation, there are N cities,
numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th
road connects City A_i and City B_i. Every road connects two distinct cities.
Also, for any two cities, there is at most one road that **directly** connects
them.
One day, it was decided that the State of Takahashi would be divided into two
states, Taka and Hashi. After the division, each city in Takahashi would
belong to either Taka or Hashi. It is acceptable for all the cities to belong
Taka, or for all the cities to belong Hashi. Here, the following condition
should be satisfied:
* Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the
same state. If it is impossible to divide the cities into Taka and Hashi so
that the condition is satisfied, print `-1`. | [{"input": "5 5\n 1 2\n 1 3\n 3 4\n 3 5\n 4 5", "output": "4\n \n\nFor example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong\nto Hashi, the condition is satisfied. Here, the number of roads whose endpoint\ncities belong to the same state, is 4.\n\n* * *"}, {"input": "5 1\n 1 2", "output": "-1\n \n\nIn this sample, the condition cannot be satisfied regardless of which cities\nbelong to each state.\n\n* * *"}, {"input": "4 3\n 1 2\n 1 3\n 2 3", "output": "3\n \n\n* * *"}, {"input": "10 39\n 7 2\n 7 1\n 5 6\n 5 8\n 9 10\n 2 8\n 8 7\n 3 10\n 10 1\n 8 10\n 2 3\n 7 4\n 3 9\n 4 10\n 3 4\n 6 1\n 6 7\n 9 5\n 9 7\n 6 9\n 9 4\n 4 6\n 7 5\n 8 3\n 2 5\n 9 2\n 10 7\n 8 6\n 8 9\n 7 3\n 5 3\n 4 5\n 6 3\n 2 10\n 5 10\n 4 2\n 6 2\n 8 4\n 10 6", "output": "21"}] |
Print the answer.
* * * | s893379435 | Wrong Answer | p03321 | Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M | from itertools import product
def dfs(links, fixed, s):
q = [(s, 0)]
cnt = [0, 0]
while q:
v, c = q.pop()
if fixed[v] > -1:
if fixed[v] != c:
return False
continue
fixed[v] = c
cnt[c] += 1
for u in links[v]:
q.append((u, c ^ 1))
return cnt
def is_biparate(n, links):
fixed = [-1] * n
can = []
for i in range(n):
if fixed[i] > -1:
continue
cnt = dfs(links, fixed, i)
if cnt == False:
return False
can.append(cnt)
mx = float("inf")
for p in product(*can):
s = sum(p)
t = n - s
tmx = max(s, t)
mx = min(mx, tmx)
mn = n - mx
return (mx * (mx - 1) + mn * (mn - 1)) // 2
n, m = map(int, input().split())
links = [set(range(n)) - {i} for i in range(n)]
for _ in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
links[a].remove(b)
links[b].remove(a)
print(is_biparate(n, links))
| Statement
In the State of Takahashi in AtCoderian Federation, there are N cities,
numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th
road connects City A_i and City B_i. Every road connects two distinct cities.
Also, for any two cities, there is at most one road that **directly** connects
them.
One day, it was decided that the State of Takahashi would be divided into two
states, Taka and Hashi. After the division, each city in Takahashi would
belong to either Taka or Hashi. It is acceptable for all the cities to belong
Taka, or for all the cities to belong Hashi. Here, the following condition
should be satisfied:
* Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the
same state. If it is impossible to divide the cities into Taka and Hashi so
that the condition is satisfied, print `-1`. | [{"input": "5 5\n 1 2\n 1 3\n 3 4\n 3 5\n 4 5", "output": "4\n \n\nFor example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong\nto Hashi, the condition is satisfied. Here, the number of roads whose endpoint\ncities belong to the same state, is 4.\n\n* * *"}, {"input": "5 1\n 1 2", "output": "-1\n \n\nIn this sample, the condition cannot be satisfied regardless of which cities\nbelong to each state.\n\n* * *"}, {"input": "4 3\n 1 2\n 1 3\n 2 3", "output": "3\n \n\n* * *"}, {"input": "10 39\n 7 2\n 7 1\n 5 6\n 5 8\n 9 10\n 2 8\n 8 7\n 3 10\n 10 1\n 8 10\n 2 3\n 7 4\n 3 9\n 4 10\n 3 4\n 6 1\n 6 7\n 9 5\n 9 7\n 6 9\n 9 4\n 4 6\n 7 5\n 8 3\n 2 5\n 9 2\n 10 7\n 8 6\n 8 9\n 7 3\n 5 3\n 4 5\n 6 3\n 2 10\n 5 10\n 4 2\n 6 2\n 8 4\n 10 6", "output": "21"}] |
Print the answer.
* * * | s623237659 | Accepted | p03321 | Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M | N, M = map(int, input().split())
E = [[] for _ in range(N + 1)]
E_mat = [[0] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
a, b = map(int, input().split())
E[a].append(b)
E[b].append(a)
E_mat[a][b] = 1
E_mat[b][a] = 1
E_inv = [[] for _ in range(N + 1)]
for v, e_mat in enumerate(E_mat[1:], 1):
for u, e in enumerate(e_mat[1:], 1):
if e == 0 and v != u:
E_inv[v].append(u)
colors = [0] * (N + 1)
def dfs(v, c):
colors[v] = c
cnts[c] += 1
for u in E_inv[v]:
if colors[u] == c:
print(-1)
exit()
if colors[u] != 0:
continue
dfs(u, -c)
K = []
for v in range(1, N + 1):
cnts = [0, 0, 0]
if colors[v] == 0:
dfs(v, 1)
K.append(cnts[1:])
dp = 1
for a, b in K:
dp = dp << a | dp << b
dp = bin(dp)[:1:-1]
ans = 0
mi = float("inf")
for i, c in enumerate(dp):
if c == "1":
if mi > abs(N / 2 - i):
mi = abs(N / 2 - i)
ans = i
print(ans * (ans - 1) // 2 + (N - ans) * (N - ans - 1) // 2)
| Statement
In the State of Takahashi in AtCoderian Federation, there are N cities,
numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th
road connects City A_i and City B_i. Every road connects two distinct cities.
Also, for any two cities, there is at most one road that **directly** connects
them.
One day, it was decided that the State of Takahashi would be divided into two
states, Taka and Hashi. After the division, each city in Takahashi would
belong to either Taka or Hashi. It is acceptable for all the cities to belong
Taka, or for all the cities to belong Hashi. Here, the following condition
should be satisfied:
* Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the
same state. If it is impossible to divide the cities into Taka and Hashi so
that the condition is satisfied, print `-1`. | [{"input": "5 5\n 1 2\n 1 3\n 3 4\n 3 5\n 4 5", "output": "4\n \n\nFor example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong\nto Hashi, the condition is satisfied. Here, the number of roads whose endpoint\ncities belong to the same state, is 4.\n\n* * *"}, {"input": "5 1\n 1 2", "output": "-1\n \n\nIn this sample, the condition cannot be satisfied regardless of which cities\nbelong to each state.\n\n* * *"}, {"input": "4 3\n 1 2\n 1 3\n 2 3", "output": "3\n \n\n* * *"}, {"input": "10 39\n 7 2\n 7 1\n 5 6\n 5 8\n 9 10\n 2 8\n 8 7\n 3 10\n 10 1\n 8 10\n 2 3\n 7 4\n 3 9\n 4 10\n 3 4\n 6 1\n 6 7\n 9 5\n 9 7\n 6 9\n 9 4\n 4 6\n 7 5\n 8 3\n 2 5\n 9 2\n 10 7\n 8 6\n 8 9\n 7 3\n 5 3\n 4 5\n 6 3\n 2 10\n 5 10\n 4 2\n 6 2\n 8 4\n 10 6", "output": "21"}] |
Print the answer.
* * * | s480886254 | Wrong Answer | p03321 | Input is given from Standard Input in the following format:
N M
A_1 B_1
A_2 B_2
:
A_M B_M | #!/usr/bin/env python3
import numpy as np
class Impossible(ValueError):
pass
def main():
n, m, am, bm = read()
c_adj_mat = get_c_adj_mat(n, m, am, bm)
try:
color = get_color(n, m, c_adj_mat)
print(min_cut(color, n))
except Impossible:
print(-1)
def min_cut(color, n):
cnts = [0, 0, 0]
for c in color:
assert c in [-1, 0, 1]
cnts[c + 1] += 1
smaller = min(n // 2, min(cnts[0], cnts[2]) + cnts[1])
larger = n - smaller
res = smaller * (smaller - 1) // 2
res += larger * (larger - 1) // 2
return res
def get_c_adj_mat(n, m, am, bm):
c_adj_mat = [[False for i in range(n)] for j in range(n)]
for i in range(n):
c_adj_mat[i][i] = True
for k in range(m):
a0 = am[k] - 1
b0 = bm[k] - 1
c_adj_mat[a0][b0] = c_adj_mat[b0][a0] = True
return np.invert(np.array(c_adj_mat))
def get_color(n, m, c_adj_mat):
color = [0 for i in range(n)]
if not np.any(c_adj_mat):
return color
flag = False
for i in range(n):
for j in range(n):
if c_adj_mat[i, j]:
flag = True
break
if flag:
break
assert flag
color[i] = 1
dfs(color, i, c_adj_mat, n)
return color
def dfs(color, i, c_adj_mat, n):
assert color[i] in [-1, 1]
for j in range(n):
if not c_adj_mat[i, j]:
continue
if j == i:
continue
if color[j] * color[i] == -1:
continue
elif color[j] * color[i] == 1:
raise Impossible
else:
color[j] = -color[i]
dfs(color, j, c_adj_mat, n)
def read():
n, m = map(int, input().split())
am = []
bm = []
for i in range(m):
a, b = map(int, input().split())
am.append(a)
bm.append(b)
return n, m, am, bm
main()
| Statement
In the State of Takahashi in AtCoderian Federation, there are N cities,
numbered 1, 2, ..., N. M bidirectional roads connect these cities. The i-th
road connects City A_i and City B_i. Every road connects two distinct cities.
Also, for any two cities, there is at most one road that **directly** connects
them.
One day, it was decided that the State of Takahashi would be divided into two
states, Taka and Hashi. After the division, each city in Takahashi would
belong to either Taka or Hashi. It is acceptable for all the cities to belong
Taka, or for all the cities to belong Hashi. Here, the following condition
should be satisfied:
* Any two cities in the same state, Taka or Hashi, are directly connected by a road.
Find the minimum possible number of roads whose endpoint cities belong to the
same state. If it is impossible to divide the cities into Taka and Hashi so
that the condition is satisfied, print `-1`. | [{"input": "5 5\n 1 2\n 1 3\n 3 4\n 3 5\n 4 5", "output": "4\n \n\nFor example, if the cities 1, 2 belong to Taka and the cities 3, 4, 5 belong\nto Hashi, the condition is satisfied. Here, the number of roads whose endpoint\ncities belong to the same state, is 4.\n\n* * *"}, {"input": "5 1\n 1 2", "output": "-1\n \n\nIn this sample, the condition cannot be satisfied regardless of which cities\nbelong to each state.\n\n* * *"}, {"input": "4 3\n 1 2\n 1 3\n 2 3", "output": "3\n \n\n* * *"}, {"input": "10 39\n 7 2\n 7 1\n 5 6\n 5 8\n 9 10\n 2 8\n 8 7\n 3 10\n 10 1\n 8 10\n 2 3\n 7 4\n 3 9\n 4 10\n 3 4\n 6 1\n 6 7\n 9 5\n 9 7\n 6 9\n 9 4\n 4 6\n 7 5\n 8 3\n 2 5\n 9 2\n 10 7\n 8 6\n 8 9\n 7 3\n 5 3\n 4 5\n 6 3\n 2 10\n 5 10\n 4 2\n 6 2\n 8 4\n 10 6", "output": "21"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s376979846 | Wrong Answer | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | import itertools
def read():
N, T = list(map(int, input().strip().split()))
A = list()
B = list()
for i in range(N):
a, b = list(map(int, input().strip().split()))
A.append(a)
B.append(b)
return N, T, A, B
def solve(N, T, A, B):
dp1 = [[0 for j in range(T)] for i in range(N + 2)]
dp2 = [[0 for j in range(T)] for i in range(N + 2)]
for i, t in itertools.product(range(N), range(T)):
u = t - A[i]
if u >= 0:
dp1[i + 1][t] = max(dp1[i][u] + B[i], dp1[i][t])
else:
dp1[i + 1][t] = dp1[i][t]
u = t - A[N - 1 - i]
if u >= 0:
dp2[N - i][t] = max(dp2[N + 1 - i][u] + B[i], dp2[N + 1 - i][t])
else:
dp2[N - i][t] = dp2[N + 1 - i][t]
v = 0
for i, t in itertools.product(range(N), range(T)):
v = max(v, dp1[i][t] + dp2[i + 2][(T - 1) - t] + B[i])
return v
if __name__ == "__main__":
inputs = read()
print("%s" % solve(*inputs))
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s865996632 | Accepted | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | N, T = map(int, input().split())
table = [[0] * (N + 1) for _ in range(T + 1)]
dish = sorted((tuple(map(int, input().split())) for _ in range(N)), key=lambda t: t[0])
for t in range(T + 1):
for n in range(N):
minutes_to_take, deliciousness = dish[n]
now = table[t][n]
if t < T:
table[min(T, t + minutes_to_take)][n + 1] = max(
table[min(T, t + minutes_to_take)][n + 1], now + deliciousness
)
if n < N:
table[t][n + 1] = max(table[t][n + 1], now)
print(table[T][N])
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s681987283 | Accepted | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | e = enumerate
(n, t), *g = [list(map(int, t.split())) for t in open(0)]
d = [0]
g.sort()
dp = []
d = [0] * t
for a, b in g:
p = d[:]
for i in range(a, t):
v = d[i - a] + b
if v > p[i]:
p[i] = v
dp += (p,)
d = p
a = m = 0
for (*_, v), (_, w) in zip(dp[-2::-1], g[::-1]):
if w > m:
m = w
if v + m > a:
a = v + m
print(a)
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s604568349 | Wrong Answer | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | N, T = [int(s) for s in input().split()]
lst = [[int(s) for s in input().split()] for i in range(N)]
result = []
for k in range(N):
ls = [e for e in lst]
y = ls.pop(k)
dp = [0 for i in range(T - 1)]
for i in range(N - 1):
a, b = ls[i]
for t in range(T - 1 - a):
s = T - t - 2 - a
x = dp[s] + b
if dp[s + a] < x:
dp[s + a] = x
result.append(dp[-1] + y[1])
print(max(result))
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s750058992 | Accepted | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | N, T = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]
dp0 = {0: 0}
dp1 = {0: 0}
for i, (a, b) in enumerate(AB):
_dp0 = {0: 0}
_dp1 = {0: 0}
for k0, v0 in dp0.items():
if k0 in _dp0:
_dp0[k0] = max(_dp0[k0], v0)
else:
_dp0[k0] = v0
if k0 + a < T:
if k0 + a in _dp0:
_dp0[k0 + a] = max(_dp0[k0 + a], v0 + b)
else:
_dp0[k0 + a] = v0 + b
if k0 in _dp1:
_dp1[k0] = max(_dp1[k0], v0 + b)
else:
_dp1[k0] = v0 + b
for k1, v1 in dp1.items():
if k1 in _dp1:
_dp1[k1] = max(_dp1[k1], v1)
else:
_dp1[k1] = v1
if k1 + a < T:
if k1 + a in _dp1:
_dp1[k1 + a] = max(_dp1[k1 + a], v1 + b)
else:
_dp1[k1 + a] = v1 + b
dp0 = _dp0
dp1 = _dp1
print(max(dp1.values()))
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s251190650 | Wrong Answer | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | # ABC145 - Virtual
def fact(n, mod):
ret = 1
for i in range(2, n + 1):
ret = (ret * i) % mod
return ret
# A
if False:
print(int(input()) ** 2)
# B
if False:
N = int(input())
S = input()
print("Yes" if N % 2 == 0 and S[: N // 2] * 2 == S else "No")
# C
if False:
import itertools
N = int(input())
X = [tuple(map(int, input().split())) for _ in range(N)]
ans = 0
for p in itertools.permutations(X):
# print(p)
sx, sy = p[0]
for x, y in p:
ans += ((sx - x) ** 2 + (sy - y) ** 2) ** 0.5
sx, sy = x, y
# print(ans)
ans /= fact(N, 10**9 + 7)
print(ans)
# D
if False:
def pCq(p, q, mod):
pCq = 1
for i in range(q):
pCq *= p - i
pCq = pCq % mod
den = fact(q, mod)
inv = pow(den, mod - 2, mod)
pCq = (pCq * inv) % mod
return pCq
X, Y = map(int, input().split())
MOD = 10**9 + 7
ans = 0
n, m = 2 * X - Y, 2 * Y - X
if n % 3 == 0 and m % 3 == 0:
n //= 3
m //= 3
ans += pCq(m + n, max(m, n), MOD)
print(ans)
# E
if True:
N, T = map(int, input().split())
D = [tuple(map(int, input().split())) for _ in range(N)]
D.sort(key=lambda x: x[1])
ans = D.pop()[1]
dp = [[-1 for j in range(T + 1)] for i in range(N)]
# print(dp)
for i in range(N):
dp[i][0] = 0
for j in range(T):
dp[0][j] = 0
for i in range(1, N):
for j in range(1, T + 1):
dp[i][j] = max(
dp[i - 1][j],
(
dp[i - 1][j - D[i - 1][0]] + D[i - 1][1]
if j - D[i - 1][0] >= 0
else -1
),
)
ans += dp[-1][-1]
print(ans)
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s707406032 | Wrong Answer | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | n, t, *L = map(int, open(0).read().split())
d = [0] * (t + max(L[::2]))
for w, v in zip(*[iter(L)] * 2):
for i in range(t + w - 1, w - 1, -1):
if d[i - w] + v > d[i]:
d[i] = d[i - w] + v
print(max(d[t - 1 :]))
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s124772546 | Wrong Answer | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | import sys
sys.setrecursionlimit(4100000)
import heapq
n, t = [int(i) for i in input().split()]
a = [[int(i) for i in input().split()] for _ in range(n)]
heap = []
for i in range(len(a)):
heapq.heappush(heap, [-a[i][1] / a[i][0], a[i][0], a[i][1]])
ans = 0
maxleft = 0
while t > 0.5 and len(heap) > 0:
z, x, c = heapq.heappop(heap)
if t - x > 0.5:
t -= x
t -= 0.5
ans += c
else:
maxleft = max(maxleft, c)
print(ans + maxleft)
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s654247897 | Wrong Answer | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | n, t = map(int, input().split(" "))
tmp_food_table = []
for _ in range(n):
tmp_food_table.append(tuple(map(int, input().split(" "))))
food_table = []
for i in tmp_food_table:
food_table.append((i[0], i[1], i[1] / i[0]))
max_food = max(food_table, key=lambda x: x[1])
food_table.remove(max_food)
food_table = sorted(food_table, key=lambda x: x[1], reverse=True)
t = t - 1
current_t = 0
result = 0
for i in food_table:
current_t = current_t + i[0]
if current_t > t:
break
result = result + i[1]
result = result + max_food[1]
print(result)
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum possible happiness Takahashi can achieve.
* * * | s591177812 | Wrong Answer | p02863 | Input is given from Standard Input in the following format:
N T
A_1 B_1
:
A_N B_N | import sys
input = sys.stdin.readline
class AtCoder:
def main(self):
N, T = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda ab: ab[1] - ab[0], reverse=True)
total_t = 0
ans = 0
umami_min = 3000
boder = 0
for i, ab in enumerate(AB):
if total_t + ab[0] > T - 0.5:
boder = i
break
else:
umami_min = min(ab[1], umami_min)
ans += ab[1]
total_t += ab[0]
nokori = AB[boder:]
if len(nokori) > 1:
nokori.append([0, umami_min])
nokori.sort(key=lambda ab: ab[1], reverse=True)
ans -= umami_min
for i in range(2):
ans += nokori[i][1]
else:
ans += AB[boder][1]
print(ans)
# Run main
if __name__ == "__main__":
AtCoder().main()
| Statement
Takahashi is at an all-you-can-eat restaurant.
The restaurant offers N kinds of dishes. It takes A_i minutes to eat the i-th
dish, whose deliciousness is B_i.
The restaurant has the following rules:
* You can only order one dish at a time. The dish ordered will be immediately served and ready to eat.
* You cannot order the same kind of dish more than once.
* Until you finish eating the dish already served, you cannot order a new dish.
* After T-0.5 minutes from the first order, you can no longer place a new order, but you can continue eating the dish already served.
Let Takahashi's happiness be the sum of the deliciousness of the dishes he
eats in this restaurant.
What is the maximum possible happiness achieved by making optimal choices? | [{"input": "2 60\n 10 10\n 100 100", "output": "110\n \n\nBy ordering the first and second dishes in this order, Takahashi's happiness\nwill be 110.\n\nNote that, if we manage to order a dish in time, we can spend any amount of\ntime to eat it.\n\n* * *"}, {"input": "3 60\n 10 10\n 10 20\n 10 30", "output": "60\n \n\nTakahashi can eat all the dishes within 60 minutes.\n\n* * *"}, {"input": "3 60\n 30 10\n 30 20\n 30 30", "output": "50\n \n\nBy ordering the second and third dishes in this order, Takahashi's happiness\nwill be 50.\n\nWe cannot order three dishes, in whatever order we place them.\n\n* * *"}, {"input": "10 100\n 15 23\n 20 18\n 13 17\n 24 12\n 18 29\n 19 27\n 23 21\n 18 20\n 27 15\n 22 25", "output": "145"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s473139363 | Runtime Error | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | N, K = map(int,input().split())
P = [list(input().split()) for _ in range(N)]
a = 0
b = N
for i in range(1, N):
if P[0][2] == "W":
if P[i][2] == "W":
if (((int(P[0][0]) - int(P[i][0]) // K) % 2 == 1 and ((int(P[0][1]) - int(P[i][1]) // K) % 2 == 1) or ((int(P[0][0]) - int(P[i][0]) // K) % 2 == 1 and ((int(P[0][1]) - int(P[i][1]) // K) % 2 == 0:
a += 1
else:
if not ((((int(P[0][0]) - int(P[i][0]) // K) % 2 == 1 and ((int(P[0][1]) - int(P[i][1]) // K) % 2 == 1) or ((int(P[0][0]) - int(P[i][0]) // K) % 2 == 1 and ((int(P[0][1]) - int(P[i][1]) // K) % 2 == 0):
a += 1
else:
if P[i][2] == "B":
if (((int(P[0][0]) - int(P[i][0]) // K) % 2 == 1 and ((int(P[0][1]) - int(P[i][1]) // K) % 2 == 1) or ((int(P[0][0]) - int(P[i][0]) // K) % 2 == 1 and ((int(P[0][1]) - int(P[i][1]) // K) % 2 == 0:
a += 1
else:
if not ((((int(P[0][0]) - int(P[i][0]) // K) % 2 == 1 and ((int(P[0][1]) - int(P[i][1]) // K) % 2 == 1) or ((int(P[0][0]) - int(P[i][0]) // K) % 2 == 1 and ((int(P[0][1]) - int(P[i][1]) // K) % 2 == 0):
a += 1
print(max(a,N-a)) | Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s176058219 | Runtime Error | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | import sys
import numpy as np
read=sys.stdin.read
readline=sys.stdin.readline
def main():
def mycount(x1,y1,x2,y2,mat,s):
return mat[s,x2,y2]-mat[s,x1,y2]-mat[s,x2,y1]+mat[s,x1,y1]
wb={'W':0,'B':1}
n,k=map(int,readline().split())
xyc=[l.split() for l in read().splitlines()]
xyc=[[int(lst[0]) % (2*k),int(lst[1]) % (2*k),wb[lst[2]]] for lst in xyc]
mat=np.zeros((2,4*k+1,4*k+1),dtype='int64')
cand=np.zeros((2,2*k+1,2*k+1),dtype='int64')
for i in range(4):
for e in xyc:
mat[e[2],e[0]+1+2*k*(i&1),e[1]+1+2*k*(i>>1&1)]+=1
mat=np.cumsum(mat,axis=1)
mat=np.cumsum(mat,axis=2)
# for i in range(1,4*k+1):
# for j in range(1,4*k+1):
# mat[:,i,j]=mat[:,i,j]+mat[:,i-1,j]+mat[:,i,j-1]-mat[:,i-1,j-1]
cand+=mat[0,k:3*k+1,k:3*k+1]+mat[0,:2*k+1,:2*k+1]-mat[0,k:3*k+1,:2*k+1]-mat[0,:2*k+1,k:3*k+1]\
cand+=mat[0,2*k:4*k+1,2*k:4*k+1]+mat[0,k:3*k+1,k:3*k+1]-mat[0,2*k:4*k+1,k:3*k+1]-mat[0,k:3*k+1,2*k:4*k+1]\
cand+=mat[1,2*k:4*k+1,k:3*k+1]+mat[1,k:3*k+1,:2*k+1]-mat[1,2*k:4*k+1,:2*k+1]-mat[1,k:3*k+1,k:3*k+1]\
cand+=mat[1,k:3*k+1,2*k:4*k+1]+mat[1,:2*k+1,k:3*k+1]-mat[1,:2*k+1,2*k:4*k+1]-mat[1,k:3*k+1,k:3*k+1]
#print(mat)
print(np.max(cand))
if __name__=='__main__':
main()
| Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s671222583 | Runtime Error | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | n, k = map(int, input().split())
imos=[[0]*(k*2+1) for _ in range(k*2+1)]
def kukan(z, c):
if c=='B':
if 0<z<=k:
return [(0, k-z), (k*2-z, k*2)]
elif z=0:
return [(0, k)]
else:
return [(k*2-z, k*3-z)]
else:
if 0<=z<=k:
return [(k-z, k*2-z)]
else:
return [(0, k*2-z), (k*3-z, k*2)]
for i in range(n):
x, y, c = input().split()
x=int(x)%(k*2)
y=int(y)%(k*2)
kukanx=kukan(x, c)
kukany=kukan(y, c)
for kx in kukanx:
for ky in kukany:
imos[kx[0]][ky[0]]+=1
imos[kx[0]][ky[1]]-=1
imos[kx[1]][ky[0]]-=1
imos[kx[1]][ky[1]]+=1
def sum_imos(l):
w=len(l)
h=len(l[0])
for i in range(w):
for j in range(1, h):
l[i][j]+=l[i][j-1]
for i in range(1, w):
for j in range(h):
l[i][j]+=l[i-1][j]
return l
imos=sum_imos(imos)
print(max(max(imos[i]) for i in range(k*2+1) ))
| Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s955813912 | Runtime Error | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | n,k=map(int,input().split())
l=[input().split() for _ in range(n)]
w=[[0]*(k+1) for _ in range(k+1)]
b=[[0]*(k+1) for _ in range(k+1)]
for i in range(n):
if ((int(l[i][0])//k+int(l[i][1])//k)%2==0)^(l[i][2]=='W'):
b[int(l[i][0])%k][int(l[i][1])%k]+=1
else:
w[int(l[i][0])%k][int(l[i][1])%k]+=1
for i in range(1,k+1):
b[0][i]+=b[0][i-1]
b[i][0]+=b[i-1][0]
w[0][i]+=w[0][i-1]
w[i][0]+=w[i-1][0]
for i in range(1,k+1):
for j in range(1,k+1):
b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]
w[i][j]+=w[i-1][j]+w[i][j-1]-w[i-1][j-1]
ans=0
for i in range(k+1):
for j in range(k+1):
ans=max(ans,b[k][k]-b[i][k]-b[k][j]+2*b[i][j]+w[i][k]+w[k][j]-2*w[i][j])
ans=max(ans,w[k][k]-w[i][k]-w[k][j]+2*w[i][j]+b[i][k]+b[k][j]-2*b[i][j])
print(ans)
n,k=map(int,input().split())
l=[input().split() for _ in range(n)]
w=[[0]*(k+1) for _ in range(k+1)]
b=[[0]*(k+1) for _ in range(k+1)]
for i in range(n):
if ((int(l[i][0])//k+int(l[i][1])//k)%2==0)^(l[i][2]=='W'):
b[int(l[i][0])%k][int(l[i][1])%k]+=1
else:
w[int(l[i][0])%k][int(l[i][1])%k]+=1
for i in range(1,k+1):
b[0][i]+=b[0][i-1]
b[i][0]+=b[i-1][0]
w[0][i]+=w[0][i-1]
w[i][0]+=w[i-1][0]
for i in range(1,k+1):
for j in range(1,k+1):
b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]
w[i][j]+=w[i-1][j]+w[i][j-1]-w[i-1][j-1]
ans=0
for i in range(k+1):
for j in range(k+1):
ans=max(ans,b[k][k]-b[i][k]-b[k][j]+2*b[i][j]+w[i][k]+w[k][j]-2*w[i][j])
ans=max(ans,w[k][k]-w[i][k]-w[k][j]+2*w[i][j]+b[i][k]+b[k][j]-2*b[i][j])
print(ans)
Submission
問題 D - Checker
ユーザ名 lpo
投稿日時 2019/10/11 18:33:24
言語 PyPy3 (2.4.0)
状態 n,k=map(int,input().split())
l=[input().split() for _ in range(n)]
w=[[0]*(k+1) for _ in range(k+1)]
b=[[0]*(k+1) for _ in range(k+1)]
for i in range(n):
if ((int(l[i][0])//k+int(l[i][1])//k)%2==0)^(l[i][2]=='W'):
b[int(l[i][0])%k][int(l[i][1])%k]+=1
else:
w[int(l[i][0])%k][int(l[i][1])%k]+=1
for i in range(1,k+1):
b[0][i]+=b[0][i-1]
b[i][0]+=b[i-1][0]
w[0][i]+=w[0][i-1]
w[i][0]+=w[i-1][0]
for i in range(1,k+1):
for j in range(1,k+1):
b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]
w[i][j]+=w[i-1][j]+w[i][j-1]-w[i-1][j-1]
ans=0
for i in range(k+1):
for j in range(k+1):
ans=max(ans,b[k][k]-b[i][k]-b[k][j]+2*b[i][j]+w[i][k]+w[k][j]-2*w[i][j])
ans=max(ans,w[k][k]-w[i][k]-w[k][j]+2*w[i][j]+b[i][k]+b[k][j]-2*b[i][j])
print(ans)
n,k=map(int,input().split())
l=[input().split() for _ in range(n)]
w=[[0]*(k+1) for _ in range(k+1)]
b=[[0]*(k+1) for _ in range(k+1)]
for i in range(n):
if ((int(l[i][0])//k+int(l[i][1])//k)%2==0)^(l[i][2]=='W'):
b[int(l[i][0])%k][int(l[i][1])%k]+=1
else:
w[int(l[i][0])%k][int(l[i][1])%k]+=1
for i in range(1,k+1):
b[0][i]+=b[0][i-1]
b[i][0]+=b[i-1][0]
w[0][i]+=w[0][i-1]
w[i][0]+=w[i-1][0]
for i in range(1,k+1):
for j in range(1,k+1):
b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]
w[i][j]+=w[i-1][j]+w[i][j-1]-w[i-1][j-1]
ans=0
for i in range(k+1):
for j in range(k+1):
ans=max(ans,b[k][k]-b[i][k]-b[k][j]+2*b[i][j]+w[i][k]+w[k][j]-2*w[i][j])
ans=max(ans,w[k][k]-w[i][k]-w[k][j]+2*w[i][j]+b[i][k]+b[k][j]-2*b[i][j])
print(ans) | Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s963180164 | Runtime Error | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | give up | Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s614744295 | Runtime Error | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | 6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W | Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s242580049 | Accepted | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | def cumsum_2d(arr, cumsum_from="top_left"):
K = len(arr) # arr: K * K array
ret = [[0 for _ in range(K)] for _ in range(K)]
# 行方向に累積和
for y in range(K):
v = 0
for x in range(K):
if "right" in cumsum_from:
x = K - 1 - x
v += arr[y][x]
ret[y][x] = v
# 列方向に累積和
for y in range(1, K):
before_y = y - 1
if "bottom" in cumsum_from:
y = K - 1 - y
before_y = y + 1
for x in range(K):
ret[y][x] += ret[before_y][x]
return ret
def main():
N, K = list(map(int, input().split()))
# 同じ色になるセルをひとまとめにする
field_even = [[0 for _ in range(K)] for _ in range(K)]
field_odd = [[0 for _ in range(K)] for _ in range(K)]
for _ in range(N):
X, Y, C = input().split()
X = int(X) - 1
Y = int(Y) - 1
white_flag = 1 if C == "W" else 0
score = X // K + Y // K + white_flag
if score % 2 == 0:
field_even[Y % K][X % K] += 1
else:
field_odd[Y % K][X % K] += 1
# 右上・右下・左上・左下からの2次元累積和
even_tr = cumsum_2d(field_even, "top_right")
even_br = cumsum_2d(field_even, "bottom_right")
even_tl = cumsum_2d(field_even, "top_left")
even_bl = cumsum_2d(field_even, "bottom_left")
odd_tr = cumsum_2d(field_odd, "top_right")
odd_br = cumsum_2d(field_odd, "bottom_right")
odd_tl = cumsum_2d(field_odd, "top_left")
odd_bl = cumsum_2d(field_odd, "bottom_left")
# K * Kの正方形内の分割方法を全通り試して答えを求める
# 分割のうち、左上の正方形の右下のセルの座標で場合分け
ans = 0
for y in range(-1, K):
for x in range(-1, K):
# pattern 1
c = 0
if y >= 0 and x >= 0:
c += even_tl[y][x]
if y + 1 < K and x + 1 < K:
c += even_br[y + 1][x + 1]
if y + 1 < K and x >= 0:
c += odd_bl[y + 1][x]
if y >= 0 and x + 1 < K:
c += odd_tr[y][x + 1]
ans = max(ans, c)
# pattern 2(oddとeven入れ替え(白黒反転))
c = 0
if y >= 0 and x >= 0:
c += odd_tl[y][x]
if y + 1 < K and x + 1 < K:
c += odd_br[y + 1][x + 1]
if y + 1 < K and x >= 0:
c += even_bl[y + 1][x]
if y >= 0 and x + 1 < K:
c += even_tr[y][x + 1]
ans = max(ans, c)
print(ans)
if __name__ == "__main__":
main()
| Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s338584422 | Wrong Answer | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | # import time
# 入力部
N, K = map(int, input().split())
data_list = []
for i in range(N):
x, y, c = input().split()
x, y = int(x), int(y)
data_list.append([x, y, c])
# start = time.time()
# スコア行列[K, K]
# 白チェッカー左下を(x, y)としたときの正解数-誤答数
score_matrix = [[0 for x in range(K + 1)] for y in range(K + 1)]
# time1 = time.time()
# 白チェッカー左下が(0, 0)のときの希望の数
base_hope = 0
for data in data_list:
x, y, c = data[0] // K, data[1] // K, data[2]
x_coord, y_coord = data[0] % K, data[1] % K
# 正解
if ((x + y) % 2 == 0 and c == "W") or ((x + y) % 2 == 1 and c == "B"):
score_matrix[y_coord + 1][x_coord + 1] += 1
base_hope += 1
# 誤答
else:
score_matrix[y_coord + 1][x_coord + 1] -= 1
# time2 = time.time()
# スコア行列の二次元累積和を求める O(K^2)
xy = [(x, y) for x in range(1, K + 1) for y in range(1, K + 1)]
for x, y in xy:
accum = (
score_matrix[y][x]
+ score_matrix[y][x - 1]
+ score_matrix[y - 1][x]
- score_matrix[y - 1][x - 1]
)
score_matrix[y][x] = accum
# time3 = time.time()
# 希望行列を累積和に基づき計算していく O(K^2)
# 同時に最大値を計算
max_hope = 0
for x, y in xy:
diff = (
score_matrix[K - 1][x - 1]
+ score_matrix[y - 1][K - 1]
- 2 * score_matrix[y - 1][x - 1]
)
hope = base_hope - diff
true_hope = max(hope, N - hope)
if true_hope > max_hope:
max_hope = true_hope
print(max_hope)
| Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s509457180 | Wrong Answer | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | n, k = [int(_) for _ in input().split()]
x = []
y = []
c = []
for i in range(0, n):
t_x, t_y, t_c = input().split()
x.append(int(t_x))
y.append(int(t_y))
c.append(t_c)
for i in range(0, n):
if c[i] == "W":
c[i] = "B"
y[i] = y[i] + k
for i in range(0, n):
x[i] = x[i] % (2 * k)
y[i] = y[i] % (2 * k)
a = [[0 for _ in range(6 * k)] for _ in range(6 * k)]
for i in range(n):
a[x[i] + 1 + (2 * k - 1)][y[i] + 1 + (2 * k - 1)] += 2
a[x[i] + 1 + (2 * k - 1)][y[i] - k + 1 + (2 * k - 1)] -= 2
a[x[i] + 1 + (2 * k - 1)][y[i] + 1 + k + (2 * k - 1)] -= 2
a[x[i] + 1 + (2 * k - 1)][y[i] + 1 + 2 * k + (2 * k - 1)] += 1
a[x[i] + 1 + (2 * k - 1)][y[i] + 1 - 2 * k + (2 * k - 1)] += 1
a[x[i] - k + 1 + (2 * k - 1)][y[i] - k + 1 + (2 * k - 1)] += 2
a[x[i] - k + 1 + (2 * k - 1)][y[i] + 1 + (2 * k - 1)] -= 2
a[x[i] - k + 1 + (2 * k - 1)][y[i] + 1 + k + (2 * k - 1)] += 2
a[x[i] - k + 1 + (2 * k - 1)][y[i] + 1 + 2 * k + (2 * k - 1)] -= 1
a[x[i] - k + 1 + (2 * k - 1)][y[i] + 1 - 2 * k + (2 * k - 1)] -= 1
a[x[i] + 1 + k + (2 * k - 1)][y[i] + 1 + (2 * k - 1)] -= 2
a[x[i] + 1 + k + (2 * k - 1)][y[i] + 1 + k + (2 * k - 1)] += 2
a[x[i] + 1 + k + (2 * k - 1)][y[i] + 1 + 2 * k + (2 * k - 1)] -= 1
a[x[i] + 1 + k + (2 * k - 1)][y[i] + 1 - k + (2 * k - 1)] += 2
a[x[i] + 1 + k + (2 * k - 1)][y[i] + 1 - 2 * k + (2 * k - 1)] -= 1
a[x[i] + 1 + 2 * k + (2 * k - 1)][y[i] + 1 + (2 * k - 1)] += 1
a[x[i] + 1 + 2 * k + (2 * k - 1)][y[i] + 1 + k + (2 * k - 1)] -= 1
a[x[i] + 1 + 2 * k + (2 * k - 1)][y[i] + 1 + 2 * k + (2 * k - 1)] += 1
a[x[i] + 1 + 2 * k + (2 * k - 1)][y[i] + 1 - k + (2 * k - 1)] -= 1
a[x[i] + 1 - 2 * k + (2 * k - 1)][y[i] + 1 + (2 * k - 1)] += 1
a[x[i] + 1 - 2 * k + (2 * k - 1)][y[i] + 1 + k + (2 * k - 1)] -= 1
a[x[i] + 1 - 2 * k + (2 * k - 1)][y[i] + 1 - k + (2 * k - 1)] -= 1
a[x[i] + 1 - 2 * k + (2 * k - 1)][y[i] + 1 - 2 * k + (2 * k - 1)] += 1
s = [[0 for _ in range(6 * k)] for _ in range(6 * k)]
s[0][0] = a[0][0]
for i in range(0, 6 * k - 1):
for j in range(0, i + 1):
if j == 0:
s[j][i + 1] = s[j][i] + a[j][i + 1]
else:
s[j][i + 1] = s[j][i] + s[j - 1][i + 1] + a[j][i + 1] - s[j - 1][i]
for l in range(0, i + 1):
if l == 0:
s[i + 1][l] = s[i][l] + a[i + 1][l]
else:
s[i + 1][l] = s[i][l] + s[i + 1][l - 1] + a[i + 1][l] - s[i][l - 1]
s[i + 1][i + 1] = s[i][i + 1] + s[i + 1][i] + a[i + 1][i + 1] - s[i][i]
ans = 0
for i in range(0, 2 * k):
for j in range(0, 2 * k):
ans = max(ans, s[i + (k - 1)][j + (k - 1)])
print(ans)
| Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s941914760 | Wrong Answer | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | from subprocess import *
call(
(
"julia",
"-e",
"""
const lines=readlines()
input()=shift!(lines)
int(s)=parse(Int16,s)
intLine()=map(int,split(input()))
function nextLine(k)
x,y,c=split(input())
int(x)%2k+1,(int(y)+(c=="W")k)%2k+1
end
function solve(k,g)
m=0
for i=1:2k,j=1:2k
a=0
for x=-1:2,y=-1:2
(x+y)%2!=0&&continue
v,w=i+x*k,j+y*k
v>0<w&&(a+=g[min(2k,v)][min(2k,w)])
v>k&&w>0&&(a-=g[min(2k,v-k)][min(2k,w)])
w>k&&v>0&&(a-=g[min(2k,v)][min(2k,w-k)])
v>k<w&&(a+=g[min(2k,v-k)][min(2k,w-k)])
end
m=max(m,a)
end
m
end
function main()
n,k=intLine()
g=Array{Int16,1}[[0for j=1:2k]for i=1:2k]
for i=1:n
x,y=nextLine(k)
g[x][y]+=1
end
println(solve(k,cumsum(map(cumsum,g))))
end
main()
""",
)
)
| Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s586245157 | Accepted | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | def main():
"""
Range:
(x,y)
X: x-K+1 〜 x+K-1
Y: y-K+1 〜 y+K-1
rangeをすべて試す: 無理
1つの希望を満たすように範囲指定:
模様は確定する
(0,0)-(K,K) * 2(W or B)
K^2 * N -> 10^3^2 * 10^5
他の希望は満たされるか判定
"""
N, K = map(int, input().split())
xy = []
for _ in range(N):
x, y, c = input().split()
a = K if c == "B" else 0
xy.append((int(x), int(y) + a))
AC(K, xy)
# TLE(K, xy)
def fix(x, K2):
return min(max(0, x), K2)
def boundary(x, y, K):
K2 = K * 2
s = set()
for i in range(-2, 2):
for j in range(-2, 2):
if (i + j) % 2 == 1:
continue
x1 = fix(x + i * K, K2)
y1 = fix(y + j * K, K2)
x2 = fix(x + (i + 1) * K, K2)
y2 = fix(y + (j + 1) * K, K2)
if x1 == x2 or y1 == y2:
continue
s.add(((x1, y1), (x2, y2)))
return s
def AC(K, xy):
K2 = K * 2
w = [[0] * (K2 + 1) for _ in range(K2 + 1)]
for x, y in xy:
x = x % K2
y = y % K2
for (x1, y1), (x2, y2) in boundary(x, y, K):
w[y1][x1] += 1
w[y1][x2] -= 1
w[y2][x1] -= 1
w[y2][x2] += 1
ans = max_cumsum(w)
print(ans)
def max_cumsum(w):
# error in atcoder pypy3
# import numpy as np
# return np.array(w).cumsum(axis=1).cumsum(axis=0).max()
import copy
w = copy.deepcopy(w)
l = len(w)
for i in range(1, l):
for j in range(l):
w[j][i] += w[j][i - 1]
for i in range(l):
for j in range(1, l):
w[j][i] += w[j - 1][i]
return max(map(max, w))
def TLE(K, xy):
ans = 0
for i in range(2 * K):
for j in range(2 * K):
tmp_ans = 0
for x, y in xy:
x = (x + i) % (2 * K)
y = (y + j) % (2 * K)
w1 = x < K and y < K
w2 = x >= K and y >= K
if w1 or w2:
tmp_ans += 1
ans = max(ans, tmp_ans)
print(ans)
if __name__ == "__main__":
main()
| Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of desires that can be satisfied at the same time.
* * * | s664688600 | Wrong Answer | p03458 | Input is given from Standard Input in the following format:
N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N | a = list(map(int, input().split()))
b = []
for i in range(a[0]):
b.append(list(map(str, input().split())))
condition = []
for j in range(a[1]):
for k in range(a[1]):
case = 0
for l in range(a[0]):
if ((int(b[l][0]) - j) // a[1] + (int(b[l][1]) - k) // a[1]) % 2 == 0:
color = "B"
else:
color = "W"
if b[l][2] == color:
case = case + 1
condition.append(case)
print(max(condition))
| Statement
AtCoDeer is thinking of painting an infinite two-dimensional grid in a
_checked pattern of side K_. Here, a checked pattern of side K is a pattern
where each square is painted black or white so that each connected component
of each color is a K × K square. Below is an example of a checked pattern of
side 3:

AtCoDeer has N desires. The i-th desire is represented by x_i, y_i and c_i. If
c_i is `B`, it means that he wants to paint the square (x_i,y_i) black; if c_i
is `W`, he wants to paint the square (x_i,y_i) white. At most how many desires
can he satisfy at the same time? | [{"input": "4 3\n 0 1 W\n 1 2 W\n 5 3 B\n 5 4 B", "output": "4\n \n\nHe can satisfy all his desires by painting as shown in the example above.\n\n* * *"}, {"input": "2 1000\n 0 0 B\n 0 1 W", "output": "2\n \n\n* * *"}, {"input": "6 2\n 1 2 B\n 2 1 W\n 2 2 B\n 1 0 B\n 0 6 W\n 4 5 W", "output": "4"}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s457376794 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | # 12:40
n = int(input())
a1, a2, a3 = map(int, input().split())
b1, b2, b3 = map(int, input().split())
dp = [0] * (n + 1)
for i in range(1, n + 1):
tmp = dp[-1] + 1
if i - a1 >= 0:
if dp[i - a1] + b1 > tmp:
tmp = dp[i - a1] + b1
if i - a2 >= 0:
if dp[i - a2] + b2 > tmp:
tmp = dp[i - a2] + b2
if i - a3 >= 0:
if dp[i - a3] + b3 > tmp:
tmp = dp[i - a3] + b3
dp[i] = tmp
m = dp[-1]
# print(dp)
# print(m)
dp = [0] * (m + 1)
for j in range(1, m + 1):
tmp = dp[-1] + 1
if j - b1 >= 0:
if dp[j - b1] + a1 > tmp:
tmp = dp[j - b1] + a1
if j - b2 >= 0:
if dp[j - b2] + a2 > tmp:
tmp = dp[j - b2] + a2
if j - b3 >= 0:
if dp[j - b3] + a3 > tmp:
tmp = dp[j - b3] + a3
dp[j] = tmp
print(dp[-1])
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s401219326 | Accepted | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | def main():
n = int(input())
w1 = list(map(int, input().split()))
w2 = list(map(int, input().split()))
v1 = [0, 0, 0]
for i in range(3):
v1[i] = w2[i] - w1[i]
dp1 = [0] * (n + 1)
for i in range(n):
p = 0
q = 0
r = 0
if i + 1 - w1[0] >= 0:
p = dp1[i + 1 - w1[0]] + v1[0]
if i + 1 - w1[1] >= 0:
q = dp1[i + 1 - w1[1]] + v1[1]
if i + 1 - w1[2] >= 0:
r = dp1[i + 1 - w1[2]] + v1[2]
dp1[i + 1] = max(p, q, r, dp1[i])
m = dp1[-1] + n
v2 = [0, 0, 0]
for i in range(3):
v2[i] = w1[i] - w2[i]
dp2 = [0] * (m + 1)
for i in range(m):
p = 0
q = 0
r = 0
if i + 1 - w2[0] >= 0:
p = dp2[i + 1 - w2[0]] + v2[0]
if i + 1 - w2[1] >= 0:
q = dp2[i + 1 - w2[1]] + v2[1]
if i + 1 - w2[2] >= 0:
r = dp2[i + 1 - w2[2]] + v2[2]
dp2[i + 1] = max(p, q, r, dp2[i])
print(m + dp2[-1])
if __name__ == "__main__":
main()
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s413312454 | Accepted | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | import sys
sys.setrecursionlimit(10**8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()
INF = 10**15
def solve():
x1 = [g1, s1, b1]
x2 = [g2, s2, b2]
# dp[i] := max future donguri when we use i donguri
dp = [-INF] * (N + 1)
dp[0] = N
for i in range(3):
t1 = x1[i]
t2 = x2[i]
if t1 >= t2:
continue
for j in range(t1, N + 1):
r = dp[j - t1] - t1
if r >= 0 and dp[j] < r + t2:
dp[j] = r + t2
don = max(dp)
dp = [0] * (don + 1)
dp[0] = don
for i in range(3):
t1 = x1[i]
t2 = x2[i]
if t1 <= t2:
continue
for j in range(t2, don + 1):
r = dp[j - t2] - t2
if r >= 0 and dp[j] < r + t1:
dp[j] = r + t1
return max(dp)
print(solve())
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s602852942 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
ca = list(map(int, input().split()))
cb = list(map(int, input().split()))
ab = [a - b for a, b in zip(ca, cb)]
ba = [b - a for a, b in zip(ca, cb)]
def cal(array, x):
global N
init = N
a, b, c = array
gx, sx, bx = x
if a <= 0:
r0 = 0
else:
r0 = init // gx
init -= gx * r0
if b <= 0:
r1 = 0
else:
r1 = init // sx
init -= sx * r1
if c <= 0:
r2 = 0
else:
r2 = init // bx
init -= bx * r2
res = 0
for k in range(r0 + 1):
for l in range(r1 + 1):
for m in range(r2 + 1):
loss = k * gx + l * sx + m * bx
if loss > N:
continue
score = k * a + l * b + m * c
if score > res:
res = score
N += res
cal(ba, ca)
cal(ab, cb)
print(N)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s148054215 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
fr = tuple(map(int, input().split()))
to = tuple(map(int, input().split()))
ans = N
for g in range(N // fr[0] + 1):
for s in range(N // fr[1] + 1):
D = N - g * fr[0] - s * fr[1]
if D < 0:
break
b, D = divmod(D, fr[2])
q, r = divmod(g, to[0])
D += q
g = r
q, r = divmod(s, to[1])
D += q
s = r
q, r = divmod(b, to[2])
D += q
s = r
q, r = divmod(g, fr[0])
D += q
q, r = divmod(s, fr[1])
D += q
q, r = divmod(b, fr[2])
D += q
ans = max(ans, D)
print(ans)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s434082502 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
ga, sa, ba = map(int, input().split(" "))
gb, sb, bb = map(int, input().split(" "))
g_ratio = gb / ga
s_ratio = sb / sa
b_ratio = bb / ba
lst = []
lst.append([0, ga, g_ratio])
lst.append([1, sa, s_ratio])
lst.append([2, ba, b_ratio])
lst2 = sorted(lst, key=lambda x: -x[2])
outs = [0, 0, 0]
n = N
out = 0
for n1 in range(N + 1):
for n2 in range(N - n1 + 1):
for n3 in range(N - n1 - n2 + 1):
nn = [n1, n2, n3]
for i in range(3):
# print(lst2[i])
a0 = lst2[i][1]
ratio0 = lst2[i][2]
if ratio0 <= 1:
continue
n0 = nn[i]
# n0 = (n // a0) * a0
# print(n0, a0)
# n -= n0
# n0s[i] = n0
outs[i] = int(n0 * ratio0)
out = max(out, sum(outs) + (N - n1 - n2 - n3))
print(out)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s057511662 | Runtime Error | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | import math
n = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
wa_orig = [1, ga, sa, ba]
wb_orig = [1, gb, sb, bb]
wa = []
wb = []
for i in range(len(wa_orig)):
if wa_orig[i] <= wb_orig[i]:
wa.append(wa_orig[i])
wb.append(wb_orig[i])
dp = [0] * (n + 1)
for i in range(1, n + 1):
d = []
for ai in range(len(wa)):
w1 = wa[ai]
w2 = wb[ai]
if i - w1 < 0:
continue
d.append(dp[i - w1] + w2)
dp[i] = max(d)
wa = []
wb = []
for i in range(len(wa_orig)):
if wa_orig[i] >= wb_orig[i]:
wa.append(wa_orig[i])
wb.append(wb_orig[i])
nn = dp[-1]
if len(wa) == 2:
# greedy
w1 = wb[1]
w2 = wa[1]
value = (nn // w1) * w2 + (nn % w1)
print(value)
exit(0)
start = 0
start_v = 0
if len(wa) == 3:
v1 = wb[1]
v2 = wa[1]
w1 = wb[2]
w2 = wa[2]
if v2 / v1 > w2 / w1:
v1, v2 = w1, w2
l = (v1 * w1) // math.gcd(v1, w1)
lv = w2 * (l // w1)
start = (nn // l) * l
start_v = (nn // l) * lv
dp = [0] * (nn + 1)
dp[start] = start_v
for i in range(start + 1, nn + 1):
d = []
for ai in range(len(wb)):
w1 = wb[ai]
w2 = wa[ai]
if i - w1 < 0:
continue
d.append(dp[i - w1] + w2)
dp[i] = max(d)
print(dp[-1])
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s456992580 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
array = [(1, b[0] / a[0]), (2, b[1] / a[1]), (3, b[2] / a[2])]
array.sort(key=lambda x: x[1], reverse=True)
debug = [N, 0, 0, 0]
for arr in array:
if arr[1] > 1:
debug[arr[0]] += debug[0] // a[arr[0] - 1]
debug[0] -= (debug[0] // a[arr[0] - 1]) * a[arr[0] - 1]
for arr in array:
if arr[1] > 1:
debug[0] += debug[arr[0]] * b[arr[0] - 1]
debug[arr[0]] = 0
for arr in array[::-1]:
if arr[1] < 1:
debug[arr[0]] += debug[0] // b[arr[0] - 1]
debug[0] -= (debug[0] // b[arr[0] - 1]) * b[arr[0] - 1]
for arr in array[::-1]:
if arr[1] < 1:
debug[0] += debug[arr[0]] * a[arr[0] - 1]
debug[arr[0]] = 0
print(debug[0])
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s712687908 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | n = int(input())
ra = [int(v) for v in input().split()]
rb = [int(v) for v in input().split()]
ab_rate = sorted([(rb[i] / ra[i], i, ra[i], rb[i]) for i in range(3)], reverse=True)
ba_rate = sorted([(ra[i] / rb[i], i, rb[i], ra[i]) for i in range(3)], reverse=True)
def excange(r, x):
if r[0] <= 1:
return x, 0
else:
y = (x // r[2]) * r[3]
x = x % r[2]
return x, y
comb = [[0, 1, 2], [1, 0, 2], [2, 0, 1], [2, 1, 0], [0, 2, 1], [1, 2, 1]]
profmax = n
for k in comb:
prof, amari = 0, n
for i in k:
res = excange(ab_rate[i], amari)
amari = res[0]
prof += res[1]
profmax = max(profmax, prof + amari)
for k in comb:
prof, amari = 0, profmax
for i in k:
res = excange(ba_rate[i], amari)
amari = res[0]
prof += res[1]
profmax = max(profmax, prof + amari)
print(profmax)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s721510893 | Accepted | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
rate = [list(map(int, input().split())) for i in range(2)]
def solve(d, r):
l = len(r)
if l == 1:
d = d // r[0][0] * r[0][1] + d % r[0][0]
elif l == 2:
a = 0
w = [r[0][0], r[1][0]]
v = [r[0][1] - r[0][0], r[1][1] - r[1][0]]
for i in range(d // w[0] + 1):
a = max(a, v[0] * i + v[1] * ((d - w[0] * i) // w[1]))
d += a
elif l == 3:
a = [0] * (N + 1)
w = [r[0][0], r[1][0], r[2][0]]
v = [r[0][1] - r[0][0], r[1][1] - r[1][0], r[2][1] - r[2][0]]
for i in range(N + 1):
for j in range(l):
if i >= w[j]:
a[i] = max(a[i], a[i - w[j]] + v[j])
d += a[-1]
return d
r = []
for i in range(3):
if rate[0][i] < rate[1][i]:
r.append((rate[0][i], rate[1][i]))
d = solve(N, r)
r = []
for i in range(3):
if rate[0][i] > rate[1][i]:
r.append((rate[1][i], rate[0][i]))
d = solve(d, r)
print(d)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s477747103 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = B[0] / A[0]
b = B[1] / A[1]
c = B[2] / A[2]
C = [(A[0], a), (A[1], b), (A[2], c)]
C = sorted(C, key=lambda x: x[1], reverse=True)
D = [(B[0], a), (B[1], b), (B[2], c)]
D = sorted(D, key=lambda x: x[1], reverse=True)
if C[0][1] >= 1:
N0 = N // C[0][0]
N1 = N % C[0][0]
N2 = 0
N3 = N1
if C[1][1] > 1:
N2 = N1 // C[1][0]
N3 = N1 % C[1][0]
N4 = 0
N5 = N3
if C[2][1] > 1:
N4 = N3 // C[2][0]
N5 = N3 % C[2][0]
n = N0 * D[0][0]
n += N2 * D[1][0]
n += N4 * D[2][0]
n += N5
E = [(A[0], 1 / a), (A[1], 1 / b), (A[2], 1 / c)]
E = sorted(E, key=lambda x: x[1], reverse=True)
F = [(B[0], 1 / a), (B[1], 1 / b), (B[2], 1 / c)]
F = sorted(F, key=lambda x: x[1], reverse=True)
m = 0
if F[0][1] > 1:
n0 = n // F[0][0]
n1 = n % F[0][0]
n2 = 0
n3 = n1
if F[1][1] > 1:
n2 = n1 // F[1][0]
n3 = n1 % F[1][0]
n4 = 0
n5 = n3
m = n0 * E[0][0]
m += n2 * E[1][0]
m += n4 * E[2][0]
m += n5
m = max(m, n)
else:
E = [(A[0], 1 / a), (A[1], 1 / b), (A[2], 1 / c)]
E = sorted(E, key=lambda x: x[1], reverse=True)
F = [(B[0], 1 / a), (B[1], 1 / b), (B[2], 1 / c)]
F = sorted(F, key=lambda x: x[1], reverse=True)
if F[0][1] > 1:
N0 = N // F[0][0]
N1 = N % F[0][0]
N2 = 0
N3 = N1
if F[1][1] > 1:
N2 = N1 // F[1][0]
N3 = N1 % F[1][0]
N4 = 0
N5 = N3
if F[2][1] > 1:
N4 = N3 // F[2][0]
N5 = N3 % F[2][0]
m = N0 * E[0][0]
m += N2 * E[1][0]
m += N4 * E[2][0]
m += N5
print(m)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s921375850 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())
def perm_rec(items, p, perms):
if items:
p = p + [None]
for i in range(len(items)):
tmp_items = list(items)
p[-1] = tmp_items.pop(i)
perm_rec(tmp_items, p, perms)
else:
perms.append(p)
def perm():
perms_tmp = []
perm_rec(["g", "s", "b"], [], perms_tmp)
perms = []
for perm in perms_tmp:
for i in range(0, 3**3):
new_perm = list(perm)
for j in range(3):
if i % 3 == 2:
new_perm[j] = new_perm[j].upper()
elif i % 3 == 1:
new_perm[j] = " "
i //= 3
perms.append(new_perm)
return perms
perms = perm()
def convert(items, perm, g, s, b):
items = items.copy()
for p in perm:
if p == "g":
items["d"] += g * items["g"]
items["g"] = 0
elif p == "s":
items["d"] += s * items["s"]
items["s"] = 0
elif p == "b":
items["d"] += b * items["b"]
items["b"] = 0
elif p == "G":
c = items["d"] // g
items["g"] += c
items["d"] -= c * g
elif p == "S":
c = items["d"] // s
items["s"] += c
items["d"] -= c * s
elif p == "B":
c = items["d"] // b
items["b"] += c
items["d"] -= c * b
return items
max_d = 0
for p1 in perms:
items = convert({"d": N, "g": 0, "s": 0, "b": 0}, p1, gA, sA, bA)
for p2 in perms:
mid_items = convert(items, p2, gB, sB, bB)
# print(items, p1, mid_items)
last_items = convert(mid_items, ["g", "s", "b"], gA, sA, bA)
d = last_items["d"]
if d > max_d:
max_d = d
print(max_d)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s431465280 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | # D
N = int(input())
Agsb = list(map(int, input().split()))
Bgsb = list(map(int, input().split()))
divgsb = [Agsb[0] / Bgsb[0], Agsb[1] / Bgsb[1], Agsb[2] / Bgsb[2]]
vidgsb = [Bgsb[0] / Agsb[0], Bgsb[1] / Agsb[1], Bgsb[2] / Agsb[2]]
haveg = 0
haves = 0
haveb = 0
if max(vidgsb) == vidgsb[0]:
if max(vidgsb[1:]) == vidgsb[1]:
if vidgsb[0] > 1:
haveg = N // Agsb[0]
N %= Agsb[0]
if vidgsb[1] > 1:
haves = N // Agsb[1]
N %= Agsb[1]
if vidgsb[2] > 1:
haveb = N // Agsb[2]
N %= Agsb[2]
else:
if vidgsb[0] > 1:
haveg = N // Agsb[0]
N %= Agsb[0]
if vidgsb[2] > 1:
haves = N // Agsb[2]
N %= Agsb[2]
if vidgsb[1] > 1:
haveb = N // Agsb[1]
N %= Agsb[1]
elif max(vidgsb) == vidgsb[1]:
if max(vidgsb[0], vidgsb[2]) == vidgsb[0]:
if vidgsb[1] > 1:
haveg = N // Agsb[1]
N %= Agsb[1]
if vidgsb[0] > 1:
haves = N // Agsb[0]
N %= Agsb[0]
if vidgsb[2] > 1:
haveb = N // Agsb[2]
N %= Agsb[2]
else:
if vidgsb[1] > 1:
haveg = N // Agsb[1]
N %= Agsb[1]
if vidgsb[2] > 1:
haves = N // Agsb[2]
N %= Agsb[2]
if vidgsb[0] > 1:
haveb = N // Agsb[0]
N %= Agsb[0]
else:
if max(vidgsb[:1]) == vidgsb[1]:
if vidgsb[2] > 1:
haveg = N // Agsb[2]
N %= Agsb[2]
if vidgsb[1] > 1:
haves = N // Agsb[1]
N %= Agsb[1]
if vidgsb[0] > 1:
haveb = N // Agsb[0]
N %= Agsb[0]
else:
if vidgsb[2] > 1:
haveg = N // Agsb[2]
N %= Agsb[2]
if vidgsb[0] > 1:
haves = N // Agsb[0]
N %= Agsb[0]
if vidgsb[1] > 1:
haveb = N // Agsb[1]
N %= Agsb[1]
N += haveg * Bgsb[0] + haves * Bgsb[1] + haveb * Bgsb[2]
haveg, haves, haveb = 0, 0, 0
if max(divgsb) == divgsb[0]:
if max(divgsb[1:]) == divgsb[1]:
if divgsb[0] > 1:
haveg = N // Bgsb[0]
N %= Bgsb[0]
if divgsb[1] > 1:
haves = N // Bgsb[1]
N %= Bgsb[1]
if divgsb[2] > 1:
haveb = N // Bgsb[2]
N %= Bgsb[2]
else:
if divgsb[0] > 1:
haveg = N // Bgsb[0]
N %= Bgsb[0]
if divgsb[2] > 1:
haves = N // Bgsb[2]
N %= Bgsb[2]
if divgsb[1] > 1:
haveb = N // Bgsb[1]
N %= Bgsb[1]
elif max(divgsb) == divgsb[1]:
if max(divgsb[0], divgsb[2]) == divgsb[0]:
if divgsb[1] > 1:
haveg = N // Bgsb[1]
N %= Bgsb[1]
if divgsb[0] > 1:
haves = N // Bgsb[0]
N %= Bgsb[0]
if divgsb[2] > 1:
haveb = N // Bgsb[2]
N %= Bgsb[2]
else:
if divgsb[1] > 1:
haveg = N // Bgsb[1]
N %= Bgsb[1]
if divgsb[2] > 1:
haves = N // Bgsb[2]
N %= Bgsb[2]
if divgsb[0] > 1:
haveb = N // Bgsb[0]
N %= Bgsb[0]
else:
if max(divgsb[:1]) == divgsb[1]:
if divgsb[2] > 1:
haveg = N // Bgsb[2]
N %= Bgsb[2]
if divgsb[1] > 1:
haves = N // Bgsb[1]
N %= Bgsb[1]
if divgsb[0] > 1:
haveb = N // Bgsb[0]
N %= Bgsb[0]
else:
if divgsb[2] > 1:
haveg = N // Bgsb[2]
N %= Bgsb[2]
if divgsb[0] > 1:
haves = N // Bgsb[0]
N %= Bgsb[0]
if divgsb[1] > 1:
haveb = N // Bgsb[1]
N %= Bgsb[1]
N += haveg * Agsb[0] + haves * Agsb[1] + haveb * Agsb[2]
print(N)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s492182451 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
f = [0, 0, 0]
for i in range(3):
if A[i] < B[i]:
f[i] = 1
elif A[i] > B[i]:
f[i] = -1
ma = N
for i in range(3):
if f[i] < 0:
continue
Di = (N // A[i]) * B[i]
Ni = N - N // A[i]
for j in range(3):
if i == j:
continue
if f[j] < 0:
ma = max(ma, Ni + Di)
continue
Dj = Di + (Ni // A[j]) * B[i]
Nj = Ni - Ni // A[j]
for k in range(3):
if i == k or j == k:
continue
if f[k] < 0:
ma = max(ma, Nj + Dj)
continue
Dk = Dj + (Nj // A[k]) * B[k]
Nk = Nj - Nj // A[k]
ma = max(ma, Nk + Dk)
N = ma
for i in range(3):
if f[i] > 0:
continue
Di = (N // B[i]) * A[i]
Ni = N - N // B[i]
for j in range(3):
if i == j:
continue
if f[j] > 0:
ma = max(ma, Ni + Di)
continue
Dj = Di + (Ni // B[j]) * A[j]
Nj = Ni - Ni // B[j]
for k in range(3):
if i == k or j == k:
continue
if f[k] > 0:
ma = max(ma, Nj + Dj)
continue
Dk = Dj + (Nj // B[k]) * A[k]
Nk = Nj - Nj // B[k]
ma = max(ma, Nk + Dk)
print(ma)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s732720535 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | N = int(input())
gx, sx, bx = map(int, input().split())
gy, sy, by = map(int, input().split())
ns = [gx / gy, sx / sy, bx / by, gy / gx, sy / sx, by / bx]
dic = {i: n for i, n in enumerate(ns) if n > 1}
dic = sorted(dic.items(), key=lambda x: x[1], reverse=True)
ans = 0
for d, _ in dic:
if d == 0:
if gy > N:
continue
n1 = N // gy
N -= n1 * gy
ans += n1 * gx
elif d == 1:
if sy > N:
continue
n1 = N // sy
N -= n1 * sy
ans += n1 * sx
elif d == 2:
if by > N:
continue
n1 = N // by
N -= n1 * by
ans += n1 * bx
elif d == 3:
if gx > N:
continue
n1 = N // gx
N -= n1 * gx
ans += n1 * gy
elif d == 4:
if sx > N:
continue
n1 = N // sx
N -= n1 * sx
ans += n1 * sy
elif d == 5:
if bx > N:
continue
n1 = N // bx
N -= n1 * bx
ans += n1 * by
print(N + int(ans))
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s276513817 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | import itertools
N = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
def times(M, a, b):
tmp = M
tmp -= a * (M // a)
tmp += b * (M // a)
return tmp
L = [0, 0, 0]
for i in range(3):
if a[i] < b[i]:
L[i] = -1
elif a[i] == b[i]:
L[i] = 0
else:
L[i] = 1
ans = N
for seq in itertools.permutations(range(3), 2):
tmp = N
tmp = times(tmp, a[seq[0]], b[seq[0]])
tmp = times(tmp, b[seq[1]], a[seq[1]])
ans = max(ans, tmp)
print(ans)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Print the maximum number of acorns that Chokudai can bring to the nest.
* * * | s112507918 | Wrong Answer | p03008 | Input is given from Standard Input in the following format:
N
g_A s_A b_A
g_B s_B b_B | n = int(input())
kane = [0, 0, 0]
l = [list(map(int, input().split())) for i in range(2)]
u = []
for i in range(3):
u.append(l[1][i] / l[0][i])
l.append(u)
uu = sorted(u, reverse=True)
if max(u) <= 1:
print(n)
else:
kane[u.index(uu[0])] += n // l[0][u.index(uu[0])]
n = n % l[0][u.index(uu[0])]
if uu[1] > 1:
kane[u.index(uu[1])] += n // l[0][u.index(uu[1])]
n = n % l[0][u.index(uu[1])]
if uu[2] > 1:
kane[u.index(uu[2])] += n // l[0][u.index(uu[2])]
n = n % l[0][u.index(uu[2])]
n += kane[u.index(max(u))] * l[1][u.index(max(u))]
n += kane[u.index(uu[1])] * l[1][u.index(uu[1])]
n += kane[u.index(uu[2])] * l[1][u.index(uu[2])]
# print(kane)
# print(l)
# print(n)
kane = [0, 0, 0]
u = []
for i in range(3):
u.append(l[0][i] / l[1][i])
uu = sorted(u, reverse=True)
l[0], l[1] = l[1], l[0]
if uu[0] > 1:
kane[u.index(uu[0])] += n // l[0][u.index(uu[0])]
n = n % l[0][u.index(uu[0])]
if uu[1] > 1:
kane[u.index(uu[1])] += n // l[0][u.index(uu[1])]
n = n % l[0][u.index(uu[1])]
if uu[2] > 1:
kane[u.index(uu[2])] += n // l[0][u.index(uu[2])]
n = n % l[0][u.index(uu[2])]
n += kane[u.index(max(u))] * l[1][u.index(max(u))]
n += kane[u.index(uu[1])] * l[1][u.index(uu[1])]
n += kane[u.index(uu[2])] * l[1][u.index(uu[2])]
print(n)
| Statement
The squirrel Chokudai has N acorns. One day, he decides to do some trades in
multiple precious metal exchanges to make more acorns.
His plan is as follows:
1. Get out of the nest with N acorns in his hands.
2. Go to Exchange A and do some trades.
3. Go to Exchange B and do some trades.
4. Go to Exchange A and do some trades.
5. Go back to the nest.
In Exchange X (X = A, B), he can perform the following operations any integer
number of times (possibly zero) in any order:
* Lose g_{X} acorns and gain 1 gram of gold.
* Gain g_{X} acorns and lose 1 gram of gold.
* Lose s_{X} acorns and gain 1 gram of silver.
* Gain s_{X} acorns and lose 1 gram of silver.
* Lose b_{X} acorns and gain 1 gram of bronze.
* Gain b_{X} acorns and lose 1 gram of bronze.
Naturally, he cannot perform an operation that would leave him with a negative
amount of acorns, gold, silver, or bronze.
What is the maximum number of acorns that he can bring to the nest? Note that
gold, silver, or bronze brought to the nest would be worthless because he is
just a squirrel. | [{"input": "23\n 1 1 1\n 2 1 1", "output": "46\n \n\nHe can bring 46 acorns to the nest, as follows:\n\n * In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }\n * In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n * In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }\n\nHe cannot have 47 or more acorns, so the answer is 46."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s545748343 | Accepted | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | import sys
input = sys.stdin.readline
def linput(ty=int, cvt=list):
return cvt(map(ty, input().split()))
def gcd(a: int, b: int):
while b:
a, b = b, a % b
return a
def lcm(a: int, b: int):
return a * b // gcd(a, b)
def main():
D = int(input()) # days
vC = [
0,
] + linput() # decrine rate
# sumC = sum(vC)
# satis (d,i)
mS = [
[
0,
]
+ linput()
for _ in [
0,
]
* D
]
## sample output
vT = [
int(input())
for _ in [
0,
]
* D
] ## for p.B
vL = [
-1,
] * (
26 + 1
) # last day of Type
# day = -1
res = 0 # manzoku
vR = []
rapp = vR.append
for d in range(D): # [0,364] day
t = vT[d] ## sample output
vS = mS[d]
Sdt = vS[t]
res += Sdt
vL[t] = d
C = sum(c * (d - l) for c, l in zip(vC, vL))
res -= C
rapp(res)
# sT = "No Yes".split()
# print(sT[res])
# print(res)
print(*vR, sep="\n")
if __name__ == "__main__":
main()
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s865057872 | Runtime Error | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | D, C = int(input()), list(map(int, input().split()))
s1, s2, s3, s4, s5 = (
list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),
)
d1, d2, d3, d4, d5 = (
int(input()),
int(input()),
int(input()),
int(input()),
int(input()),
)
sumonw = 0
sumonw += s1[d1 - 1]
cs = sum(C)
cs = cs - C[d1 - 1]
sumonw -= cs
print(sumonw)
sumonw += s2[d2 - 1]
cs = sum(C) * 2
cs = cs - C[d2 - 1] * 2 - C[d1 - 1]
sumonw -= cs
print(sumonw)
sumonw += s3[d3 - 1]
cs = sum(C) * 3
cs = cs - C[d3 - 1] * 3 - C[d2 - 1] * 2 - C[d1 - 1]
sumonw -= cs
print(sumonw)
sumonw += s4[d4 - 1]
cs = sum(C) * 4
cs = cs - C[d4 - 1] * 4 - C[d3 - 1] * 3 - C[d2 - 1] * 2 - C[d1 - 1]
sumonw -= cs
print(sumonw)
sumonw += s5[d5 - 1]
cs = sum(C) * 5
cs = cs - C[d5 - 1] * 5 - C[d4 - 1] * 4 - C[d2 - 1] * 2 - C[d1 - 1]
sumonw -= cs
print(sumonw)
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s385933830 | Wrong Answer | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | s = [input() for i in range(2)]
l0 = int(s[0])
l1 = list(map(int, s[1].split()))
l1all = sum(l1)
t = [input() for i in range(l0)]
for i in range(l0):
l2 = list(map(int, t[i].split()))
sowa = 0
u = [input() for i in range(l0)]
for i in range(l0):
l3 = int(u[i])
# print("l3:",l3,",l2[",l3-1,"]:",l2[l3-1],",l1all:",l1all,",l1[",l3-1,"]:",l1[l3-1])
sowa += l2[l3 - 1]
sowa -= l1all
sowa += l1[l3 - 1]
# print("l3:",l3-1,",l2[l3]:",l2[l3-1],",l1all:",l1all,",l1[l3]:",l1[l3-1])
print(sowa)
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s989218795 | Runtime Error | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | D = input()
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(26)]
rows = int(input())
x = [input() for i in range(rows)]
print("18398")
print("35037")
print("51140")
print("65837")
print("79325")
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s641024042 | Accepted | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | d = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for i in [0] * d]
t = [int(input()) for i in [0] * d]
k = 0
n = 1
p = [0] * 26
for i in t:
k += s[n - 1][i - 1]
p[i - 1] = n
k -= sum([(n - p[i]) * c[i] for i in range(26)])
print(k)
n += 1
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s442924364 | Wrong Answer | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | D, C = int(input()), list(map(int, input().split()))
sumone = 0
cs = sum(C)
lastcs = [0]
for i in range(1, D + 1):
currentday = list(map(int, input().split()))
sumone += max(currentday)
typed = currentday.index(max(currentday)) + 1
lastcs.append(typed)
cc = cs * i
for j in range(1, i + 1):
if lastcs[j] == typed and j != i:
cc -= 0
else:
cc -= C[lastcs[j] - 1] * j
sumone = sumone - cc
print(sumone)
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s318935287 | Accepted | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | # coding: utf-8
# Your code here!
import numpy as np
import copy
D = int(input())
C = list(map(int,input().split()))
contest = [list(map(int,input().split())) for i in range(D)]
inputs = [int(input()) for i in range(D)]
# print(contest)
# print(inputs)
lists = np.array([0 for i in range(26)])
Y = 0
for i,j in zip(contest,inputs):
lists += 1
lists[j-1] = 0
X = C * lists.T
# print(X)
# print(sum(X))
# x = sum(C[:j-1])+sum(C[j:])
# y = i[j-1]-x+solve
Y = i[j-1]-sum(X)+Y
print(Y)
Y = copy.deepcopy(Y) | Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s594734694 | Runtime Error | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | import random
D = int(input()) # コンテの日数
c = list(map(int, input().split())) # 26タイプ(AAC~AZC)のコンテストの満足度低下度
s = [
list(map(int, input().split())) for _ in range(D)
] # s[d][i]...d(1<=d<=D)日目にi(1<=i<=26)番目のコンテストを行った時の満足度上昇
# types=[int(input()) for _ in range(D)] #コンテタイプ(1~26)
# 満足度低下はΣ(1<=i<=26)ci*(d-last(d,i)) #なおlast(d,i)=d日目以前に最後にタイプiのコンテストをやった日にち(ない場合0)
# よって満足度はs[d][i]-Σ(1<=i<=26)ci*(d-last(d,i))
# i日目のスコアはmax(10^6+s[d][i]-Σ(1<=i<=26)ci*(d-last(d,i)),0)
# last=[0]*26 #typeごとに、最後に使われた日にちを記録するよ
typ = 0
ans = 0
# greedyに最大とりまーす
# D=365
types = [(i % 26) + 1 for i in range(D)]
# print(l)
sc = [0] * D
last = [0] * 26
for i in range(D):
typ = types[i] - 1
score = s[i][typ]
dwn = 0
last[typ] = i + 1
for j in range(26):
dwn = dwn + c[j] * ((i + 1) - last[j])
ans = score - dwn
# print(score,last)
sc[i] = ans
all = sum(sc)
for _ in range(800):
types2 = []
mi = float("inf")
ind = 0
for i in range(D):
if mi > sc[i]:
mi = sc[i]
ind = i
l = list(range(1, 27))
lab1 = l.pop(types[ind])
lab2 = random.choice(l)
spans = ind // 26
types2 = types
for i in range(spans * 26, min((spans + 1) * 26, D)):
if types[i] == lab2:
types[i] = lab1
types[ind] = lab2
break
sc2 = [0] * D
last = [0] * 26
for i in range(D):
typ = types[i] - 1
score = s[i][typ]
dwn = 0
last[typ] = i + 1
for j in range(26):
dwn = dwn + c[j] * ((i + 1) - last[j])
ans = score - dwn
# print(score,last)
sc2[i] = ans
all2 = sum(sc2)
if all2 < all:
types = types2
else:
sc = sc2
all = all2
# print(max(all,all2))
for i in range(D):
print(types[i])
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s260562121 | Wrong Answer | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | def yumiri(L, n):
for i in range(len(L)):
if L[i] > n:
return i - 1
def naobou(d, q):
newans = ans
rd = t[d - 1] - 1
rq = yumiri(ZL[rd], d)
newans += SUMD[ZL[rd][rq] - ZL[rd][rq - 1] - 1] * c[rd]
newans += SUMD[ZL[rd][rq + 1] - ZL[rd][rq] - 1] * c[rd]
newans -= SUMD[ZL[rd][rq + 1] - ZL[rd][rq - 1] - 1] * c[rd]
del ZL[rd][rq]
nd = q - 1
nq = yumiri(ZL[nd], d)
newans += SUMD[ZL[nd][nq + 1] - ZL[nd][nq] - 1] * c[nd]
newans -= SUMD[d - ZL[nd][nq] - 1] * c[nd]
newans -= SUMD[ZL[nd][nq + 1] - d - 1] * c[nd]
ZL[nd].insert(nq + 1, d)
newans -= s[d - 1][rd]
newans += s[d - 1][nd]
if newans < ans:
ZL[rd].insert(rq, d)
del ZL[nd][nq + 1]
return ans
else:
t[d - 1] = q
return newans
def rieshon():
ans = 0
for i in range(D):
ans += s[i][t[i] - 1]
for j in range(26):
if t[i] - 1 == j:
L[j] = 0
ZL[j].append(i + 1)
else:
L[j] += 1
ans -= L[j] * c[j]
for i in range(26):
ZL[i].append(D + 1)
return ans
import time
import random
startTime = time.time()
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
L = [0] * 26
ZL = [[0] for _ in range(26)]
SUMD = [0] * (D + 1)
for i in range(1, D + 1):
SUMD[i] = SUMD[i - 1] + i
t = [0] * D
for i in range(D):
ma = s[i][0]
man = 0
for j in range(1, 26):
if s[i][j] == ma:
if c[j] < c[man]:
ma = s[i][j]
man = j
elif s[i][j] > ma:
ma = s[i][j]
man = j
t[i] = man + 1
ans = rieshon()
while time.time() - startTime < 1.8:
d = random.randrange(D) + 1
q = random.randrange(26) + 1
if t[d - 1] == q:
continue
ans = naobou(d, q)
for i in range(D):
print(t[i])
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
Let v_d be the satisfaction at the end of day d. Print D integers v_d to
Standard Output in the following format:
v_1
v_2
\vdots
v_D
* * * | s646816679 | Wrong Answer | p02619 | Input is given from Standard Input in the form of the input of Problem A
followed by the output of Problem A.
D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
* The constraints and generation methods for the input part are the same as those for Problem A.
* For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints. | import random
D = int(input())
C = list(map(int, input().split())) # (#cty,)
S = [list(map(int, input().split())) for _ in range(D)] # (D, #cty)
class Scorer:
def __init__(self, c, s):
n_cty = len(c)
self.c = c # (#cty,)
self.s = s # (D, #cty)
self.elapsed_time = [0] * n_cty
self.score = 0
def check_updated_score(self, d, cty):
elapsed_time = self.elapsed_time[:]
# update elapsed time
for cty_ in range(len(elapsed_time)):
if cty_ == cty:
elapsed_time[cty_] = 0
else:
elapsed_time[cty_] += 1
cur_score = self.s[d][cty]
for cty_ in range(len(elapsed_time)):
cur_score -= self.c[cty_] * elapsed_time[cty_]
return self.score + cur_score
def update_score(self, d, cty):
elapsed_time = self.elapsed_time[:]
# update elapsed time
for cty_ in range(len(elapsed_time)):
if cty_ == cty:
elapsed_time[cty_] = 0
else:
elapsed_time[cty_] += 1
cur_score = self.s[d][cty]
for cty_ in range(len(elapsed_time)):
cur_score -= self.c[cty_] * elapsed_time[cty_]
self.score += cur_score
self.elapsed_time = elapsed_time
return self.score
scorer = Scorer(C, S)
T = []
for d in range(D):
best_cty = -1
best_score = -1
for cty_ in range(26):
cur_score = scorer.check_updated_score(d, cty_)
if best_score < cur_score:
best_cty = cty_
best_score = cur_score
scorer.update_score(d, best_cty)
T.append(best_cty)
N_REPLACE = 5
N_ITER = 100
EPS = 0.5
ATT = 0.95
best_score = -1
for _ in range(N_ITER):
scorer = Scorer(C, S)
T_ = T[:]
# replace N elements
for i in range(N_REPLACE):
T_[random.randint(0, D - 1)] = random.randint(1, 26)
for d in range(D):
cty = T_[d] - 1
scorer.update_score(d, cty)
if best_score <= scorer.score:
best_score = scorer.score
T = T_
elif random.random() < EPS:
best_score = scorer.score
T = T_
EPS *= ATT
# for t in T:
# print(t)
print(best_score)
| Statement
You will be given a contest schedule for D days. For each d=1,2,\ldots,D,
calculate the satisfaction at the end of day d.
* * * | [{"input": "5\n 86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82\n 19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424\n 6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570\n 6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256\n 8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452\n 19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192\n 1\n 17\n 13\n 14\n 13", "output": "18398\n 35037\n 51140\n 65837\n 79325\n \n\nNote that this example is a small one for checking the problem specification.\nIt does not satisfy the constraint D=365 and is never actually given as a test\ncase."}] |
For each dataset, find the smallest number _i_ that satisfies the condition
_a_ _i_ = _a_ _j_ (_i_ > _j_ ) and print a line containing three
integers, _j_ , _a_ _i_ and _i_ − _j_. Numbers should be separated by a
space. Leading zeros should be suppressed. Output lines should not contain
extra characters.
You can assume that the above _i_ is not greater than 20. | s364786236 | Accepted | p00761 | The input consists of multiple datasets. A dataset is a line containing two
integers _a_ 0 and _L_ separated by a space. _a_ 0 and _L_ represent the
initial integer of the sequence and the number of digits, respectively, where
1 ≤ _L_ ≤ 6 and 0 ≤ _a_ 0 < 10 _L_ .
The end of the input is indicated by a line containing two zeros; it is not a
dataset. | while True:
n, d = input().split()
if n == "0" and d == "0":
break
a = [int(n)]
while True:
n = "".join(sorted(n.zfill(int(d))))
b = int(n[::-1]) - int(n)
if b in a:
break
a += [b]
n = str(b)
print(a.index(b), b, len(a) - a.index(b))
| Recurring Decimals
A decimal representation of an integer can be transformed to another integer
by rearranging the order of digits. Let us make a sequence using this fact.
A non-negative integer _a_ 0 and the number of digits _L_ are given first.
Applying the following rules, we obtain _a_ _i_ +1 from _a_ _i_.
1. Express the integer _a_ _i_ in decimal notation with _L_ digits. Leading zeros are added if necessary. For example, the decimal notation with six digits of the number 2012 is 002012.
2. Rearranging the digits, find the largest possible integer and the smallest possible integer; In the example above, the largest possible integer is 221000 and the smallest is 000122 = 122.
3. A new integer _a_ _i_ +1 is obtained by subtracting the smallest possible integer from the largest. In the example above, we obtain 220878 subtracting 122 from 221000.
When you repeat this calculation, you will get a sequence of integers _a_ 0 ,
_a_ 1 , _a_ 2 , ... .
For example, starting with the integer 83268 and with the number of digits 6,
you will get the following sequence of integers _a_ 0 , _a_ 1 , _a_ 2 , ... .
> _a_ 0 = 083268
> _a_ 1 = 886320 − 023688 = 862632
> _a_ 2 = 866322 − 223668 = 642654
> _a_ 3 = 665442 − 244566 = 420876
> _a_ 4 = 876420 − 024678 = 851742
> _a_ 5 = 875421 − 124578 = 750843
> _a_ 6 = 875430 − 034578 = 840852
> _a_ 7 = 885420 − 024588 = 860832
> _a_ 8 = 886320 − 023688 = 862632
> …
>
Because the number of digits to express integers is fixed, you will encounter
occurrences of the same integer in the sequence _a_ 0 , _a_ 1 , _a_ 2 , ...
eventually. Therefore you can always find a pair of _i_ and _j_ that
satisfies the condition _a_ _i_ = _a_ _j_ (_i_ > _j_ ). In the example
above, the pair (_i_ = 8, _j_ = 1) satisfies the condition because _a_ 8 =
_a_ 1 = 862632.
Write a program that, given an initial integer _a_ 0 and a number of digits
_L_ , finds the smallest _i_ that satisfies the condition _a_ _i_ = _a_ _j_
(_i_ > _j_ ). | [{"input": "4\n 83268 6\n 1112 4\n 0 1\n 99 2\n 0 0", "output": "6174 1\n 1 862632 7\n 5 6174 1\n 0 0 1\n 1 0 1"}] |
For each dataset, find the smallest number _i_ that satisfies the condition
_a_ _i_ = _a_ _j_ (_i_ > _j_ ) and print a line containing three
integers, _j_ , _a_ _i_ and _i_ − _j_. Numbers should be separated by a
space. Leading zeros should be suppressed. Output lines should not contain
extra characters.
You can assume that the above _i_ is not greater than 20. | s272836293 | Wrong Answer | p00761 | The input consists of multiple datasets. A dataset is a line containing two
integers _a_ 0 and _L_ separated by a space. _a_ 0 and _L_ represent the
initial integer of the sequence and the number of digits, respectively, where
1 ≤ _L_ ≤ 6 and 0 ≤ _a_ 0 < 10 _L_ .
The end of the input is indicated by a line containing two zeros; it is not a
dataset. | def sort_MxMn(SeriesA):
global max_a, min_a
listA = list(str(SeriesA))
while len(listA) < b:
listA.append("0")
listA = [int(x) for x in listA]
max_a = sorted(listA, reverse=True)
min_a = sorted(listA)
def translate(listEx):
num = 0
for i in range(b):
num += listEx[i] * (10 ** (b - i - 1))
return num
for k in range(10):
a, b = map(int, input().split())
if a + b == 0:
break
c = []
for i in range(10):
c.append(a)
sort_MxMn(a)
a = translate(max_a) - translate(min_a)
if (a in c) == True:
print("{} {} {}".format(c.index(a), a, i - c.index(a) + 1))
break
| Recurring Decimals
A decimal representation of an integer can be transformed to another integer
by rearranging the order of digits. Let us make a sequence using this fact.
A non-negative integer _a_ 0 and the number of digits _L_ are given first.
Applying the following rules, we obtain _a_ _i_ +1 from _a_ _i_.
1. Express the integer _a_ _i_ in decimal notation with _L_ digits. Leading zeros are added if necessary. For example, the decimal notation with six digits of the number 2012 is 002012.
2. Rearranging the digits, find the largest possible integer and the smallest possible integer; In the example above, the largest possible integer is 221000 and the smallest is 000122 = 122.
3. A new integer _a_ _i_ +1 is obtained by subtracting the smallest possible integer from the largest. In the example above, we obtain 220878 subtracting 122 from 221000.
When you repeat this calculation, you will get a sequence of integers _a_ 0 ,
_a_ 1 , _a_ 2 , ... .
For example, starting with the integer 83268 and with the number of digits 6,
you will get the following sequence of integers _a_ 0 , _a_ 1 , _a_ 2 , ... .
> _a_ 0 = 083268
> _a_ 1 = 886320 − 023688 = 862632
> _a_ 2 = 866322 − 223668 = 642654
> _a_ 3 = 665442 − 244566 = 420876
> _a_ 4 = 876420 − 024678 = 851742
> _a_ 5 = 875421 − 124578 = 750843
> _a_ 6 = 875430 − 034578 = 840852
> _a_ 7 = 885420 − 024588 = 860832
> _a_ 8 = 886320 − 023688 = 862632
> …
>
Because the number of digits to express integers is fixed, you will encounter
occurrences of the same integer in the sequence _a_ 0 , _a_ 1 , _a_ 2 , ...
eventually. Therefore you can always find a pair of _i_ and _j_ that
satisfies the condition _a_ _i_ = _a_ _j_ (_i_ > _j_ ). In the example
above, the pair (_i_ = 8, _j_ = 1) satisfies the condition because _a_ 8 =
_a_ 1 = 862632.
Write a program that, given an initial integer _a_ 0 and a number of digits
_L_ , finds the smallest _i_ that satisfies the condition _a_ _i_ = _a_ _j_
(_i_ > _j_ ). | [{"input": "4\n 83268 6\n 1112 4\n 0 1\n 99 2\n 0 0", "output": "6174 1\n 1 862632 7\n 5 6174 1\n 0 0 1\n 1 0 1"}] |
Print the minimum total fare.
* * * | s624813141 | Accepted | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | x = lambda a, b: min(a, b)
print(x(int(input()), int(input())) + x(int(input()), int(input())))
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Print the minimum total fare.
* * * | s645077376 | Runtime Error | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | import sys
input = sys.stdin.readline
a=int(input()
b=int(input()
c=int(input()
d=int(input()
print(min(a,b)+min(c,d))
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Print the minimum total fare.
* * * | s712159837 | Runtime Error | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | A, B, C, D = map(int, input().split())
list1 = [A, B]
list2 = [C, D]
X = min(list1)
Y = min(list2)
print(Y + X)
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Print the minimum total fare.
* * * | s149664347 | Accepted | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | Fare = [int(input()) for X in range(4)]
print(min(Fare[0], Fare[1]) + min(Fare[2], Fare[3]))
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Print the minimum total fare.
* * * | s243378437 | Accepted | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | t = min(int(input()), int(int(input())))
b = min(int(input()), int(int(input())))
print(t + b)
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Print the minimum total fare.
* * * | s402137951 | Accepted | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | a = [int(input()) for _ in range(2)]
c = [int(input()) for _ in range(2)]
print(min(a) + min(c))
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Print the minimum total fare.
* * * | s342635657 | Accepted | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | trainA = int(input())
trainB = int(input())
busA = int(input())
busB = int(input())
train_min = min(trainA, trainB)
bus_min = min(busA, busB)
min_price = train_min + bus_min
print(min_price)
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Print the minimum total fare.
* * * | s916524594 | Accepted | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | a = [[], []]
for i in range(2):
a[0].append(int(input()))
for i in range(2):
a[1].append(int(input()))
a[0].sort()
a[1].sort()
print(a[0][0] + a[1][0])
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Print the minimum total fare.
* * * | s915191996 | Accepted | p03399 | Input is given from Standard Input in the following format:
A
B
C
D | f = lambda: min(int(input()), int(input()))
print(f() + f())
| Statement
You planned a trip using trains and buses. The train fare will be A yen (the
currency of Japan) if you buy ordinary tickets along the way, and B yen if you
buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy
ordinary tickets along the way, and D yen if you buy an unlimited ticket.
Find the minimum total fare when the optimal choices are made for trains and
buses. | [{"input": "600\n 300\n 220\n 420", "output": "520\n \n\nThe train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you\nbuy an unlimited ticket. Thus, the optimal choice for trains is to buy an\nunlimited ticket for 300 yen. On the other hand, the optimal choice for buses\nis to buy ordinary tickets for 220 yen.\n\nTherefore, the minimum total fare is 300 + 220 = 520 yen.\n\n* * *"}, {"input": "555\n 555\n 400\n 200", "output": "755\n \n\n* * *"}, {"input": "549\n 817\n 715\n 603", "output": "1152"}] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.