code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
class Queue(object):
def __init__(self, _max):
if type(_max) != int:
raise ValueError
self._array = [None for i in range(0, _max)]
self._head = 0
self._tail = 0
self._cnt = 0
def enqueue(self, value):
if self.is_full():
raise IndexError
self._array[self._head] = value
self._cnt += 1
if self._head + 1 >= len(self._array):
self._head = 0
else:
self._head += 1
def dequeue(self):
if self.is_empty():
raise IndexError
value = self._array[self._tail]
self._cnt -= 1
if self._tail + 1 >= len(self._array):
self._tail = 0
else:
self._tail += 1
return value
def is_empty(self):
return self._cnt <= 0
def is_full(self):
return self._cnt >= len(self._array)
def round_robin(quantum, jobs):
queue = Queue(100000)
total = 0
for job in jobs:
queue.enqueue(job)
while not queue.is_empty():
name, time = queue.dequeue()
if time > quantum:
total += quantum
queue.enqueue((name, time - quantum))
else:
total += time
print("{0} {1}".format(name, total))
if __name__ == "__main__":
n, quantum = input().split()
jobs = [input().split() for i in range(0, int(n))]
jobs = [(job[0], int(job[1])) for job in jobs]
round_robin(int(quantum), jobs) | # similar to problem if n person are arranged in a row and have 3 different hats to wear
# no of ways of wearing hats so that every adjacent person wears different hats
# so ith person cannot wear more than 3 no of different ways
mod = 10**9+7
def main():
ans =1
n = int(input())
arr = list(map(int , input().split()))
col=[0 for i in range(0 , 6)]
for i in range(0 , n):
cnt , cur =0 , -1
if arr[i]==col[0]:
cnt = cnt+1
cur = 0
if arr[i]==col[1]:
cnt = cnt+1
cur = 1
if arr[i]==col[2]:
cnt = cnt+1
cur = 2
if cur ==-1:
print(0)
exit()
col[cur]=col[cur]+1
ans = (ans*cnt)%mod
ans = ans%mod
print(ans)
if __name__ =="__main__":
main() | 0 | null | 64,894,014,252,704 | 19 | 268 |
while(1):
m,f,r=map(int,input().split())
if m==-1 and f==-1 and r==-1:
break
elif m==-1 or f==-1:
print("F")
elif m+f>=80:
print("A")
elif m+f>=65 and m+f<80:
print("B")
elif m+f>=50 and m+f<65:
print("C")
elif m+f>=30 and m+f<50 and r>=50:
print("C")
elif m+f>=30 and m+f<50:
print("D")
elif m+f<30:
print("F")
| N = int(input())
i = 2
a = 0
while i <= N:
a += N // i
i *= 2
i = 2*5
b = 0
while i <= N:
b += N // i
i *= 5
print(min(a,b) if N % 2 == 0 else 0) | 0 | null | 58,885,692,371,242 | 57 | 258 |
from __future__ import division, print_function
from sys import stdin
n = int(stdin.readline())
result = [0, 0]
for _ in range(n):
taro, hanako = stdin.readline().split()
if taro > hanako:
result[0] += 3
elif taro < hanako:
result[1] += 3
else:
result[0] += 1
result[1] += 1
print(*result) | # -*- coding: utf-8 -*-
n = input()
x = y = 0
for _ in xrange(n):
a, b = raw_input().split()
if a==b:
x += 1
y += 1
elif a>b:
x += 3
else:
y += 3
print x, y | 1 | 1,980,296,923,318 | null | 67 | 67 |
count, outrate = map(int, input().split())
if count >= 10:
result = outrate
print(result)
else:
result = outrate + 100 * (10 - count)
print(result)
| def out(data) -> str:
print(' '.join(map(str, data)))
N = int(input())
data = [int(i) for i in input().split()]
out(data)
for i in range(1, N):
tmp = data[i]
j = i - 1
while j >= 0 and data[j] > tmp:
data[j + 1] = data[j]
j -= 1
data[j + 1] = tmp
out(data)
| 0 | null | 31,872,844,537,540 | 211 | 10 |
#!/usr/bin/env python3
# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
N = int(input())
C = list(input())
Rc = len(list(filter(lambda x:x=="R",C)))
Wc = len(C) - Rc
c = 0
for i in range(Rc):
if C[i] == "W":
c+=1
# print(Rc)
# print(Wc)
print(c)
pass
if __name__ == '__main__':
main()
| # coding=utf-8
inputs = raw_input().rstrip().split()
W, H, x, y, r = [int(x) for x in inputs]
if 0 <= x - r and x + r <= W and 0 <= y - r and y + r <= H:
print 'Yes'
else:
print 'No' | 0 | null | 3,375,512,246,040 | 98 | 41 |
N, K, S = map(int, input().split())
print(" ".join([str(S)] * K + [str(S+1)] * (N-K)) if S<10**9 else " ".join([str(S)] * K + ['1'] * (N-K)))
| from collections import Counter
n = int(input())
a = list(map(int, input().split()))
a += list(range(1,n+1))
a = Counter(a).most_common()
a.sort()
for i,j in a:
print(j-1) | 0 | null | 62,121,393,466,564 | 238 | 169 |
import math
N = int(input())
ans = math.floor(N / 2)
if N % 2 == 0:
print(ans - 1)
else:
print(ans) |
def main():
N, M, X = map(int,input().split())
cost = []
effect = []
for _ in range(N):
t = list(map(int,input().split()))
cost.append(t[0])
effect.append(t[1:])
res = float("inf")
for bits in range(1<<N):
f = True
total = 0
ans = [0] * M
for flag in range(N):
if (1<< flag) & (bits):
total += cost[flag]
for idx, e in enumerate(effect[flag]):
ans[idx] += e
for a in ans:
if a < X: f = False
if f:
res = min(res, total)
print(res if res != float("inf") else -1)
if __name__ == "__main__":
main()
| 0 | null | 87,886,817,054,322 | 283 | 149 |
def CheckNum(n, x, i):
x = i
if x % 3 == 0:
print(" %d"%(i), end="")
return 2, i, x
return 1, i, x
def Include3(n, x, i):
if x % 10 == 3:
print(" %d"%(i), end="")
return 2, i, x
x = int(x/10)
if x != 0:
return 1, i, x
return 2, i, x
def EndCheckNum(n, x, i):
i += 1
if i <= n:
return 0, i, x
print("")
return 3, i, x
def call(n):
counter = 0
i = 1
x = 1
func = [CheckNum, Include3, EndCheckNum]
while counter != 3:
counter, i, x = func[counter](n, x, i)
n = int(input())
call(n) | n = int(input())
for i in range(1, n + 1):
if i % 3 == 0 or str(i).find("3") != -1:
print(" {}".format(i), end = '')
print()
| 1 | 924,889,643,282 | null | 52 | 52 |
import sys
N = int(input())
A = [int(x) for x in input().split()]
L = [0] * N + [A[N]] # 下限
U = [0] * N + [A[N]] # 上限
for i in range(N - 1, -1, -1):
L[i] = (L[i + 1] + 1) // 2 + A[i]
U[i] = U[i + 1] + A[i]
if 1 < L[0]:
print(-1)
sys.exit()
U[0] = 1
for i in range(1, N + 1):
U[i] = min(2 * (U[i - 1] - A[i - 1]), U[i]) # 深さiの頂点数
print(sum(U)) | import math
N = int(input())
A = list(map(int,input().split()))
up = [[] for _ in range(N+1)] # 葉から考慮した場合の、最小値
up[N] = [A[N],A[N]]
#print(up)
for i in reversed(range(N)):
Min, Max = up[i+1]
#print(Min,Max)
Min_n = math.ceil(Min/2) + A[i]
Max_n = Max + A[i]
#print(A[i],i)
up[i] = [Min_n, Max_n]
#print(up)
if up[0][0] >= 2:
print(-1)
else:
down = 1
#print(down)
ans = 1
for i in range(1, N+1):
down *= 2
down = min(down,up[i][1]) - A[i]
ans += down + A[i]
print(ans) | 1 | 18,853,383,964,998 | null | 141 | 141 |
def main():
a,b,c = map(int,input().split(" "))
if (c-a-b)**2 > a*b*4 and c > a + b:
print("Yes")
else:
print("No")
main() | N = int(input())
S = []
for _ in range(N):
S.append(input())
ac = 0
wa = 0
tle = 0
re = 0
for s in S:
if s == "AC":
ac += 1
if s == "WA":
wa += 1
if s == "TLE":
tle += 1
if s == "RE":
re += 1
print("AC x ",ac)
print("WA x ",wa)
print("TLE x ",tle)
print("RE x ",re) | 0 | null | 29,940,112,587,880 | 197 | 109 |
from collections import deque
n = int(input())
adj = [[] for _ in range(n)]
for i in range(n):
u,k,*v = map(int, input().split())
v = [w-1 for w in v]
adj[u-1].extend(v)
queue = deque([0])
ans = [-1] * n
ans[0] = 0
while queue:
now = queue.popleft()
for u in adj[now]:
if ans[u] < 0:# 未訪問
ans[u] = ans[now] + 1
queue.append(u)
for i in range(n):
print(i+1,ans[i])
| from collections import deque
N = int(input())
graph = [[] for _ in range(N)]
for i in range(N):
X = list(map(int, input().split()))
for j in range(2, len(X)):
graph[i].append(X[j]-1)
dist = [-1] * N
dist[0] = 0
q = deque([])
q.append(0)
while q:
x = q.popleft()
for next_x in graph[x]:
if(dist[next_x] > -1):
continue
dist[next_x] = dist[x] + 1
q.append(next_x)
for i in range(N):
print(i+1, dist[i])
| 1 | 3,982,488,340 | null | 9 | 9 |
import sys
import math
def input():
return sys.stdin.readline().rstrip()
def main():
N, K = map(int, input().split())
A = list(map(int, input().split()))
ng = 0
ok = 10 ** 9 + 1
while ok - ng > 1:
mid = (ok + ng) // 2
ans = sum(list(map(lambda x: math.ceil(x / mid) - 1, A)))
# for i in range(N):
# ans += A[i]//mid -1
# if A[i]%mid !=0:
# ans +=1
if ans <= K:
ok = mid
else:
ng = mid
print(ok)
if __name__ == "__main__":
main()
| from sys import stdin
import numpy as np
from numba import njit, i8
n, k = map(int, stdin.readline().split())
a = np.array(stdin.readline().split(), dtype = np.int64)
@njit(cache=True)
def c(x, a):
res = 0
for i in range(n):
res += 0 - - a[i] // x - 1
return res <= k
ng = 0
ok = 10 ** 9 + 1
while ok - ng > 1:
mid = (ok + ng) >> 1
if c(mid, a):
ok = mid
else:
ng = mid
print(ok)
| 1 | 6,531,011,936,800 | null | 99 | 99 |
def check(n,k):
r=[]
while n>0:
r.append(n%k)
n//=k
return len(r)
n,k=map(int,input().split())
print(check(n,k)) | # n, k = map(int, input().split())
# from collections import OrderedDict
# d = OrderedDict()
# a = list(input().split())
b = list(map(int, input().split()))
# print(r + max(0, 100*(10-n)))
# print("Yes" if 500*n >= k else "No")
# s = input()
# a = int(input())
# b = int(input())
# c = int(input())
# s = input()
print(b[-1], *b[:2]) | 0 | null | 51,131,984,788,420 | 212 | 178 |
N,T = list(map(int,input().split()))
A =[list(map(int,input().split())) for i in range(N)]
A.sort(key = lambda x:x[0])
INF = float("inf")
DP = [[-INF]*(T+3001) for i in range(N+1)]
DP[0][0] = 0
for i in range(N):
for j in range(T):
DP[i+1][j] = max(DP[i+1][j],DP[i][j])
DP[i+1][j+A[i][0]] = max(DP[i+1][j+A[i][0]],DP[i][j+A[i][0]],DP[i][j]+A[i][1])
score = 0
for i in range(N):
for j in range(T+A[i][0]):
score = DP[i+1][j] if DP[i+1][j]>score else score
print(score) | N, T = map(int, input().split())
A, B = zip(*[tuple(map(int, input().split())) for _ in range(N)])
dp1 = [[0]*(T) for _ in range(N+1)]
dp2 = [[0]*(T) for _ in range(N+2)]
for i in range(N):
for j in range(T):
dp1[i+1][j] = max(dp1[i][j], dp1[i][j-A[i]]+B[i]) if j-A[i]>=0 else dp1[i][j]
for i in range(N, 0, -1):
for j in range(T):
dp2[i][j] = max(dp2[i+1][j], dp2[i+1][j-A[i-1]]+B[i-1]) if j-A[i-1]>=0 else dp2[i+1][j]
ans = -1
for i in range(1, N+1):
for j in range(T):
ans = max(ans, dp1[i-1][j]+dp2[i+1][T-j-1]+B[i-1])
print(ans) | 1 | 151,551,260,265,480 | null | 282 | 282 |
S, T = map(str, input().split())
print(T+S) | def solve():
S,T = input().split()
print(T+S)
if __name__ == "__main__":
solve() | 1 | 103,036,624,625,672 | null | 248 | 248 |
N,M = map(int, input().split())
prices = list(map(int,input().split()))
prices.sort()
print(sum(prices[0:M])) | n,k = input().split()
k = int(k)
#print(n,k)
p = [int(s) for s in input().split()]
p.sort()
#print(p)
#print(k)
p2 = p[0:k]
#print(p2)
s = sum(p)
#print(s)
print(sum(p2)) | 1 | 11,596,131,162,370 | null | 120 | 120 |
import numpy as np
n = int(input())
a = list(map(int,input().split()))
dp = np.zeros((n+1,2), int)
dp[1],dp[2] = [0,a[0]], [0,max(a[0], a[1])]
for i in range(3,n+1):
if(i%2 == 0):
dp[i][0] = max(dp[i-1][0],dp[i-2][0]+a[i-1],dp[i-2][1])
dp[i][1] = max(dp[i-1][1],dp[i-2][1]+a[i-1])
else:
dp[i][0] = max(dp[i-1][1],dp[i-2][1],dp[i-2][0]+a[i-1])
dp[i][1] = dp[i-2][1]+a[i-1]
print(dp[n][(n+1)%2]) | mount = []
for i in range(0, 10):
n = input()
mount.append(n)
mount.sort(reverse = True)
for i in range(0, 3):
print mount[i] | 0 | null | 18,835,970,864,380 | 177 | 2 |
x = int(input())
print("Yes" if x >= 30 else "No")
| import sys
readline = sys.stdin.readline
def main():
A, V = map(int, readline().rstrip().split())
B, W = map(int, readline().rstrip().split())
T = int(readline())
v = V - W
if v <= 0:
print('NO')
return
if abs(A - B) / v > T:
print('NO')
else:
print('YES')
if __name__ == '__main__':
main() | 0 | null | 10,372,844,579,632 | 95 | 131 |
import sys
li = []
for line in sys.stdin:
li.append(int(line))
if len(li) == 10:
break
li.sort(reverse=True)
li = li[0:3]
for i in li:
print(i) | m = []
while True:
try:
m.append(int(raw_input()))
except EOFError:
break
m.sort()
m.reverse()
for h in m[0:3]:
print h | 1 | 27,249,300 | null | 2 | 2 |
# -*- coding: utf-8 -*-
import sys
N=input()
bit=[ [ 0 for _ in range(N+1) ] for __ in range(27) ]
def add(i,a,w):
while a<=N:
bit[i][a]+=w
a+=a&-a
def sum(i,a):
ret=0
while 0<a:
ret+=bit[i][a]
a-=a&-a
return ret
S=sys.stdin.readline().strip()
S=[None]+list(S) #1-indexed
for j,x in enumerate(S):
if j==0: continue
i=ord(x)-96
add(i,j,1)
Q=input()
for _ in range(Q):
q1,q2,q3=sys.stdin.readline().split()
if q1=="1":
q1=int(q1)
q2=int(q2)
current_s=q3
former_s=S[q2]
former_s=ord(former_s)-96 #1-indexed
S[q2]=current_s #文字列で更新された1文字を置き換える
current_s=ord(current_s)-96
add(current_s,q2,1)
add(former_s,q2,-1)
if q1=="2":
q2=int(q2)
q3=int(q3)
begin,end=q2,q3
cnt=0
for i in range(1,27):
if 0<sum(i,end)-sum(i,begin-1):
cnt+=1
print cnt | import sys
import math
import bisect
def main():
n = int(input())
s = ['ACL'] * n
print(''.join(s))
if __name__ == "__main__":
main()
| 0 | null | 32,391,568,641,026 | 210 | 69 |
n = int(input())
a = list(input().split())
tmp = []
flag = 0
for i in range(n):
if int(a[i]) % 2 == 0:
tmp.append(a[i])
for j in range(len(tmp)):
if int(tmp[j]) % 3 == 0 or int(tmp[j]) % 5 == 0:
continue
else:
flag = 1
if flag == 1 : print("DENIED")
else : print("APPROVED") | n=int(input())
a=list(map(int,input().split()))
ans="APPROVED"
for i in range(n):
if a[i]%2==0:
if a[i]%3==0 or a[i]%5==0:
continue
else:
ans="DENIED"
break
print(ans) | 1 | 68,945,057,116,572 | null | 217 | 217 |
N = int(input())
res = 0
if N % 2 == 0:
div = N // 2
for i in range(1,30):
res += div // 5**i
print(res)
| from collections import deque,defaultdict
import copy
def main():
from collections import defaultdict
import copy
n,u,v = map(int,input().split())
u -= 1
v -= 1
graph = defaultdict(deque)
for _ in range(n-1):
a,b = map(int,input().split())
a -= 1
b -= 1
graph[a].append(b)
graph[b].append(a)
depth = dfs(graph,u,n)
# print(depth)
depth2 = dfs(graph,v,n)
# print(depth2)
ans = 0
ans2 = 10**6
for i,j in zip(depth,depth2):
if i < j:
ans = max(j,ans)
# elif i == j:
# ans2 = min(j,ans2)
print(ans-1)
def dfs(G,s,n):
stack = deque([s])
graph = copy.deepcopy(G)
depth = [None] * n
depth[s] = 0
while stack:
u = stack[-1]
if graph[u]:
v = graph[u].popleft()
if depth[v] == None:
depth[v] = depth[u] + 1
stack.append(v)
else:
stack.pop()
return depth
main() | 0 | null | 116,946,295,796,440 | 258 | 259 |
n = input()
n = n.split()
d = int(n[1])
n = int(n[0])
c = 0
for a in range(n):
b = input()
b = b.split()
x = int(b[0])
y = int(b[1])
if x * x + y * y <= d * d:
c = c + 1
print(c) | charge, n_coin = map(int,input().split())
coin_ls = list(map(int, input().split()))
coin_ls = [0] + coin_ls
dp = [[float('inf')] * (charge+1) for _ in range(n_coin+1)]
dp[0][0] = 0
for coin_i in range(1,n_coin+1):
for now_charge in range(0,charge+1):
if now_charge - coin_ls[coin_i] >= 0:
dp[coin_i][now_charge] = min(dp[coin_i][now_charge], dp[coin_i][now_charge-coin_ls[coin_i]]+1)
dp[coin_i][now_charge] = min(dp[coin_i-1][now_charge], dp[coin_i][now_charge])
print(dp[n_coin][charge])
| 0 | null | 3,008,374,010,808 | 96 | 28 |
import math
def main():
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
sumA = t1 * a1 + t2 * a2
sumB = t1 * b1 + t2 * b2
if sumA == sumB:
return 'infinity'
if sumA < sumB:
sumA, sumB = sumB, sumA
a1, b1 = b1, a1
a2, b2 = b2, a2
# A の方が sum が大きいとする
halfA = t1 * a1
halfB = t1 * b1
if halfA > halfB:
return 0
div, mod = divmod(halfB - halfA, sumA - sumB)
if mod == 0:
return div * 2
return div * 2 + 1
print(main()) | from itertools import combinations
n = int(input())
d = list(map(int, input().split()))
ans = 0
d = combinations(d, 2)
for i, j in d:
ans += i*j
print(ans)
| 0 | null | 150,067,723,089,332 | 269 | 292 |
N = int(input())
A = list(map(int, input().split()))
if N == 0:
if A[0] == 1:
print (1)
exit()
else:
print (-1)
exit()
MAX = 10 ** 18
lst = [1] * (10 ** 5 + 10)
#純粋な2分木としたときの最大値
for i in range(N + 1):
lst[i + 1] = min(MAX, lst[i] * 2)
# print (lst[:10])
for i in range(N + 1):
tmp = lst[i] - A[i]
if tmp < 0:
print (-1)
exit()
lst[i + 1] = min(lst[i + 1], tmp * 2)
# print (lst[:10])
if lst[N] >= A[N]:
lst[N] = A[N]
else:
print (-1)
exit()
# print (lst[:10])
ans = 1
tmp = 0
for i in range(N, 0, -1):
ans += lst[i]
lst[i - 1] = min(lst[i] + A[i - 1], lst[i - 1])
# print (lst[:10])
print (ans)
| n = int(input())
a = list(map(int, input().split()))
s = [0]*(n+2)
for i in range(n, -1, -1): s[i] = s[i+1]+a[i]
def solve():
cur = 1
res = 0
for i in range(n+1):
if cur < a[i]: return -1
res += cur
cur -= a[i]
if cur > s[i+1]: return -1
cur = min(cur*2, s[i+1])
return res
print(solve()) | 1 | 18,770,586,496,778 | null | 141 | 141 |
# -*- coding: utf-8 -*-
a = [int(raw_input()) for _ in range(10)]
#print a
b = sorted(a, reverse=True)
#print b
for c in b[0:3]:
print c | import math
S = input()
ans = 0
l = int(math.floor(len(S)/2))
for i in range(l):
if S[i] != S[len(S)-1-i]:
ans += 1
print(ans) | 0 | null | 59,822,351,983,280 | 2 | 261 |
def breathSerch(P,N):
n=len(P)-1
m=1
QQ=[N[1]]
while(QQ != []):
R=[]
for Q in QQ:
for i in range(1,n+1):
if Q[i]==1 and P[i]==-1:
P[i]=m
R.append(N[i])
QQ = R
m=m+1
n = int(input())
A = [[0 for j in range(n+1)] for i in range(n+1)]
for i in range(n):
vec = input().split()
u = int(vec[0])
k = int(vec[1])
nodes = vec[2:]
for i in range(int(k)):
v = int(nodes[i])
A[u][v] = 1
P=[-1 for i in range(n+1)]
P[1]=0
breathSerch(P,A)
for i in range(1,n+1):
print(i,P[i]) | from collections import deque
V = int(input())
edge = [[] for _ in range(V)]
for _ in range(V):
u, _, *v = map(lambda x: int(x)-1, input().split())
edge[u] = sorted(v)
dist = [-1] * V
dist[0] = 0
que = deque([0])
while len(que):
v = que.popleft()
for c in edge[v]:
if dist[c] == -1:
dist[c] = dist[v] + 1
que.append(c)
for i, d in enumerate(dist):
print(i+1, d) | 1 | 3,587,236,470 | null | 9 | 9 |
while True:
try:
a,b = map(int, input().split())
d = 0
s = sum([a,b])
while (s):
s //= 10
d += 1
print(d)
except EOFError: break
| string = input()
a, b, c = map(int, (string.split(' ')))
if a < b & b < c :
print('Yes')
else:
print('No')
| 0 | null | 195,984,001,518 | 3 | 39 |
#!/usr/bin/env python3
def main():
N= int(input())
ans = (N+1)//2/N
print(ans)
if __name__ == "__main__":
main()
| n=int(input())
print(-~n//2/n) | 1 | 177,099,124,625,632 | null | 297 | 297 |
a = sorted([int(input()) for i in range(10)])
a.reverse()
for i in range(3):
print(a[i]) | from sys import stdin
for x in sorted([int(l) for l in stdin],reverse=True)[0:3]:
print(x) | 1 | 20,448,778 | null | 2 | 2 |
S = input()
K = int(input())
ss = []
seq = 1
for a,b in zip(S,S[1:]):
if a==b:
seq += 1
else:
ss.append(seq)
seq = 1
ss.append(seq)
if len(ss)==1:
print(len(S)*K//2)
exit()
if S[0] != S[-1]:
ans = sum([v//2 for v in ss]) * K
print(ans)
else:
ans = sum([v//2 for v in ss[1:-1]]) * K
ans += (ss[0]+ss[-1])//2 * (K-1)
ans += ss[0]//2 + ss[-1]//2
print(ans) | St = len(set(input()))
print('Yes' if St == 2 else 'No') | 0 | null | 114,959,229,204,460 | 296 | 201 |
from sys import stdin
rooms = [[[0] * 10 for _ in range(3)] for _ in range(4)]
stdin.readline().rstrip()
while True:
try:
b, f, r, v = [int(x) for x in stdin.readline().rstrip().split()]
rooms[b-1][f-1][r-1] += v
except:
break
for b in range(4):
for f in range(3):
print(" ", end = "")
print(*rooms[b][f])
else:
if b != 3:
print("#"*20)
| N = int(input())
X = input()
def popcount(x):
tmp = x
count = 0
while tmp > 0:
if tmp & 1 == 1:
count += 1
tmp >>= 1
return count
x = int(X, 2)
pcx = X.count("1")
pc1 = x % (pcx - 1) if pcx > 1 else 0
pc2 = x % (pcx + 1)
for i in range(N):
if X[i] == '0':
X_next = pc2 + pow(2,N-1-i,pcx+1)
X_pop_next = pcx + 1
elif X[i] == '1' and pcx - 1 != 0:
X_next = pc1 - pow(2,N-1-i,pcx-1)
X_pop_next = pcx - 1
else :
print(0)
continue
if X_pop_next == 0:
print(0)
continue
X_next = X_next % X_pop_next
ans = 1
while X_next != 0:
X_next = X_next % popcount(X_next)
ans += 1
print(ans) | 0 | null | 4,634,111,240,512 | 55 | 107 |
#!/usr/bin python3
# -*- coding: utf-8 -*-
h, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
#DP[i] = i までの魔法でモンスターの体力を減らすため消耗する魔力の最小値
dp = [0] * 20001
for i in range(h):
dp[i] = min(dp[i-a] + b for a, b in ab)
print(dp[h-1])
| import sys
input = sys.stdin.readline
H,N = map(int,input().split())
spells = [list(map(int,input().split())) for i in range(N)]
INF = 10**10
dp = [INF]*(H+1)
dp[0] = 0
for use in spells:
damage = use[0]
mp = use[1]
for i in range(1,H+1):
dp[i] = min(dp[max(0,i-damage)] + mp, dp[i])
print(dp[-1])
| 1 | 80,968,990,049,642 | null | 229 | 229 |
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np # cumsum
# from bisect import bisect_left, bisect_right
def solve():
N = II()
A = LI()
memo = {}
ans = 0
for i, a in enumerate(A):
i = i + 1
# print(i, a - i, i - a)
ans = ans + memo.get(i - a, 0)
memo[a + i] = memo.get(a + i, 0) + 1
print(ans)
if __name__ == '__main__':
solve()
| import sys
import math
sys.setrecursionlimit(10**9)
def main():
N = int(input())
A = list(map(int,input().split()))
dic = [{},{}]
counted = [set(),set()]
def count(x,type):
type = type - 1
if x <= 0:
return
if not x in counted[type]:
dic[type][x] = 1
counted[type].add(x)
else:
dic[type][x] += 1
for i in range(N):
type1 = (i+1)-A[i]
type2 = (i+1)+A[i]
count(type1,1)
count(type2,2)
ans = 0
# print(dic,counted)
for index,cnt in dic[0].items():
if index in counted[1]:
ans += cnt * dic[1][index]
print(ans)
if __name__ == "__main__":
main() | 1 | 26,108,557,895,264 | null | 157 | 157 |
a = int(input())
res = 0
for i in range(a+1):
if i % 3 ==0 or i % 5 ==0:
pass
else:
res += i
print(res) | N=int(input())
k=0
for i in range(N+1):
if i%3==0 or i%5==0:
k+=0
else:
k+=i
print(k) | 1 | 34,699,744,237,120 | null | 173 | 173 |
A, B, K = list(map(int, input().split()))
A -= K
if A < 0:
B += A
A = 0
if B < 0:
B = 0
print(A, B)
| import sys
A, B, K = map(int, input().split())
if A >= K:
A -= K
print(A, B)
sys.exit(0)
else:
K -= A
if B >= K:
B -= K
print(0, B)
sys.exit(0)
else:
print(0, 0) | 1 | 103,781,894,614,980 | null | 249 | 249 |
r=int(input())
print(r**2)
| """
author : halo2halo
date : 9, Jan, 2020
"""
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
N = int(readline())
print(int(N**2))
| 1 | 145,865,000,603,878 | null | 278 | 278 |
import sys
for x in sys.stdin:
if "?" in x:
break
print(eval(x.replace("/", "//"))) | n, k = map(int, input().split())
mod = 10**9 + 7
res = 0
z = n*(n+1)//2
for x in range(k, n+2):
min_ = (x-1)*x//2
max_ = z - (n-x)*(n+1-x)//2
res += max_ - min_ + 1
res %= mod
print(res)
| 0 | null | 16,888,302,081,212 | 47 | 170 |
S = list(map(str, input().split()))
count = len(S)
stack = []
for i in range(count):
if S[i] == "+":
b = stack.pop()
a = stack.pop()
stack.append(a + b)
elif S[i] == "-":
b = stack.pop()
a = stack.pop()
stack.append(a - b)
elif S[i] == "*":
b = stack.pop()
a = stack.pop()
stack.append(a * b)
else:
stack.append(int(S[i]))
print(stack[0])
| n = int(input())
mod=10**9+7
if n==1:
print(0)
else:
print(((10**n)-(9**n)-(9**n)+(8**n))%mod)
| 0 | null | 1,617,064,654,784 | 18 | 78 |
# coding: utf-8
N = list(map(int,list(input())))
sN = sum(N)
if sN%9 ==0:
print("Yes")
else:
print("No")
| from sys import stdin
import math
inp = lambda : stdin.readline().strip()
a = list(inp())
a2 = [int(x) for x in a]
if sum(a2) % 9 == 0:
print("Yes")
else:
print("No") | 1 | 4,421,962,834,290 | null | 87 | 87 |
from math import floor
A= input().split()
p = int(A[0])
q = round(float(A[1])*100)
t = p*q
t //= 100
print(t) | N=int(input())
stones = list(input())
l = 0
r = N-1
count = 0
while(True):
while(stones[l] == "R" and l < N-1):
l += 1
while(stones[r] == "W" and r > 0):
r -= 1
if (l >= r):
print(count)
exit()
else:
count += 1
l += 1
r -= 1 | 0 | null | 11,476,072,885,600 | 135 | 98 |
import re
import copy
pattern = r'([0-9])'
repatter = re.compile(pattern)
n = int(input())
text = input()
*lists, = text.split(" ")
lists2 = copy.copy(lists)
lists3 = copy.copy(lists)
# バブルソート
def bubble_sort(a):
flag = 1
i = 0
while flag:
flag = 0
for j in range(n-1, i, -1):
x = repatter.findall(a[j])
y = repatter.findall(a[j-1])
if x < y:
a[j], a[j-1] = a[j-1], a[j]
flag = 1
i += 1
print(*a)
print("Stable")
# 選択ソート
def select_sort(a):
for i in range(n):
min_j = i
for j in range(i+1, n):
x = repatter.findall(a[j])
y = repatter.findall(a[min_j])
if x < y:
min_j = j
a[i], a[min_j] = a[min_j], a[i]
print(*a)
def isStable(before, after):
flag = True
for i in range(n):
for j in range(i+1, n):
for a in range(n):
for b in range(a+1, n):
x = repatter.findall(before[i])
y = repatter.findall(before[j])
if x == y and before[i] == after[b] and before[j] == after[a]:
flag = False
if flag is True:
print("Stable")
else:
print("Not stable")
bubble_sort(lists)
select_sort(lists2)
isStable(lists3, lists2)
| from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 6)
def depth(adj, node, parent=None):
ret = 0
for child in adj[node]:
if child == parent:
continue
d = depth(adj, child, node) + 1
ret = max(ret, d)
return ret
def main():
N, U, V = list(map(int, input().split(' ')))
U -= 1
V -= 1
adj = defaultdict(list)
for _ in range(N - 1):
A, B = list(map(lambda x: int(x) - 1, input().split(' ')))
adj[A].append(B)
adj[B].append(A)
path = [-1] * N
def find_path_on_tree(to_node, node, parent=None, d=0):
if node == to_node:
path[d] = node
return True
for child in adj[node]:
if child == parent:
continue
if not find_path_on_tree(to_node, child, node, d + 1):
continue
path[d] = node
return True
return False
find_path_on_tree(U, V) # to U from V
path = [node for node in path if node != -1]
dist = len(path) - 1
partial_depth = depth(adj, path[(dist // 2) + 1], path[dist // 2])
print(partial_depth + (dist // 2))
if __name__ == '__main__':
main() | 0 | null | 59,007,098,473,490 | 16 | 259 |
N = int(input())
x = []
y = []
for i in range(N):
in1, in2 = list(map(int,input().split()))
x.append(in1)
y.append(in2)
#f = [[0]*2 for i in range(N)]
X=[]
Y=[]
for i in range(N):
#f[i][0] = x[i]-x[i]
#f[i][1] = x[i]+y[i]
X.append(x[i]-y[i])
Y.append(x[i]+y[i])
dam1 = max(X) - min(X)
dam2 = max(Y) - min(Y)
print(max(dam1,dam2)) | class Dice:
def __init__(self, usr_input, command):
self.eyes = list(map(int, usr_input.split()))
self.pos = {label:eye for label, eye in zip(range(1, 7), self.eyes)}
self.com = list(command)
def roll(self):
for c in self.com:
self.eyes = [n for n in self.pos.values()]
if c == 'E':
self.pos[1] = self.eyes[3]
self.pos[3] = self.eyes[0]
self.pos[4] = self.eyes[5]
self.pos[6] = self.eyes[2]
elif c == 'N':
self.pos[1] = self.eyes[1]
self.pos[2] = self.eyes[5]
self.pos[5] = self.eyes[0]
self.pos[6] = self.eyes[4]
elif c == 'S':
self.pos[1] = self.eyes[4]
self.pos[2] = self.eyes[0]
self.pos[5] = self.eyes[5]
self.pos[6] = self.eyes[1]
elif c == 'W':
self.pos[1] = self.eyes[2]
self.pos[3] = self.eyes[5]
self.pos[4] = self.eyes[0]
self.pos[6] = self.eyes[3]
def top_print(self):
print(self.pos.get(1))
if __name__ == '__main__':
dice1 = Dice(input(), input())
dice1.roll()
dice1.top_print() | 0 | null | 1,803,736,024,100 | 80 | 33 |
N = int(input())
s, t = map(str, input().split())
ans = []
for i in range(N):
a = s[i] + t[i]
ans.append(a)
print(''.join(ans)) | n = int(input())
S,T = input().split()
result = ''
for s,t in zip(S,T):
result += s
result += t
print(result) | 1 | 112,214,276,501,628 | null | 255 | 255 |
n=int(input())
m=n//2
l=n-m
answer=l/n
if n==1:
print(1)
else:
print(answer) | N = int(input())
g = 0
for n in range(1, N + 1):
if n % 2 == 0:
g += 1
ANS = (N - g) / N
print(ANS) | 1 | 177,266,036,163,348 | null | 297 | 297 |
x,n = map(int,input().split())
p = list(map(int,input().split()))
i = 0
ans = -1
while True:
temp1 = x - i
temp2 = x + i
if temp1 not in p:
ans = temp1
break
if temp2 not in p:
ans = temp2
break
i += 1
print(ans) | (n,m) = [int(i) for i in input().split()]
A = []
for nc in range(n):
A.append([int(i) for i in input().split()])
b = []
for mc in range(m):
b.append(int(input()))
product = []
for nc in range(n):
total = 0
for mc in range(m):
total += A[nc][mc] * b[mc]
product.append(total)
[print(p) for p in product] | 0 | null | 7,581,325,697,950 | 128 | 56 |
N=int(input())
A=0
for i in range(1,N+1):
if i%3!=0 and i%5!=0:
A=A+i
print(int(A)) | def main():
mod=1000000007
s=int(input())
m=s*2
inv=lambda x: pow(x,mod-2,mod)
Fact=[1] #階乗
for i in range(1,m+1):
Fact.append(Fact[i-1]*i%mod)
Finv=[0]*(m+1) #階乗の逆元
Finv[-1]=inv(Fact[-1])
for i in range(m-1,-1,-1):
Finv[i]=Finv[i+1]*(i+1)%mod
def comb(n,r):
if n<r:
return 0
return Fact[n]*Finv[r]*Finv[n-r]%mod
ans=0
num=s//3
for i in range(1,num+1):
n=s-i*3
ans+=comb(n+i-1,n)
ans%=mod
print(ans)
if __name__=='__main__':
main() | 0 | null | 19,064,836,171,260 | 173 | 79 |
N=int(input())
A=list(map(int, input().split()))
dp = [[0 for i in range(2)] for j in range(N+1)]
if N in [2, 3]: print(max(A))
else:
dp[2][0] = A[1]
dp[2][1] = A[0]
dp[3][0] = A[2]
dp[3][1] = max(A[1], A[0])
for i in range(4, N+1):
if i % 2 == 0:
dp[i][0] = max(max(dp[i-2][0], dp[i-2][1])+A[i-1], dp[i-1][1]+A[i-1])
dp[i][1] = dp[i-2][1]+A[i-2]
else:
dp[i][0] = max(dp[i-2][0], dp[i-2][1])+A[i-1]
dp[i][1] = max(dp[i-1][0], dp[i-1][1])
print(max(dp[-1][0], dp[-1][1])) | #!/usr/bin/env python
# encoding: utf-8
class Solution:
"""
@param prices: Given an integer array
@return: Maximum profit
"""
@staticmethod
def bubble_sort():
# write your code here
array_length = int(input())
unsorted_array = [int(x) for x in input().split()]
flag = 1
count = 0
while flag:
flag = 0
for j in range(array_length - 1, 0, -1):
if unsorted_array[j] < unsorted_array[j - 1]:
unsorted_array[j], unsorted_array[j - 1] = unsorted_array[j - 1], unsorted_array[j]
flag = 1
count += 1
print(" ".join(map(str, unsorted_array)))
print(str(count))
if __name__ == '__main__':
solution = Solution()
solution.bubble_sort() | 0 | null | 18,861,233,373,180 | 177 | 14 |
a, b, c = map(int, input().split())
k = int(input())
if a >= b:
while a >= b:
if k == 0:
print("No")
exit()
b *= 2
k -= 1
if b >= c:
while b >= c:
if k == 0:
print("No")
exit()
c *= 2
k -= 1
print("Yes")
| a, b, c = map(int, input().split())
k = int(input())
p = "No"
for i in range(k):
if not(a < b and b < c):
if b <= a:
b = b * 2
else:
c = c * 2
if a < b and b < c:
p = "Yes"
print(p) | 1 | 6,935,453,603,584 | null | 101 | 101 |
n=int(input())
rng = [[] for _ in range(n)]
for i in range(n):
x,l=map(int,input().split())
rng[i] = [x-l,x+l]
rng.sort(key=lambda y:y[1])
cnt=0
t_mx=-(10**10)
for i in range(n):
if t_mx > rng[i][0]:
cnt += 1
else:
t_mx = rng[i][1]
print(n-cnt) | def solve(XL, N):
XL.sort(key=lambda x:x[1])
count = N
for i in range(1, N):
if XL[i][0] < XL[i-1][1]:
XL[i][1] = XL[i-1][1]
count -= 1
return count
N = int(input())
XL = []
for i in range(N):
x, l = map(int, input().split(' '))
XL.append([x-l, x+l])
print(solve(XL, N)) | 1 | 89,621,557,067,702 | null | 237 | 237 |
length = int(input())
first_string, second_string = input().split()
first_list = list(first_string)
second_list = list(second_string)
result_list = []
for i in range(length):
result_list.append(first_list[i])
result_list.append(second_list[i])
result = "".join(result_list)
print(result)
| import sys
N = int(input())
S,T = input().split()
if not ( 1 <= N <= 100 ): sys.exit()
if not ( len(S) == len(T) and len(S) == N ): sys.exit()
if not ( S.islower() and T.islower() ): sys.exit()
for I in range(N):
print(S[I],end='')
print(T[I],end='') | 1 | 111,735,722,575,324 | null | 255 | 255 |
import sys
import numpy as np
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
N, T = lr()
AB = [lr() for _ in range(N)]
AB.sort()
# 時間のかかる物は後ろへ
dp = np.zeros(T, np.int64)
answer = 0
for a, b in AB:
answer = max(answer, dp.max() + b)
prev = dp
dp[a:] = np.maximum(prev[a:], prev[:-a] + b)
print(answer)
| import sys
from collections import defaultdict
from queue import Queue
def main():
input = sys.stdin.buffer.readline
n = int(input())
g = defaultdict(list)
for i in range(n):
line = list(map(int, input().split()))
if line[1] > 0:
g[line[0]] = line[2:]
q = Queue()
q.put(1)
dist = [-1] * (n + 1)
dist[1] = 0
while not q.empty():
p = q.get()
for nxt in g[p]:
if dist[nxt] == -1:
dist[nxt] = dist[p] + 1
q.put(nxt)
for i in range(1, n + 1):
print(i, dist[i])
if __name__ == "__main__":
main()
| 0 | null | 75,576,255,543,420 | 282 | 9 |
import sys
def solve():
a, b = map(int, input().split())
ans = gcd(a, b)
print(ans)
def gcd(a, b):
return gcd(b, a % b) if b else a
if __name__ == '__main__':
solve() | class Dice(object):
def __init__(self, n):
self.n = n
def move(self, to):
if to == 'N':
self.n[0], self.n[1], self.n[4], self.n[5] = self.n[1], self.n[5], self.n[0], self.n[4]
elif to == 'E':
self.n[0], self.n[2], self.n[3], self.n[5] = self.n[3], self.n[0], self.n[5], self.n[2]
elif to == 'S':
self.n[0], self.n[1], self.n[4], self.n[5] = self.n[4], self.n[0], self.n[5], self.n[1]
elif to == 'W':
self.n[0], self.n[2], self.n[3], self.n[5] = self.n[2], self.n[5], self.n[0], self.n[3]
def top(self):
return self.n[0]
dice = Dice([int(i) for i in input().split()])
move = input()
for m in move:
dice.move(m)
print(dice.top())
| 0 | null | 124,859,342,050 | 11 | 33 |
N = int(input())
A = list(map(int, input().split()))
y = sorted([(i+1, A[i]) for i in range(N)], key=lambda x: x[1])
print(" ".join(map(str, [z[0] for z in y])))
| rooms = [0] * (4*3*10)
count = int(input())
for i in range(count):
b, f, r, v = [int(x) for x in input().split()]
rooms[30*(b-1) + 10*(f-1) + (r-1)] += v
for i, room in enumerate(rooms, start=1):
print('', room, end='')
if i%10 == 0:
print()
if i%30 == 0 and i%120 != 0:
print('#'*20) | 0 | null | 91,023,171,187,252 | 299 | 55 |
import sys
input = sys.stdin.readline
def main():
from collections import deque
from collections import defaultdict
n=int(input())
ab=[list(map(int,input().split())) for _ in range(n-1)]
g=[[] for _ in range(n)]
for i,abi in enumerate(ab):
a,b=abi
g[b-1].append(a-1)
g[a-1].append(b-1)
todo=deque([(0,-1)])
dc={}
while len(todo)>0:
a,pc=todo.popleft()
l=g[a]
c=0
for li in l:
d,e=min(a,li),max(a,li)
if (d,e) not in dc:
c+=1 if c+1!=pc else 2
dc[(d,e)]=c
todo.append([li,c])
print(max(dc.values()))
for a,b in ab:
print(dc[(a-1,b-1)])
if __name__=='__main__':
main()
| n=int(input())
A=list(map(int,input().split()))
x=1
A.sort()
if 0 in A:
print(0)
exit()
for i in range(n):
x*=A[n-i-1]
if x>10**18:
print(-1)
exit()
else:
continue
print(x) | 0 | null | 76,043,438,394,570 | 272 | 134 |
# 解説を参考に作成
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
import math
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, K, As):
ans = max(As)
l, r = 1, ans
for _ in range(30):
m = (l + r) // 2
cnt = 0
for A in As:
cnt += math.ceil(A / m) - 1
if cnt <= K:
ans = min(ans, m)
r = m - 1
else:
l = m + 1
if l > r:
break
print(ans)
if __name__ == '__main__':
# S = input()
# N = int(input())
N, K = map(int, input().split())
As = [int(i) for i in input().split()]
# Bs = [int(i) for i in input().split()]
solve(N, K, As)
| n=int(input())
a=[int(i) for i in input().split()]
flag=0
for i in range(0,len(a)):
if(a[i]%2==0 and (a[i]%3==0 or a[i]%5==0)):
pass
elif(a[i]%2==0 and (a[i]%3!=0 or a[i]%5!=0)):
flag=1
break
if(flag==1):
print('DENIED')
else:
print('APPROVED') | 0 | null | 37,655,115,225,092 | 99 | 217 |
import math
A,B=map(int,input().split())
print(A*B//math.gcd(A,B)) | import math
a,b = map(int,input().split())
print((a*b)//math.gcd(a,b)) | 1 | 113,902,180,968,896 | null | 256 | 256 |
for i in range ( int ( input ( ) ) ):
( a, b, c ) = sorted ( list ( map ( int, input ( ).split ( ) ) ) )
if ( a ** 2 ) + ( b ** 2 ) == ( c ** 2 ):
print ( "YES" )
else:
print ( "NO" ) | class Queue():
def __init__(self, size=10):
self.queue = [None] * size
self.head = self.tail = 0
self.MAX = size
self.num_items = 0
def is_empty(self):
return self.num_items == 0
def is_full(self):
return self.num_items == self.MAX
def enqueue(self, x):
if self.is_full():
raise IndexError
self.queue[self.tail] = x
self.num_items += 1
if self.tail+1 == self.MAX:
self.tail = 0
else:
self.tail += 1
def dequeue(self):
if self.is_empty():
raise IndexError
x = self.queue[self.head]
self.num_items -= 1
if self.head+1 == self.MAX:
self.head = 0
else:
self.head += 1
return x
class Process:
def __init__(self, name, time):
self.name = name
self.time = time
def print_time(self):
return self.time
def print_name(self):
return self.name
def main():
n, q = map(int, input().split())
qq = Queue(n)
t = 0
for i in range(n):
name, time = input().split()
p = Process(name, int(time))
qq.enqueue(p)
while not qq.is_empty():
p = qq.dequeue()
if p.time - q <= 0:
t += p.time
print("{} {}".format(p.name, t))
else:
p.time -= q
t += q
qq.enqueue(p)
if __name__ == '__main__':
main()
| 0 | null | 21,625,563,300 | 4 | 19 |
import math
y=int(input())
cnt=0
for i in range (1,y+1):
for k in range(1,y+1):
for j in range(1,y+1):
a = [i,j,k]#リスト
ans = a[0]
for x in range(len(a)):
ans = math.gcd(ans, a[x])
cnt+=ans
print(cnt) | #!/usr/bin/env python3
# coding: utf-8
import collections
def debug(arg):
if __debug__:
pass
else:
import sys
print(arg, file=sys.stderr)
def main():
pass
N, *A = map(int, open(0).read().split())
a = dict(enumerate(A, 1))
dp = collections.defaultdict(lambda: -float("inf"))
dp[0, 0] = 0
dp[1, 0] = 0
dp[1, 1] = a[1]
for i in range(2, N + 1):
jj = range(max(i // 2 - 1, 1), (i + 1) // 2 + 1)
for j in jj:
x = dp[i - 2, j - 1] + a[i]
y = dp[i - 1, j]
dp[i, j] = max(x, y)
print(dp[N, N // 2])
if __name__ == "__main__":
main() | 0 | null | 36,499,217,348,528 | 174 | 177 |
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT() : return int(input())
def MAP() : return map(int,input().split())
def LIST() : return list(MAP())
n = INT()
a = LIST()
ans = 1
r = 0
g = 0
b = 0
for x in a:
ans = ans * ( (r == x) + (g == x) + (b == x) ) % (10**9+7)
if r == x:
r += 1
elif g == x:
g += 1
else:
b += 1
print(ans) | N, K = map(int, input().split())
P = list(map(int, input().split()))
res = 0
P2 = []
for i in P:
P2.append(0.5 * (i + 1))
num = 0
for i in range(K):
num += P2[i]
res = num
for i in range(1,N-K+1):
temp = num + P2[i+K-1] - P2[i-1]
if temp > res:
res = temp
num = temp
print(res) | 0 | null | 102,725,124,534,940 | 268 | 223 |
m=[]
for i in range(10):m.append(int(input()))
m.sort(reverse=True)
for i in range(3):print(m[i])
| def qsort(l):
if l == []: return []
else:
pv = l[0]
left = []
right = []
for i in range(1, len(l)):
if l[i] > pv:
left.append(l[i])
else:
right.append(l[i])
left = qsort(left)
right = qsort(right)
left.append(pv)
return left + right
def main():
lst = []
for i in range(0, 10):
lst.append(int(raw_input()))
lst = qsort(lst)
for i in range(0, 3):
print(lst[i])
main() | 1 | 17,969,920 | null | 2 | 2 |
N = int(input())
def calc_divisor(x):
divisor = []
for i in range(1, int(x ** 0.5) + 1):
if x % i == 0:
if i != 1:
divisor.append(i)
if i != x // i:
divisor.append(x // i)
return divisor
cand = len(calc_divisor(N - 1))
divisor_n = calc_divisor(N)
for v in divisor_n:
x = N
while x % v == 0:
x //= v
if x % v == 1:
cand += 1
print(cand)
| X = int(input())
x = 360
while True:
if x % X == 0:
print (x // X)
break
x += 360 | 0 | null | 27,028,365,547,670 | 183 | 125 |
s = input()
k = int(input())
if len(set(s)) == 1:
print((len(s)*k)//2)
else:
t = [1]
for i in range(1, len(s)):
if s[i-1] == s[i]:
t[-1] += 1
else:
t.append(1)
a = 0
for i in t:
a += (i//2)*k
if s[0] == s[-1]:
if t[0] % 2 == t[-1] % 2 == 1:
a += k-1
print(a) | n = int(input())
a = list(map(int, input().split()))
q = int(input())
s = sum(a)
arr = [0] * (10**5 + 1)
for i in range(n):
arr[a[i]] += 1
ans = []
for i in range(q):
b, c = map(int, input().split())
nb = arr[b]
bc = arr[c]
s -= b * nb
s += c * nb
arr[b] = 0
arr[c] += nb
ans.append(s)
for i in range(q):
print(ans[i]) | 0 | null | 93,618,377,861,748 | 296 | 122 |
import sys
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())
def main():
n, k = mi()
p = list(map(lambda x: int(x)-1, input().split()))
c = li()
ans = -10**18
for i in range(n):
v = i
cycle_cnt = 0
cycle_sum = 0
while True:
cycle_cnt += 1
cycle_sum += c[v]
v = p[v]
if v == i:
break
cnt = 0
tmp = 0
while True:
tmp += c[v]
cnt += 1
if cnt > k:
break
r = (k-cnt)//cycle_cnt
score = tmp+r*max(0, cycle_sum)
ans = max(ans, score)
v = p[v]
if v == i:
break
print(ans)
if __name__ == '__main__':
main() | import math
H,W=map(int,input().split())
if W==1:
print(1)
elif H==1:
print(1)
else:
A=math.ceil(H*W/2)
print(A) | 0 | null | 27,945,272,749,318 | 93 | 196 |
N, K, S = map(int,input().split())
L = [S]*K
for i in range(N-K):
if S < 10**9:
L.append(S+1)
else:
L.append(1)
print(' '.join(map(str, L))) | N, K, S = map(int, input().split())
if S == 10**9:
Ans = [S for _ in range(K)] + [1 for _ in range(N-K)]
else:
Ans = [S for _ in range(K)] + [S+1 for _ in range(N-K)]
for ans in Ans:
print(ans, end=' ') | 1 | 91,198,087,235,804 | null | 238 | 238 |
n = int(input())
t_score = 0
h_score = 0
for i in range(n):
t_card, h_card = input().split()
if t_card < h_card:
h_score += 3
elif t_card > h_card:
t_score += 3
else:
h_score += 1
t_score += 1
print(t_score, h_score)
| import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
def calcScore(t, s, c):
scores = [0]*26
lasts = [0]*26
for i in range(1, len(t)):
scores[t[i]] += s[i][t[i]]
dif = i - lasts[t[i]]
scores[t[i]] -= c[t[i]] * dif * (dif-1) // 2
lasts[t[i]] = i
for i in range(26):
dif = len(t) - lasts[i]
scores[i] -= c[i] * dif * (dif-1) // 2
return scores
def update(socre, day, t, q):
p = t[day]
t[day] = q
scores[p] = 0
scores[q] = 0
lasts = [0]*26
for i in range(1, len(t)):
if t[i] == p or t[i] == q:
scores[t[i]] += s[i][t[i]]
dif = i - lasts[t[i]]
scores[t[i]] -= c[t[i]] * dif * (dif-1) // 2
lasts[t[i]] = i
for i in [p, q]:
dif = len(t) - lasts[i]
scores[i] -= c[i] * dif * (dif-1) // 2
return scores
def greedy(c, s):
day_lim = len(s)
socres = [0]*26
t = [0]*day_lim
lasts = [0]*26
for i in range(1, day_lim):
pls = [v for v in socres]
mns = [v for v in socres]
for j in range(26):
pls[j] += s[i][j]
mns[j] -= c[j] * (i - lasts[j])
sum_mns = sum(mns)
pt = sum_mns - mns[0] + pls[0]
idx = 0
for j in range(1, 26):
tmp = sum_mns - mns[j] + pls[j]
if pt < tmp:
pt = tmp
idx = j
t[i] = idx
lasts[idx] = i
for j in range(26):
if j == idx:
socres[j] = pls[j]
else:
socres[j] = mns[j]
return socres, t
D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
s[i] = list(map(int, input().split()))
scores, t = greedy(c, s)
for v in t[1:]:
print(v+1) | 0 | null | 5,800,124,949,530 | 67 | 113 |
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
s = input()
if 'RRR' in s:
print(3)
elif 'RR' in s:
print(2)
elif 'R' in s:
print(1)
else:
print(0)
if __name__ == '__main__':
main() | n = int(input())
ans = list()
for i in range(n):
line = input().split(" ")
a = float(line[0])
b = float(line[1])
c = float(line[2])
if ((a**2 + b**2) == c**2) or ((b**2 + c**2) == a**2) or ((c**2 + a**2) == b**2):
ans.append("YES")
else:
ans.append("NO")
for j in range(n):
print(ans[j]) | 0 | null | 2,427,268,556,230 | 90 | 4 |
n=int(input())
s=[]
for i in range(n):
s.append(str(input()))
s=set(s)
print(len(s)) | # -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split()))
ans = "APPROVED"
for a in A:
if a % 2 == 0:
if a % 3 == 0 or a % 5 == 0:
pass
else:
ans = "DENIED"
break
print(ans) | 0 | null | 50,009,969,092,628 | 165 | 217 |
input = input()
W,H,x,y,r = map(int ,input.split())
if x - r < 0 or x + r > W or y - r < 0 or y + r > H:
print("No")
if x - r >= 0 and x + r <= W and y - r >= 0 and y + r <= H:
print("Yes")
| N = int(input())
evidences = [[] for _ in range(N)]
for i in range(N):
A = int(input())
for _ in range(A):
x, y = map(int, input().split())
evidences[i].append((x - 1, y))
ans = 0
for i in range(1, 2 ** N):
consistent = True
for j in range(N):
if (i >> j) & 1 == 0:
continue
for x, y in evidences[j]:
if (i >> x) & 1 != y:
consistent = False
break
#if not consistent:
#break
if consistent:
ans = max(ans, bin(i)[2:].count('1'))
print(ans) | 0 | null | 61,247,137,094,352 | 41 | 262 |
N,M=map(int, input().split())
print("Yes" if N==M else "No") | import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
n,k = list(map(int, input().split()))
ans = 0
M = 10**9+7
for i in range(k, n+2):
ans += (n-i+1+n)*(i)//2 - i*(i-1)//2 + 1
ans %= M
# print(i,ans)
print(ans%M) | 0 | null | 58,079,027,738,252 | 231 | 170 |
n = int(input())
dic = {}
total = 0
for _ in range(n):
s, t = input().split()
total += int(t)
dic[s] = total
x = input()
total -= dic[x]
print(total) | n=int(input())
s=[]
t=[]
for i in range(n):
st,tt=input().split()
s.append(st)
t.append(int(tt))
x=str(input())
temp=0
ans=sum(t)
for i in range(n):
temp=temp+t[i]
if s[i]==x:
break
print(ans-temp) | 1 | 96,843,542,033,040 | null | 243 | 243 |
def solve(n, m, s):
if s.find("1"*m) >= 0:
return -1
s = s[::-1]
i = 0
path = []
while i < n:
for j in range(min(m, n-i), 0, -1):
ni = i+j
if s[ni] == "0":
i = ni
path.append(j)
break
return " ".join(map(str, path[::-1]))
n, m = map(int, input().split())
s = input()
print(solve(n, m, s)) | import sys
#input = sys.stdin.buffer.readline
def main():
N,M = map(int,input().split())
s = str(input())
s = list(reversed(s))
ans = []
now = 0
while now < N:
for i in reversed(range(min(M,N-now))):
i += 1
if s[now+i] == "0":
now += i
ans.append(i)
break
if i == 1:
print(-1)
exit()
print(*reversed(ans))
if __name__ == "__main__":
main()
| 1 | 139,365,539,700,268 | null | 274 | 274 |
import math
A, B, C, D = map(int, input().split())
if math.ceil(C/B) <= math.ceil(A/D):
print('Yes')
else:
print('No')
| S=list(map(int,input().split()))
while True:
S[2]=S[2]-S[1]
S[0]=S[0]-S[3]
if S[2]<=0:
print("Yes")
break
elif S[0]<=0:
print("No")
break | 1 | 29,881,113,906,448 | null | 164 | 164 |
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
A = [a for a, b in AB]
B = [b for a, b in AB]
A.sort()
B.sort()
if N % 2:
ma = B[N // 2]
mi = A[N // 2]
else:
ma = B[N // 2 - 1] + B[N // 2]
mi = A[N // 2 - 1] + A[N // 2]
print(ma - mi + 1) | import math
a, b, x = map(int, input().split())
c = x/a**2 # c : height of water
if c >= b/2:
tanth = 2*(b-c)/a
deg = math.atan(tanth)*180/math.pi
else:
tanth = b**2/(2*a*c)
deg = math.atan(tanth)*180/math.pi
print(deg) | 0 | null | 90,141,275,733,586 | 137 | 289 |
import math
a,b,C = tuple(float(n) for n in input().split())
C = math.radians(C)
S = a*b*math.sin(C) / 2
L = a + b + math.sqrt(a**2+b**2-2*a*b*math.cos(C))
h = b*math.sin(C)
print("{:.8f}\n{:.8f}\n{:.8f}".format(S,L,h))
| import math
a,b, A = map(float, input().split())
h = b*math.sin(math.radians(A))
S = a*h*0.5
L = a + b + math.sqrt(pow(a, 2) + pow(b, 2) - 2*a*b*math.cos(math.radians(A)))
print(S)
print(L)
print(h)
| 1 | 172,942,674,802 | null | 30 | 30 |
N, M = map(int, input().split())
odd_head, odd_tail = 1, 0
even_head, even_tail = 0, 0
if M & 1:
odd_tail = M + 1
even_head = M + 2
even_tail = M + 2 + M - 1
else:
odd_tail = 1 + (M - 1)
even_head = M + 1
even_tail = M + 1 + M
while odd_tail - odd_head > 0:
print(odd_head, odd_tail)
odd_head += 1
odd_tail -= 1
while even_tail - even_head > 0:
print(even_head, even_tail)
even_head += 1
even_tail -= 1 | from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import median
INF = float("inf")
def mycol(data,col):
return [ row[col] for row in data ]
def mysort(data,col,reverse_flag):
data.sort(key=lambda x:x[col],reverse=reverse_flag)
return data
def mymax(data):
M = -1*float("inf")
for i in range(len(data)):
m = max(data[i])
M = max(M,m)
return M
def mymin(data):
m = float("inf")
for i in range(len(data)):
M = min(data[i])
m = min(m,M)
return m
def mycount(ls,x):
# lsはソート済みであること
l = bisect_left(ls,x)
r = bisect_right(ls,x)
return (r-l)
def myoutput(ls,space=True):
if space:
if len(ls)==0:
print(" ")
elif type(ls[0])==str:
print(" ".join(ls))
elif type(ls[0])==int:
print(" ".join(map(str,ls)))
else:
print("Output Error")
else:
if len(ls)==0:
print("")
elif type(ls[0])==str:
print("".join(ls))
elif type(ls[0])==int:
print("".join(map(str,ls)))
else:
print("Output Error")
def I():
return int(input())
def MI():
return map(int,input().split())
def RI():
return list(map(int,input().split()))
def CI(n):
return [ int(input()) for _ in range(n) ]
def LI(n):
return [ list(map(int,input().split())) for _ in range(n) ]
def S():
return input()
def MS():
return input().split()
def RS():
return list(input().split())
def CS(n):
return [ input() for _ in range(n) ]
def LS(n):
return [ list(input().split()) for _ in range(n) ]
n = I()
ab = LI(n)
a = mycol(ab,0)
ma = median(a)
b = mycol(ab,1)
mb = median(b)
if n%2==1:
ans = mb - ma + 1
else:
ans = (mb-ma)*2 + 1
print(int(ans)) | 0 | null | 22,984,776,408,100 | 162 | 137 |
from functools import lru_cache
@lru_cache(None)
def f(n,k):
if n<10:
if k<1: return 1
if k<2: return n
return 0
d,m=divmod(n,10)
c=0
if k:
c+=f(d,k-1)*m
c+=f(d-1,k-1)*(9-m)
c+=f(d,k)
return c
print(f(int(input()),int(input()))) | def f(N,K):
n=len(str(N))
if n==1:
if K==1:
return N
else:
return 0
elif n==2:
if K==1:
return 9+N//10
elif K==2:
return N-9-N//10
else:
return 0
elif n==3:
if K==1:
return 18+N//100
elif K==2:
return 81+18*((N//100)-1)+f(N%100,1)
else:
return 81*((N//100)-1)+f(N%100,2)
else:
if K==1:
return 9*(n-1)+N//(10**(n-1))
elif K==2:
return 81*(n-1)*(n-2)/2+9*(n-1)*((N//((10**(n-1))))-1)+f(N%(10**(n-1)),1)
else:
return 243*(n**3-6*(n**2)+11*n-6)/2+81*(n-1)*(n-2)*((N//(10**(n-1)))-1)/2+f(N%(10**(n-1)),2)
N=int(input())
K=int(input())
print(int(f(N,K))) | 1 | 75,801,848,517,958 | null | 224 | 224 |
# coding:utf-8
# ??\???
n,m = map(int, input().split())
matrix = []
for i in range(n):
matrix.append([0] * m)
for i in range(n):
col = input().split()
for j in range(m):
matrix[i][j] = int(col[j])
vector = [0] * m
for j in range(m):
vector[j] = int(input())
for i in range(n):
row_sum = 0
for j in range(m):
row_sum += matrix[i][j] * vector[j]
print(row_sum) | import sys
sys.setrecursionlimit(10**6)
def root(x):
if pair[x]<0:
return x
else:
tmp=root(pair[x])
pair[x]=tmp
return tmp
def unite(x,y):
x=root(x)
y=root(y)
if x==y:return False
if pair[x]>pair[y]:
x,y=y,x
pair[x]+=pair[y]
pair[y]=x
return True
def size(x):
return -pair[root(x)]
icase=0
if icase==0:
n,m,k=map(int,input().split())
g=[[] for i in range(n)]
x=[[] for i in range(n)]
a=[0]*m
b=[0]*m
pair=[-1]*n
for i in range(m):
ai,bi=map(int,input().split())
g[ai-1].append(bi-1)
g[bi-1].append(ai-1)
a[i]=ai-1
b[i]=bi-1
unite(a[i],b[i])
for i in range(k):
ci,di=map(int,input().split())
x[ci-1].append(di-1)
x[di-1].append(ci-1)
for i in range(n):
ss=size(i)-1
rooti=root(i)
for gi in g[i]:
if root(gi)==rooti:
ss-=1
for xi in x[i]:
if root(xi)==rooti:
ss-=1
print(ss,end=" ")
print(" ")
| 0 | null | 31,484,930,399,890 | 56 | 209 |
a,b = map(int, raw_input().split())
print a/b,a%b,"%.5f" % (a/float(b)) | a = []
while True:
d = list(map(int,input().split()))
if d == [-1,-1,-1]:
break
a.append(d)
def Point2Grade(d):
m,f,r = d
if (m==-1)|(f==-1):
return 'F'
elif m+f >= 80:
return 'A'
elif m+f >= 65:
return 'B'
elif m+f >= 50:
return 'C'
elif m+f >= 30:
if r >=50: return 'C'
else: return 'D'
else: return 'F'
for x in a:
print(Point2Grade(x)) | 0 | null | 914,340,914,170 | 45 | 57 |
# A - Circle Pond
R = int(input())
print(2*3.14159265359*R) | import math
n = int(input())
print(2 * n * math.pi) | 1 | 31,409,904,917,790 | null | 167 | 167 |
N = int(input())
S, T = input().split()
S_list = list(S)
T_list = list(T)
ans = ""
for i, j in zip(S_list, T_list):
ans += i + j
print(ans)
| n = int(input())
dic = {}
total = 0
for _ in range(n):
s, t = input().split()
total += int(t)
dic[s] = total
x = input()
total -= dic[x]
print(total) | 0 | null | 104,411,860,702,028 | 255 | 243 |
a,b = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
ans = [10**9 for _ in range(a+1)]
ans[0] = 0
for i in range(a+1):
for j in l:
if i + j < a+1:
ans[i+j] = min(ans[i] + 1,ans[i+j])
print(ans[-1])
| N,K=map(int,input().split())
R,S,P=map(int,input().split())
T =input()
com = [0]*N
for i in range(N):
if i>=K and T[i]==T[i-K] and com[i-K]!=0:
continue
elif T[i] =="r":
com[i] =P
elif T[i] =="s":
com[i] =R
else:
com[i] =S
print(sum(com))
| 0 | null | 53,382,981,216,642 | 28 | 251 |
from collections import Counter
def main():
n = int(input())
s = input()
cnt = Counter(s)
ans = cnt["R"] * cnt["G"] * cnt["B"]
for gap in range(1, (n - 1) // 2 + 1):
for i in range(n - gap * 2):
if set([s[i], s[i + gap], s[i + 2 * gap]]) == {"R", "G", "B"}:
ans -= 1
print(ans)
if __name__ == "__main__":
main()
| from itertools import groupby
from statistics import median
from sys import exit
N = int(input())
line = ([(index, v) for index, v in enumerate(input())])
def types(line): return line[1]
def index(line): return line[0]
gp = groupby(sorted(line, key=types), types)
d = {}
for k, v in gp:
indexes = set([i for i, value in v])
d[k] = indexes
if 'R' not in d or 'G' not in d or 'B' not in d:
print(0)
exit()
delete = 0
for differ in range(1, N):
for start in range(N):
if start + differ*2 > N - 1:
break
i = line[start]
j = line[start + differ]
k = line[start + differ*2]
if len({i[1], j[1], k[1]}) != 3:
continue
upper = max(i[0], j[0], k[0])
lower = min(i[0], j[0], k[0])
median_num = median([i[0], j[0], k[0]])
if upper - median_num == median_num - lower:
delete += 1
print(len(d['R']) * len(d['G']) * len(d['B']) - delete)
| 1 | 36,113,907,517,380 | null | 175 | 175 |
# -*- coding: utf-8 -*-
a = [int(raw_input()) for _ in range(10)]
#print a
b = sorted(a, reverse=True)
#print b
for c in b[0:3]:
print c | a = [0, 0, 0, 0]
for t in range(10):
a[3] = input()
for i in range(2, -1, -1):
if (a[i] < a[i+1]):
a[i], a[i+1] = a[i+1], a[i]
for i in range(0, 3):
print a[i] | 1 | 18,057,662 | null | 2 | 2 |
while 1:
w,h=map(int,raw_input().split());a,b="#\n"
if w==0:break
print(a*h+b)*(w) | input_n = input()
n = str(input_n)
listN = list(n)
#listN = n.split(' ')
if listN[len(listN) - 1] == "s":
listN.append("e")
listN.append("s")
else:
listN.append("s")
for i in range(len(listN)):
print(listN[i], end = "")
| 0 | null | 1,576,778,611,528 | 49 | 71 |
L = raw_input().split()
a = int(L[0])
b = int(L[1])
d = int(0)
r = int(0)
f = float(0)
d = a / b
r = a % b
f = float(a) / float(b)
print "{0} {1} {2:f}".format(d,r,f) | from math import gcd
K = int(input())
ans = 0
for i in range(1, K+1):
for j in range(i+1, K+1):
for k in range(j+1, K+1):
ans += gcd(gcd(i, j), k)*6
for i in range(1, K+1):
for j in range(i+1, K+1):
ans += gcd(i, j)*6
ans += K*(K+1)//2
print(ans)
| 0 | null | 18,062,747,792,050 | 45 | 174 |
from math import *
a,b,c = map(int,raw_input().split())
sinc = sin(radians(c))
cosc = cos(radians(c))
S = 0.5 * a * b * sinc
L = a+b+sqrt(a**2+b**2-2*a*b*cosc)
h = b*sinc
print S
print L
print h | import math
a, b, C=map(int,input().split())
S=a*b*math.sin(math.pi*C/180)/2
c=math.sqrt(a**2+b**2-2*a*b*math.cos(math.pi*C/180))
L=a+b+c
h=2*S/a
print('{:.4f}'.format(S))
print('{:.4f}'.format(L))
print('{:.4f}'.format(h))
| 1 | 181,168,422,030 | null | 30 | 30 |
#!/usr/bin/env python3
from networkx.utils import UnionFind
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
n,m=map(int, input().split())
uf = UnionFind()
for i in range(1,n+1):
_=uf[i]
for _ in range(m):
a,b=map(int, input().split())
uf.union(a, b) # aとbをマージ
print(len(list(uf.to_sets()))-1)
#for group in uf.to_sets(): # すべてのグループのリストを返す
#print(group)
if __name__ == '__main__':
main()
| import collections
N,M = map(int,input().split())
road = [[] for _ in range(N+1)]
for _ in range(M):
A,B = map(int,input().split())
road[A].append(B)
road[B].append(A)
no_visit =set(range(1,N+1))
q = collections.deque([])
cnt = 0
while no_visit:
q.append(no_visit.pop())
cnt +=1
while q:
now = q.popleft()
for nxt in road[now]:
if nxt in no_visit:
q.append(nxt)
no_visit.discard(nxt)
print(cnt-1)
| 1 | 2,286,428,994,880 | null | 70 | 70 |
import sys
sys.setrecursionlimit(1000000000)
from itertools import count
from functools import lru_cache
from collections import defaultdict
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
def meg(f, ok, ng):
while abs(ok-ng)>1:
mid = (ok+ng)//2
if f(mid):
ok=mid
else:
ng=mid
return ok
#
def main():
T1,T2 = mis()
A1,A2 = mis()
B1,B2 = mis()
d1 = T1*A1 - T1*B1
d2 = T2*A2 - T2*B2
if d1 + d2 == 0:
print('infinity')
elif d1 < 0 > d2 or d1 > 0 < d2:
print(0)
else:
if d1 < 0:
d1 *= -1
d2 *= -1
if d1 + d2 > 0:
print(0)
elif d1 % (d1+d2) == 0:
print(d1 // abs(d1+d2) * 2)
else:
print(d1 // abs(d1+d2) * 2 + 1)
main()
| N = input()
INF = 10 ** 9
N = N[::-1] + '0'
dp = [[INF for _ in range(2)] for _ in range(len(N) + 1)]
dp[0][0] = 0
for i in range(len(N)):
for j in range(2):
x = int(N[i])
x += j
for k in range(10):
nexti = i + 1
nextj = 0
b = k - x
if b < 0:
nextj = 1
b += 10
dp[nexti][nextj] = min(dp[nexti][nextj], dp[i][j] + k + b)
print(dp[len(N)][0]) | 0 | null | 101,389,264,258,710 | 269 | 219 |
from math import *
from collections import defaultdict
GLOBAL = {}
def roots(n):
res = set()
for i in range(1,int(sqrt(n))+1):
if n%i == 0:
res.add(i)
res.add(n//i)
return len(res)
n = int(input())
cnt = 0
for i in range(1,n+1):
y = n//i
cnt+=((y)*(y+1)*(i))//2
print(cnt)
| N=int(input())
ans=0
for num in range(1,N+1):
count=N//num
ans+=count*(count+1)//2*num
print(ans) | 1 | 11,100,913,227,988 | null | 118 | 118 |
import sys
a = [0]*26
s = sys.stdin.read().lower()
for i in map(chr, range(ord('a'), ord('z')+1)):
print(i, ':', s.count(i))
| #!/usr/bin/python3
cmdvar_numlist=input()
cmdvar_spritd=cmdvar_numlist.split()
D,T,S=list(map(int,cmdvar_spritd))
if D/S<=T:
print("Yes")
else:
print("No") | 0 | null | 2,591,908,603,456 | 63 | 81 |
while 1:
h, w = map(int, raw_input().split())
if h == 0 and w == 0:
break
for i in range(h):
print "#" * w
print"" | A, B, K = map(int, input().split())
if A >= K:
print(str(A - K) + ' ' + str(B))
else:
print(str(0) + ' ' + str(max((B - (K - A)), 0)))
| 0 | null | 52,278,008,729,312 | 49 | 249 |
def resolve():
S, T = input().split()
print(T+S)
if '__main__' == __name__:
resolve() | s, t = input().split()
print(f"{t}{s}") | 1 | 102,920,928,824,598 | null | 248 | 248 |
a, b = map(int, input().split())
c = []
if a > b:
a, b = b, a
if b%a == 0:
print(a)
else:
while True:
for i in range(a):
x = i + 2
#print(a, x)
if a%x == 0:
if b%x == 0:
c.append(x)
a = a//x
b = b//x
#print(c)
break
elif b%(a//x) == 0:
c.append(a//x)
a = x
b = b//(a//x)
#print(c)
break
#if x%1000 == 0:
#print(x)
if x > a**0.5:
break
if x > a**0.5:
break
s = 1
for j in c:
s = s * j
print(s)
| nkc = list(map(int, input().split()))
s = input()
ok = [i for i in range(nkc[0]) if s[i]=='o']
L = []
today = -1000000
for x in ok:
if x-today >= nkc[2]+1:
today = x
L.append(x)
if len(L) == nkc[1]:
break
R = []
today = 1000000
ok.reverse()
for x in ok:
if today-x >= nkc[2]+1:
today = x
R.append(x)
if len(R) == nkc[1]:
break
R.reverse()
for i in range(nkc[1]):
if L[i] == R[i]:
print(L[i]+1) | 0 | null | 20,260,794,001,988 | 11 | 182 |
ary = list(input())
s1 = []
s = 0
s2 = []
cnt = 0
for _ in ary:
if _ == '\\':
s1.append(cnt)
cnt += 1
elif _ == '/':
if s1:
old_cnt = s1.pop()
area = cnt - old_cnt
s += area
while s2:
if old_cnt < s2[-1][0]:
area += s2.pop()[1]
else:
break
s2.append((old_cnt, area))
cnt += 1
else:
cnt += 1
print(s)
print(len(s2), *[_[1] for _ in s2])
| li = []
for i, s in enumerate(input()):
if s == "\\":
li.append([i, 0])
elif s == "/":
if li:
if li[-1][1] == 0:
li[-1][1] = i - li[-1][0]
else:
for j in range(len(li) - 1, -1, -1):
if li[j][1] == 0:
li = li[:j] + [[li[j][0], sum(tuple(zip(*li[j + 1:]))[1]) + i - li[j][0]]]
break
ans = []
for a in li:
if a[1] != 0:
ans.append(a[1])
print(sum(ans))
print(len(ans), *ans)
| 1 | 58,436,525,840 | null | 21 | 21 |
n = input()
ans = 0
for i in range(len(n)):
ans += int(n[i])
ans %= 9
print("Yes" if ans % 9 == 0 else "No") | N = int(input())
if N % 9 == 0:
ans = 'Yes'
else:
ans = 'No'
print(ans) | 1 | 4,353,075,003,772 | null | 87 | 87 |
ans = 0
for i in xrange(input()):
n = int(raw_input())
if n <= 1:
continue
j = 2
while j*j <= n:
if n%j == 0:
break
j += 1
else:
ans += 1
print ans | from math import sqrt
def isPrime(x):
if x == 2:
return True
if x < 2 or x % 2 == 0:
return False
i, xx = 3, int(sqrt(x))
while i <= xx:
if x % i == 0:
return False
i = i + 2
return True
N = int(input())
print(sum([1 for _ in range(N) if isPrime(int(input()))])) | 1 | 10,779,537,082 | null | 12 | 12 |
k=int(input())
a=7
cnt=1
while cnt<=k+2:
if a%k==0:
print(cnt)
flag=True
break
else:
flag=False
cnt+=1
a=(10*a+7)%k
if not flag:
print(-1)
| # coding: utf-8
from math import sqrt
def solve(*args: str) -> str:
k = int(args[0])
l = 9*(k//7 if 0 == k % 7 else k)
if 0 == l % 2 or 0 == l % 5:
return '-1'
r = phi = l
for i in range(2, int(sqrt(l)+1)):
if 0 == r % i:
phi = phi*(i-1)//i
while 0 == r % i:
r //= i
if 1 < r:
phi = phi*(r-1)//r
D = set()
for d in range(1, int(sqrt(phi)+1)):
if 0 == phi % d:
D.add(d)
D.add(phi//d)
ret = -1
for m in sorted(D):
if 1 == pow(10, m, l):
ret = m
break
return str(ret)
if __name__ == "__main__":
print(solve(*(open(0).read().splitlines())))
| 1 | 6,031,264,445,680 | null | 97 | 97 |
N = int(input())
S = list(input())
ans = 'No'
n = int(N / 2)
if S[:n] == S[n:]:
ans = 'Yes'
print(ans) | lst = [input() for i in range(2)]
n = int(lst[0])
s = lst[1]
t = ''
for i in range(n//2):
t += s[i]
if n % 2 == 1:
print('No')
elif t + t == s:
print('Yes')
else:
print('No')
| 1 | 146,378,757,991,160 | null | 279 | 279 |
import math
a,b,c=map(float,input().split())
c=math.radians(c)
h=b*math.sin(c)
s=a*h/2
d=math.sqrt(a**2+b**2-2*a*b*math.cos(c))
l=a+b+d
print(s,l,h) | k = int(input())
s = input()
sen = ''
if len(s) <= k:
print(s)
else:
for i in range(k):
sen = sen + s[i]
print(sen+'...')
| 0 | null | 9,912,680,074,502 | 30 | 143 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.