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
|
---|---|---|---|---|---|---|
#!/usr/bin/env python3
n = list(input())
if '3' == n[-1]:
print('bon')
elif n[-1] in ['0', '1', '6', '8']:
print('pon')
else:
print('hon')
| s=input()
i=s[-1]
if i=='2' or i=='4' or i=='5' or i=='7' or i=='9':
print('hon')
elif i=='0' or i=='1' or i=='6' or i=='8':
print('pon')
else:
print('bon') | 1 | 19,127,823,209,100 | null | 142 | 142 |
n = int(input())
l = sorted(list(map(int, input().split())))
ans = 0
import bisect
for i in range(n-2):
for j in range(i+1, n-1):
ab = l[i]+l[j]
idx = bisect.bisect_left(l, ab)
ans += max(0, idx-j-1)
print(ans) | n=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for i in range(n):
for j in range(i+1,n):
l=j;r=n
while abs(l-r)>1:
mid=(r+l)//2
if L[i]<L[j]+L[mid] and L[j]<L[i]+L[mid] and L[mid]<L[i]+L[j]:
l=mid
else: r=mid
ans+=l-j
print(ans) | 1 | 171,684,306,220,570 | null | 294 | 294 |
a,b,k=map(int,input().split())
if k<=a:
print(a-k,b)
elif k>a and k<=a+b:
print(0,a+b-k)
else:
print(0,0) | A,B,K = map(int, input().split())
print(A-K, B) if K <= A else print(0,0) if (A+B) <= K else print(0, A+B-K)
| 1 | 104,501,905,658,810 | null | 249 | 249 |
D=int(input())
c=[None]+list(map(int, input().split()))
s=[None]+[list(map(int, input().split())) for _ in range(D)]
T=[None]+[int(input()) for _ in range(D)]
lastdi=[None]+[0]*26
v=0
for i in range(1, D+1):
v+=s[i][T[i]-1]
lastdi[T[i]]=i
for x in range(1, 27):
v-=(i-lastdi[x])*c[x]
print(v) | N,K = list(map(int, input().split()))
mod = int(1e9+7)
# N+1からK個をとる手法
# だけどmin - maxまで一飛ばしで作れるはずなので引き算でもとめてよい
ans = 0
for i in range(K,N+2):
ans = (ans + i * (N+1-i) + 1) % mod
print(ans) | 0 | null | 21,716,750,153,760 | 114 | 170 |
for _ in[0]*int(input()):a,b,c=sorted(map(int,input().split()));print(['NO','YES'][a*a+b*b==c*c]) | n = int(input())
from math import ceil
change = ceil(n/1000)*1000 - n
print(change) | 0 | null | 4,270,584,355,158 | 4 | 108 |
A = list(map(int,input().split()))
if A[0] < A[1]*2:
print(0)
else:
print(A[0]-A[1]*2) | n = int(input())
buf = list(map(int,input().split()))
a = []
for i in range(n):
a.append([buf[i],i])
a = sorted(a,reverse=True)
dp = [[0]*(n+1) for i in range(n+1)]
for i in range(n):
for j in range(n-i):
cur = i+j
temp1 = dp[i][j]+a[cur][0]*abs(n-1-a[cur][1]-j)
temp2 = dp[i][j]+a[cur][0]*abs(a[cur][1]-i)
dp[i+1][j] = max(dp[i+1][j],temp2)
dp[i][j+1] = max(dp[i][j+1],temp1)
print(max([max(i) for i in dp])) | 0 | null | 99,959,908,120,270 | 291 | 171 |
_, K, *H = map(int, open(0).read().split())
print(len(tuple(filter(lambda h: h >= K, H)))) | import numpy as np
import math
n=int(input())
a=[]
for i in range(n):
a.append(list(map(int,input().split())))
a=np.array(a, dtype=object)
ans=0
for i in range(n):
for j in range(n):
if i==j:
continue
else:
x= (a[i]-a[j])**2
hei=math.sqrt(sum(x))
ans+= hei/n
print(ans)
| 0 | null | 164,071,918,113,838 | 298 | 280 |
# Aizu Problem ITP_1_7_A: Grading
#
import sys, math, os
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
while True:
m, f, r = [int(_) for _ in input().split()]
score = m + f
if m == f == r == -1:
break
elif m == -1 or f == -1:
print('F')
elif score >= 80:
print('A')
elif score >= 65:
print('B')
elif score >= 50 or (score >= 30 and r >= 50):
print('C')
elif score >= 30:
print('D')
else:
print('F') | while True:
m, f, r = [int (x) for x in input().split(' ')]
if m == -1 and f == -1 and r == -1: break
if m == -1 or f == -1 or m + f < 30:
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:
if r >= 50:
print('C')
else:
print('D') | 1 | 1,223,791,613,662 | null | 57 | 57 |
from math import *
x1, y1, x2, y2 = map(float, raw_input().split())
distance = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
print distance | import sys
input = sys.stdin.readline
n,p=map(int,input().split())
s=input()
ans=0
if p == 2 or p == 5:
for i in range(n):
if int(s[i]) % p == 0:
ans += i + 1
print(ans)
exit()
total=[0]*p#pで割った余りで分類
total[0]=1#t[0]=0の分をあらかじめカウント
t=[0]*(n+1)#s[i:n]を格納する用の配列
for i in range(n-1,-1,-1):
t[i]=t[i+1]+int(s[i])*pow(10,n-1-i,p)#s[i:n]を漸化式で付け加えていく
total[t[i]%p]+=1
for i in range(p):
ans+=total[i]*(total[i]-1)//2
print(ans) | 0 | null | 29,283,447,227,872 | 29 | 205 |
W, H, x, y, r = [int(i) for i in input().split()]
if r <= y <= H - r and r <= x <= W - r:
print('Yes')
else:
print('No') | 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')
else:
print('Yes')
| 1 | 453,435,929,010 | null | 41 | 41 |
import math
x = int(input())
ans = 0
y = 100
while y < x:
y += y//100
ans += 1
print(ans) | def f(x):
now = 0
for i in range(n):
now += (a[i]-1)//x
return now <= k
n, k = map(int, input().split())
a = list(map(int, input().split()))
ng = 0
ok= int(1e9)
while ok - ng > 1:
x = (ok + ng) // 2
if f(x):
ok = x
else:
ng = x
print(ok) | 0 | null | 16,689,277,239,130 | 159 | 99 |
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10
N = INT()
AB = [LIST() for _ in range(N)]
A, B = zip(*AB)
A = list(A)
B = list(B)
A.sort()
B.sort()
if N%2:
a = A[N//2]
b = B[N//2]
print(b-a+1)
else:
a = (A[N//2-1]+A[N//2])/2
b = (B[N//2-1]+B[N//2])/2
print(int((b-a)*2+1))
| import sys
# input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7
S, T = map(str, input().split())
A, B = map(int, input().split())
U = str(input())
if S == U:
print (A - 1, B)
else:
print (A, B - 1) | 0 | null | 44,778,589,115,440 | 137 | 220 |
input()
list = input().split()
list.reverse()
print (' '.join(list)) | import numpy as np
N = int(input())
X = int(np.ceil(N/1.08))
if np.floor(X*1.08)!=N:
X = ':('
print(X) | 0 | null | 63,550,101,404,890 | 53 | 265 |
N = int(input())
A = [int(x) for x in input().split()]
M = max(A)
count = [0] * (M + 1)
for i in range(N):
count[A[i]] += 1
check = [True] * (M + 1)
for a in range(1, M + 1):
if count[a] > 0 and check[a] == True:
for k in range(2, M // a + 1):
check[a * k] = False
ans = 0
for i in range(N):
if count[A[i]] == 1 and check[A[i]] == True:
ans += 1
print(ans) | N = int(input())
A = list(map(int, input().split()))
node = 1
# 作成しなければならない葉の残りの数
leaf = sum(A)
max_node = 1
judge = True
ans = 0
for i, a in enumerate(A):
ans += node
leaf -= a
if node < a+1 and leaf > 0:
judge = False
break
# 次の深さのノード
max_node = (node - a) * 2
node = min(leaf, max_node)
if judge and max_node == 0:
print(ans)
else:
print(-1)
| 0 | null | 16,718,828,837,612 | 129 | 141 |
x = int(raw_input())
print "%d" % x**3 | N = int(input())
town = [list(map(int, input().split())) for _ in range(N)]
s = 0
for i in range(N):
for j in range(N):
if i == j:
continue
s += ((town[i][0] - town[j][0])**2 + (town[i][1] - town[j][1])**2)**0.5
print(s / N) | 0 | null | 74,689,678,169,690 | 35 | 280 |
n = int(input())
s = [input() for _ in range(n)]
s_sort = sorted(s)
s_num = [1]*n
for i in range(1,n):
if s_sort[i] == s_sort[i-1]:
s_num[i] += s_num[i-1]
Maxnum = max(s_num)
index_num = [n for n, v in enumerate(s_num) if v == Maxnum]
[print(s_sort[i]) for i in index_num]
| def selectionSort(A,N):
b = 0
for i in range(0,N):
minj = i
for j in range(i,N):
if A[minj] > A[j]:
minj = j
A[i],A[minj] = A[minj],A[i]
if minj != i:
b += 1
print(*A)
print(b)
N = int(input())
A = list(map(int,input().split()))
selectionSort(A,N)
| 0 | null | 35,171,505,576,064 | 218 | 15 |
#!/usr/bin python3
# -*- coding: utf-8 -*-
from collections import deque
def main():
N = int(input())
Node = [set() for _ in range(N)]
Edge = {}
for i in range(N-1):
a , b = map(int, input().split())
a-=1
b-=1
if b<a:
a, b = b, a
Edge[(a, b)]=i
Node[a].add(b)
Node[b].add(a)
K = 0
for i, x in enumerate(Node):
if len(x)>K:
K = len(x)
top = i
q = deque()
seen = [False]*(N-1)
used = [set() for _ in range(N)]
q.append(top)
while len(q)>0:
cur = q.popleft()
col = 1
for i in Node[cur]:
if cur>i:
ed = (i, cur)
else:
ed = (cur, i)
if seen[Edge[ed]]==False:
while col in used[cur] or col in used[i]:
col+=1
seen[Edge[ed]] = col
used[cur].add(col)
used[i].add(col)
q.append(i)
print(K)
print('\n'.join(map(str, seen)))
if __name__ == '__main__':
main()
| n = int(input())
a = list(map(int, input().split()))
a_to_index = [0] * n
for i in range(n):
a_to_index[a[i]-1] = i + 1
a.sort()
ans = [0] * n
for index, ai in enumerate(a):
ans[index] = a_to_index[ai-1]
print(*ans)
| 0 | null | 157,820,513,019,852 | 272 | 299 |
from collections import defaultdict
H, W, M = list(map(int, input().split()))
hw = [list(map(int, input().split())) for _ in range(M)]
d1 = defaultdict(int)
d2 = defaultdict(int)
b = defaultdict(int)
for h, w in hw:
d1[h] += 1
d2[w] += 1
b[(h, w)] = 1
m1 = max(d1.values())
m2 = max(d2.values())
e1 = [k for k in d1.keys() if d1[k] == m1]
e2 = [k for k in d2.keys() if d2[k] == m2]
flag = True
for x in e1:
for y in e2:
if b[(x, y)] == 1:
pass
else:
flag = False
break
if not flag:
break
if flag:
print(m1+m2-1)
else:
print(m1+m2) | 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)
| 0 | null | 5,348,314,778,832 | 89 | 97 |
n = int(input())
nums = list(map(int, input().split()))
count = 0
while True:
flag = 0
for j in range(n - 1):
if (nums[j + 1] < nums[j]):
temp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = temp
count += 1
flag = 1
if flag != 1:
break
print(' '.join([str(i) for i in nums]))
print(count)
| def bubbleSort(A, N):
flag = 1
count = 0
while flag:
flag = 0
for j in xrange(N-1,0,-1):
if A[j] < A[j-1]:
tmp = A[j]
A[j] = A[j-1]
A[j-1] = tmp
flag += 1
count += 1
return A, count
def main():
N = int(raw_input())
A = map(int, raw_input().split())
new_A, count = bubbleSort(A, N)
print ' '.join(map(str,new_A))
print count
if __name__ == "__main__":
main() | 1 | 17,838,361,970 | null | 14 | 14 |
N,K=map(int,input().split())
P=list(map(int,input().split()))
P.sort()
b=0
for i in range(K):
b+=P[i]
print(b) | n, k = map(int, input().split())
p = list(map(int, input().split()))
sortPrice = sorted(p)
# print(sortPrice)
price = []
for i in range(0, k):
price.append(sortPrice[i])
print(sum(price)) | 1 | 11,556,599,950,392 | null | 120 | 120 |
S = str(input())
print('x'*len(S))
| # B - I miss you...
# S
S = input()
answer = '0'
for i in range(0, len(S)):
answer += 'x'
print(answer[1:len(S) + 1])
| 1 | 73,152,368,221,648 | null | 221 | 221 |
# +1/-1の折れ線で表したとき
# 「0で終わる」かつ「途中で0未満にならない」を満たせば良い
# あとは貪欲を上手い事使う
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
import math
#from math import gcd
#inf = 10**17
#mod = 10**9 + 7
n = int(input())
a = []
b = []
for _ in range(n):
s = input().rstrip()
# 最終的に上がる量/最下点
up, down = 0, 0
for i in s:
if i == ')':
up -= 1
if down > up:
down = up
else:
up += 1
if up >= 0:
a.append((down, up))
else:
b.append((up-down, down, up))
a.sort(reverse=True)
b.sort(key=lambda a: a[0],reverse=True)
c = 0
for d, u in a:
if c+d < 0:
print('No')
break
else:
c += u
else:
for _, d, u in b:
if c+d < 0:
print('No')
break
else:
c += u
else:
if c == 0:
print('Yes')
else:
print('No')
if __name__ == '__main__':
main()
| n = int(input())
l = [0 for i in range(n)]
r = [0 for i in range(n)]
dat = []
dat2 = []
st = -1
for i in range(n):
s = input()
for ch in s:
if ch == '(':
r[i] += 1
pass
else:
if r[i] > 0:
r[i] -= 1
else:
l[i] += 1
pass
# print(i, l[i], r[i])
if l[i] == 0:
if st == -1 or r[st] < r[i]:
st = i
if r[i] >= l[i]:
dat.append((l[i], i))
else:
dat2.append((r[i], i))
dat.sort()
dat2.sort(reverse=True)
if st == -1:
print("No")
exit()
now = r[st]
# print('now={}'.format(now))
for num, i in dat:
if i == st:
continue
if now < l[i]:
print("No")
exit()
now = now - l[i] + r[i]
for num, i in dat2:
# print('dat2', num, i)
if now < l[i]:
print("No")
exit()
now = now - l[i] + r[i]
if now == 0:
print("Yes")
else:
print("No") | 1 | 23,732,984,105,052 | null | 152 | 152 |
def dts():
return list(map(int,input().split()))
d,t,s=dts()
if d>t*s:
print("No")
else:
print("Yes")
| r = int(input())
D = 2* r
print(3.1415926535897 * D) | 0 | null | 17,419,752,908,208 | 81 | 167 |
n = int(input())
room_list = [[[0 for i in range(10)] for j in range(3)] for k in range(4)]
for i in range(n):
b, f, r, v = map(int, input().split())
room_list[b - 1][f - 1][r - 1] += v
output =[]
for i in range(4):
for j in range(3):
output = list(map(str, room_list[i][j]))
print(" " + " ".join(output))
if i < 3:
print("#" * 20) | n = int(input())
a = list(map(int, input().split()))
max_node = [0 for _ in range(n+1)]
for i in range(n-1, -1, -1):
max_node[i] = max_node[i+1] + a[i+1]
ans = 1
node = 1
for i in range(n+1):
node -= a[i]
if (i < n and node <= 0) or node < 0:
print(-1)
exit(0)
node = min(node * 2, max_node[i])
ans += node
print(ans) | 0 | null | 9,987,277,797,728 | 55 | 141 |
import math
a,b,c=map(int, input().split())
c=math.radians(c)
print(0.5*a*b*math.sin(c))
print(a+b+math.sqrt(a**2+b**2-2*a*b*math.cos(c)))
print(b*math.sin(c)) | import math
a, b, C = map(int, input().split())
C = math.radians(C)
S = a * b * math.sin(C) * 0.5
c = math.sqrt(a * a + b * b - 2 * a * b * math.cos(C))
L = a + b + c
h = 2 * S / a
print(S, L, h)
| 1 | 180,034,887,202 | null | 30 | 30 |
input()
a=1
d=1000000000000000000
for i in input().split():
a=min(a*int(i),d+1)
print([a,-1][a>d]) | N = int(input())
A = list(map(int, input().split()))
if 0 in A:
print(0)
else:
ans = 1
for a in A:
ans *= a
if ans > 10**18:
print(-1)
exit(0)
print(ans)
| 1 | 16,311,604,272,986 | null | 134 | 134 |
# encoding=utf-8
n = int(raw_input())
min = int(raw_input())
keep = int(raw_input())
diff = keep - min
if keep > min:
keep = min
n = n - 2
for i in range(n):
v = int(raw_input())
if v - keep > diff:
diff = v - keep
min = keep
elif keep > v:
keep = v
print diff | n = int(raw_input())
num_list = [int(raw_input()) for i in xrange(n)]
minv = num_list[0]
maxv = -1000000000
for i in xrange(1, n):
tmp = num_list[i] - minv
maxv = tmp if tmp > maxv else maxv
minv = num_list[i] if minv > num_list[i] else minv
# print "i = %d, tmp = %d, maxv = %d, minv = %d" % (i, tmp, maxv, minv)
print maxv | 1 | 13,088,906,052 | null | 13 | 13 |
n=int(input())
a=list(map(int,input().split()))
if n%2==1:
dp=[[0 for j in range(3)] for i in range(n//2)]
dp[0][0]=a[0]
dp[0][1]=a[1]
dp[0][2]=a[2]
for i in range(n//2-1):
dp[i+1][0]=(dp[i][0]+a[2*(i+1)])
dp[i+1][1]=max(dp[i][0],dp[i][1])+a[2*(i+1)+1]
dp[i+1][2]=max(dp[i])+a[2*(i+1)+2]
print(max(dp[-1]))
if n%2==0:
dp=[[0 for j in range(2)] for i in range(n//2)]
dp[0][0]=a[0]
dp[0][1]=a[1]
for i in range(n//2-1):
dp[i+1][0]=(dp[i][0]+a[2*(i+1)])
dp[i+1][1]=max(dp[i][0],dp[i][1])+a[2*(i+1)+1]
#dp[i+1][2]=max(dp[i])+a[2*(i+2)+2]
print(max(dp[-1]))
| #!/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() | 1 | 37,379,901,024,228 | null | 177 | 177 |
x = input()
print(int(x)*int(x)*int(x)) | i = int(raw_input())
print i**3 | 1 | 278,849,652,580 | null | 35 | 35 |
str = input()
lstr = list(str)
num = int(input())
for i in range(num):
order = input().split()
if (order[0] == "print"):
onum = list(map(int,order[1:3]))
for i in range(onum[0],onum[1]+1):
print(lstr[i],end = "")
print("")
elif (order[0] == "reverse"):
onum = list(map(int,order[1:3]))
if (onum[0] == 0):
tmp1 = lstr[0:onum[1]+1]
tmp1.reverse()
tmp2 = lstr[onum[1]+1:]
lstr = tmp1 + tmp2
else:
tmp1 = lstr[:onum[0]]
tmp2 = lstr[onum[0]:onum[1]+1]
tmp2.reverse()
tmp3 = lstr[onum[1]+1:]
lstr = tmp1 + tmp2 +tmp3
elif (order[0] == "replace"):
onum = list(map(int,order[1:3]))
restr = list(order[3])
for i in range(onum[0],onum[1]+1):
lstr[i] = restr[i-onum[0]]
| def rev(val, v1, v2):
for i in range((v2-v1)//2 +1):
val[v1+i], val[v2-i] = val[v2-i], val[v1+i]
return val
def rep(val1, val2, v1, v2):
for i in range(v2-v1):
val1[v1+i] = val2[i]
return val1
st = input()
lst = [i for i in st]
q = int(input())
for i in range(q):
f = list(map(str, input().split()))
if f[0] == 'print':
s = ''.join(lst[int(f[1]):int(f[2])+1])
print(s)
elif f[0] == 'reverse':
lst = rev(lst, int(f[1]), int(f[2]))
elif f[0] == 'replace':
x = [i for i in f[3]]
lst = rep(lst, x, int(f[1]), int(f[2])+1)
| 1 | 2,083,711,991,450 | null | 68 | 68 |
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline
class UnionFind:
def __init__(self, n: int):
self.n = n
self.parents = [-1] * n
def find(self, x: int):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x: int, y: int):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[y] < self.parents[x]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x: int):
return -self.parents[self.find(x)]
def same(self, x: int, y: int):
return self.find(x) == self.find(y)
def members(self, x: int):
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 solve():
N, K = map(int, rl().split())
P = list(map(lambda n: int(n) - 1, rl().split()))
C = list(map(int, rl().split()))
uf = UnionFind(N)
for i in range(N):
uf.union(i, P[i])
roop_scores = defaultdict(int)
for i in range(N):
roop_scores[uf.find(i)] += C[i]
ans = -(10 ** 18)
K -= 1
for s in range(N):
cur = P[s]
tmp = C[cur]
ans = max(ans, tmp)
roop_size = uf.size(s)
roop_score = roop_scores[uf.find(s)]
roop_cnt = K // roop_size
if roop_cnt and 0 < roop_score:
tmp += roop_score * roop_cnt
rem = K % roop_size
ans = max(ans, tmp)
while rem:
cur = P[cur]
tmp += C[cur]
ans = max(ans, tmp)
rem -= 1
continue
cnt = min(roop_size, K)
while cnt:
cur = P[cur]
tmp += C[cur]
ans = max(ans, tmp)
cnt -= 1
print(ans)
if __name__ == '__main__':
solve()
| MOD = 998244353
def main():
# もらうdp + 累積和
N, K = (int(i) for i in input().split())
LR = [[int(i) for i in input().split()] for j in range(K)]
dp = [0] * (N+2)
dpsum = [0] * (N+1)
dp[1] = 1
dpsum[1] = 1
for i in range(2, N+1):
for le, ri in LR:
L = max(0, i - ri - 1)
R = i - le
if R < 1:
continue
dp[i] += dpsum[R] - dpsum[L]
dp[i] %= MOD
dpsum[i] += dpsum[i-1] + dp[i]
dpsum[i] %= MOD
print(dp[N])
# print(dpsum)
if __name__ == '__main__':
main()
| 0 | null | 4,055,562,046,210 | 93 | 74 |
it = lambda: list(map(int, input().strip().split()))
def solve():
X, K, D = it()
X = abs(X)
if X - K * D >= 0:
return X - K * D
K = K - X // D
X = X - X // D * D
assert K >= 0
assert X >= 0
return abs(X) if K % 2 == 0 else abs(X - D)
if __name__ == '__main__':
ans = solve()
print(ans) | if __name__ == "__main__":
X, K, D = map(lambda x: abs(int(x)), input().split())
if X - K*D >= 0:
print(X - K*D)
else:
xdd = X // D
k = xdd + (K - xdd) % 2
print(abs(X - k*D))
| 1 | 5,178,267,015,530 | null | 92 | 92 |
def circle(n):
return n * n
if __name__ == "__main__":
n = int(input())
print(circle(n)) | r = int(input())
print(pow(r,2))
| 1 | 145,191,294,863,200 | null | 278 | 278 |
n,k=map(int,input().split())
if n<=9:
print(k+(100*(10-n)))
else:
print(k) | '''
INPUT SHORTCUTS
N, K = map(int,input().split())
N ,A,B = map(int,input().split())
string = str(input())
arr = list(map(int,input().split()))
N = int(input())
'''
n , m = map(int,input().split())
if n>=10:
print(m)
else:
m = m + (100*(10-n))
print(m) | 1 | 63,701,106,153,038 | null | 211 | 211 |
from collections import deque
H, W = [int(x) for x in input().split()]
field = []
for i in range(H):
field.append(input())
conn = [[[] for _ in range(W)] for _ in range(H)]
for i in range(H):
for j in range(W):
if field[i][j] == '.':
for e in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
h, w = i + e[0], j + e[1]
if 0 <= h < H and 0 <= w < W and field[h][w] == '.':
conn[i][j].append([h, w])
d = 0
for i in range(H):
for j in range(W):
l = 0
q = deque([[i, j]])
dist = [[-1 for _ in range(W)] for _ in range(H)]
dist[i][j] = 0
while q:
v = q.popleft()
for w in conn[v[0]][v[1]]:
if dist[w[0]][w[1]] == -1:
q.append(w)
dist[w[0]][w[1]] = dist[v[0]][v[1]] + 1
l = dist[w[0]][w[1]]
d = max(d, l)
print(d) | #!/usr/bin/env python3
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra
import numpy as np
def main():
H, W = list(map(int, input().split()))
N = H * W
S = [[line for line in range(W)] for row in range(H)]
for i in range(H):
row = input()
for j, line in enumerate(row):
S[i][j] = line
data = np.zeros([N, N])
for i in range(H):
for j in range(W):
n = W * i + j
if j<W-1 and S[i][j]=='.' and S[i][j+1]=='.':
data[n][n+1] = data[n+1][n] = 1
if i<H-1 and S[i][j]=='.' and S[i+1][j]=='.':
data[n][n+W] = data[n+W][n] = 1
G = csgraph_from_dense(data)
answer = 0
for n in range(N):
result = dijkstra(G, indices=n)
result = [x for x in result if x!=np.inf]
answer = max(answer, max(result))
print(int(answer))
pass
if __name__ == '__main__':
main()
| 1 | 94,699,378,351,670 | null | 241 | 241 |
def gcd(a, b):
a, b = max(a, b), min(a, b)
if b == 0:
return a
return gcd(b, a % b)
def main():
X = int(input())
ans = 360 // gcd(X, 360)
print(ans)
if __name__ == "__main__":
main()
| import math
X = int(input())
i = 1
while True:
net = (i * X)/360
if math.floor(net) == math.ceil(net):
break
else:
i+=1
print(i) | 1 | 13,127,812,764,560 | null | 125 | 125 |
n = int(input())
a = []
for i in range(1, n+1):
if i%3 == 0 or i%5 == 0:
continue
else:
a.append(i)
print(sum(a)) | N = int(input())
s = 0
i = 1
while i <= N:
if i % 3 != 0 and i % 5 != 0:
s += i
i += 1
print(s) | 1 | 34,727,868,089,060 | null | 173 | 173 |
n = input()
s , t=input().split()
print(*[s + t for s, t in zip(s, t)],sep='') | S = input()
companies = {company for company in S}
if len(companies) > 1:
print('Yes')
else:
print('No') | 0 | null | 83,372,748,831,136 | 255 | 201 |
def insersionSort(A, N, count=0, g=1):
# 間隔 g で挿入ソートを行う
for i in range(g, N):
v = A[i]
j = i - g
while j >= 0 and A[j] > v:
A[j+g] = A[j]
j -= g
count += 1
A[j+g] = v
return A, count
def shellSort():
N, *A = map(int, open(0).read().split())
G = [1]
cursor = 2
while True:
g = (3 ** cursor - 1) // 2
if g < N:
G.append(g)
cursor += 1
else:
break
G = G[::-1]
print(len(G))
print(' '.join([str(g) for g in G]))
count = 0
for g in G:
A, count = insersionSort(A, N, count, g)
print(count)
for a in A:
print(a)
if __name__=="__main__":
shellSort()
| import math
import sys
def insertionSort(data, N, g, cnt):
for i in range(g, N):
v = data[i]
j = i - g
while j >= 0 and data[j] > v:
data[j+g] = data[j]
j = j - g
cnt += 1
data[j+g] = v
return data, cnt
def shellSort(data, N, G, cnt):
h = 1
while (True):
if h > N:
break
G.append(h)
h = 3 * h + 1
m = len(G)
G.reverse()
for i in range(m):
data, cnt = insertionSort(data, N, G[i], cnt)
return m, G, cnt
def main():
N = int(input())
data = [int(input()) for _ in range(N)]
cnt = 0
G = []
m = 0
m, G, cnt = shellSort(data, N, G, cnt)
print(m)
print(' '.join(map(str, G)))
print(cnt)
for i in range(N):
print(data[i])
if __name__ == '__main__':
main()
| 1 | 30,478,869,708 | null | 17 | 17 |
r, c = map(int, input().split())
matrix = []
for _ in range(r):
L = list(map(int, input().split()))
matrix.append(L + [sum(L)])
matrix.append([sum(column) for column in zip(*matrix)])
for row in matrix:
print(' '.join(map(str, row))) | from time import time
start = time()
import random
def solve(c_list, days, now):
r = sum(c_list[i]*(now-days[i]) for i in range(26))
return r
d = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _i in range(d)]
last_days = [-1 for _i in range(26)]
result = []
score = 0
for today in range(d):
checker = [c[j]*(today-last_days[j]) for j in range(26)]
y = sum(checker)
finder = [s[today][j]-(y-checker[j]) for j in range(26)]
x = finder.index(max(finder))
last_days[x] = today
result.append(x+1)
score += s[today][x] - solve(c, last_days, today)
def change(problems, days):
last_days = [-1 for _i in range(26)]
score = 0
for i in range(d):
score += s[i][problems[i]-1]
last_days[problems[i]-1] = i
score -= sum([c[j]*(i-last_days[j]) for j in range(26)])
return score
change_list = result.copy()
hantei = True
coun = 0
while hantei:
if random.randrange(2)<1:
x, y = random.randrange(0, d), random.randrange(0, d)
while x == y:
y = random.randrange(0, d)
change_list[x], change_list[y] = change_list[y], change_list[x]
next_score = change(change_list, d)
if next_score >= score:
score = next_score
result[x], result[y] = result[y], result[x]
elif random.randrange(100)==5:
score = next_score
result[x], result[y] = result[y], result[x]
else:
change_list[x], change_list[y] = change_list[y], change_list[x]
else:
x = random.randrange(0, d)
y = random.randrange(1, 27)
while change_list[x] == y:
x = random.randrange(0, d)
y = random.randrange(1, 27)
change_list[x], z = y, change_list[x]
next_score = change(change_list, d)
if next_score >= score:
score = next_score
result[x] = y
else:
change_list[x] = z
coun += 1
if coun ==100:
coun = 0
if time()-start >= 1.8:
hantei = False
for i in result:
print(i) | 0 | null | 5,533,430,974,290 | 59 | 113 |
import math
a, b, c = map(int, input().split())
d = math.ceil(a / b)
print(d * c) | import math
N, X, T = map(int, input().split())
m = math.ceil(N/X)
print(m*T)
| 1 | 4,249,123,272,800 | null | 86 | 86 |
t = input('')
tlist = list(t)
for i in range(len(tlist)):
if tlist[i] == '?':
if i==0:
tlist[i] = 'D'
elif i==len(tlist)-1:
tlist[i] = 'D'
else:
if tlist[i-1] == 'P':
if tlist[i+1] == 'P':
tlist[i] = 'D'
elif tlist[i+1] == 'D':
tlist[i] = 'D'
elif tlist[i+1] == '?':
tlist[i] = 'D'
else:
# 1つ前が'D'
if tlist[i+1] == 'P':
tlist[i] = 'D'
elif tlist[i+1] == 'D':
tlist[i] = 'P'
elif tlist[i+1] == '?':
tlist[i] = 'P'
print("".join(tlist)) | from sys import stdin
T = stdin.readline().rstrip()
print(T.replace('?', 'D')) | 1 | 18,572,456,879,902 | null | 140 | 140 |
import sys
import itertools
# import numpy as np
import time
import math
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from functools import reduce
# from math import *
from fractions import *
N, M = map(int, readline().split())
A = list(map(lambda x: int(x) // 2, readline().split()))
def f(n):
cnt = 0
while n % 2 == 0:
n //= 2
cnt += 1
return cnt
t = f(A[0])
for i in range(N):
if f(A[i]) != t:
print(0)
exit(0)
A[i] >>= t
M >>= t
lcm = reduce(lambda a, b: (a * b) // gcd(a, b), A)
if lcm > M:
print(0)
exit(0)
print((M // lcm + 1) // 2)
| while 1:
w,h=map(int,raw_input().split());a,b="#\n"
if w==0:break
print(a*h+b)*(w) | 0 | null | 51,211,867,703,996 | 247 | 49 |
n=int(input())
a=list(map(int,input().split()))
l=[0]*(n)
for i in range(n):l[a[i]-1]=i+1
for i in l:
print(i,end=" ")
| #from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
return list(map(int,input().split()))
def I():
return int(input())
n = I()
A = readInts()
nya = []
for i in range(n):
nya.append((i+1,A[i]))
nya = sorted(nya,key = lambda x:x[1])
ans = []
for (idx,po) in nya:
ans.append(idx)
print(*ans)
| 1 | 181,340,285,247,300 | null | 299 | 299 |
import sys
import math
import bisect
def input():
return sys.stdin.readline()[:-1]
n=int(input())
s=list(input())
q=int(input())
ans=[]
dic = {}
for i in range(97,97+26):
dic[chr(i)]=[]
for num,i in enumerate(s):
dic[i].append(num)
for i in range(q):
num,l,r = map(str,input().split())
if int(num)==1:
l=int(l)-1
if s[l] == r:
continue
else:
dic[s[l]].pop(bisect.bisect_left(dic[s[l]],l))
bisect.insort_left(dic[r],l)
s[l] = r
else:
cnt=0
l=int(l)
r=int(r)
for li in dic.values():
ind = bisect.bisect_left(li,l-1)
if ind>=len(li):continue
if li[ind]<=r-1:
cnt+=1
ans.append(cnt)
print(*ans,sep="\n")
| # https://atcoder.jp/contests/abc157/tasks/abc157_e
import sys
input = sys.stdin.readline
class SegmentTree:
def __init__(self, n, op, e):
"""
:param n: 要素数
:param op: 二項演算
:param e: 単位減
例) 区間最小値 SegmentTree(n, lambda a, b : a if a < b else b, 10 ** 18)
区間和 SegmentTree(n, lambda a, b : a + b, 0)
"""
self.n = n
self.op = op
self.e = e
self.size = 1 << (self.n - 1).bit_length() # st[self.size + i] = array[i]
self.tree = [self.e] * (self.size << 1)
def built(self, array):
"""arrayを初期値とするセグメント木を構築"""
for i in range(self.n):
self.tree[self.size + i] = array[i]
for i in range(self.size - 1, 0, -1):
self.tree[i] = self.op(self.tree[i << 1], self.tree[(i << 1) + 1])
def update(self, i, x):
"""i 番目の要素を x に更新"""
i += self.size
self.tree[i] = x
while i > 1:
i >>= 1
self.tree[i] = self.op(self.tree[i << 1], self.tree[(i << 1) + 1])
def get_val(self, l, r):
"""[l, r)の畳み込みの結果を返す"""
l, r = l + self.size, r + self.size
res = self.e
while l < r:
if l & 1:
res = self.op(self.tree[l], res)
l += 1
if r & 1:
r -= 1
res = self.op(self.tree[r], res)
l, r = l >> 1, r >> 1
return res
##################################################################################################################
def popcount(x):
return bin(x).count("1")
N = int(input())
st = SegmentTree(N, lambda a, b: a | b, 0)
S = input().strip()
data = []
for s in S:
data.append(1 << (ord(s) - ord("a")))
st.built(data)
Q = int(input())
for _ in range(Q):
q, a, b = input().split()
if q == "1":
st.update(int(a)-1, 1 << (ord(b) - ord("a")))
if q == "2":
a = int(a) - 1
b = int(b)
print(popcount(st.get_val(a, b))) | 1 | 62,571,875,871,520 | null | 210 | 210 |
import sys
from bisect import bisect_right, bisect_left
from itertools import accumulate
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
def meguru_bisect(ok, ng):
while abs(ok - ng) > 1:
mid = (ok + ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
def is_ok(x):
cnt = 0
for a in A:
t = x - a
idx = bisect_right(A, t)
cnt += n - idx
return cnt < m
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
ng, ok = 0, 10 ** 15 + 1
mth = meguru_bisect(ok, ng)
R = [0] + list(accumulate(A))
res = 0
cnt = 0
for a in A:
s = mth - a
left = bisect_left(A, s)
res += (n - left) * a + R[-1] - R[left]
cnt += n - left
print(res - mth * (cnt - m))
if __name__ == '__main__':
resolve()
| N = int(input())
A = list(map(int, input().split()))
A.sort()
res = 1
for i in A:
res*=i;
if res>10**18:
res = -1
break
print(res) | 0 | null | 62,056,813,962,212 | 252 | 134 |
from numpy import cumsum
n,k=map(int,input().split())
p=list(map(int,input().split()))
p.sort()
p=cumsum(p)
print((p[k-1])) | N,K = map(int,input().split())
p_lis = list(map(int, input().split()))
sorted_lis = sorted(p_lis)
ans = 0
for i in range(K):
ans+=sorted_lis[i]
print(ans) | 1 | 11,590,439,500,948 | null | 120 | 120 |
def main():
a, b, c = map(int, input().split())
if a + b + c > 21:
print("bust")
else:
print("win")
if __name__ == "__main__":
main()
| import sys
sys.setrecursionlimit(10**6)
x = int(input())
if x >= 30:
print('Yes')
else:
print('No') | 0 | null | 62,601,640,901,240 | 260 | 95 |
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
return list(map(int,input().split()))
def m(): #複数文字
return map(int,input().split())
def onem(): #Nとかの取得
return int(input())
def s(x): #圧縮
a = []
if len(x) == 0:
return []
aa = x[0]
su = 1
for i in range(len(x)-1):
if aa != x[i+1]:
a.append([aa,su])
aa = x[i+1]
su = 1
else:
su += 1
a.append([aa,su])
return a
def jo(x): #listをスペースごとに分ける
return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
return max(map(max,x))
def In(x,a): #aがリスト(sorted)
k = bs.bisect_left(a,x)
if k != len(a) and a[k] == x:
return True
else:
return False
"""
def nibu(x,n,r):
ll = 0
rr = r
while True:
mid = (ll+rr)//2
if rr == mid:
return ll
if (ここに評価入れる):
rr = mid
else:
ll = mid+1
"""
h,w,k = m()
s = []
for i in range(h):
s.append(list(map(int,input()[:-1])))
st = [0 for i in range(h)]
dp = [[0,0] for i in range(h)]
ans = mod
for po in range(2 << h):
de = 0
tm = 0
for j in range(h):
dp[j][0] = 0
dp[j][1] = 0
for j in range(h):
if j == 0:
st[0] = 0
else:
a = (po>>j)&1
b = (po>>(j-1))&1
if a != b:
de += 1
st[j] = de
tm += de
for kp in range(w):
fl = 0
for kk in range(h):
if s[kk][kp] == 1:
dp[st[kk]][1] += 1
if all(dp[x][0] + dp[x][1] <= k for x in range(h)):
for kk in range(h):
dp[kk][0] += dp[kk][1]
dp[kk][1] = 0
else:
tm += 1
for kk in range(h):
if dp[kk][0] > k:
fl = 1
break
dp[kk][0] = dp[kk][1]
dp[kk][1] = 0
if fl:
break
else:
ans = min(tm,ans)
print(ans)
| import itertools
import bisect
N = int(input())
LS = list(map(int,input().split()))
LS.sort()
ans = 0
for a,b in itertools.combinations(LS,2):
a,b = min(a,b),max(a,b)
lo = b - a
up = a + b
cnt = bisect.bisect_left(LS,up) - bisect.bisect_right(LS,lo) - 1
if lo < a:
cnt -= 1
if cnt <= 0:
continue
ans += cnt
print(ans // 3)
| 0 | null | 109,907,206,711,038 | 193 | 294 |
n=int(input(""))
aa=input("").split(" ")
lista=[]
for i in range(n):
lista+=[int(aa[i])]
lista.sort()
listtf=[]
s=0
for i in range(lista[n-1]):
listtf+=[True]
for i in range(n):
if(listtf[lista[i]-1]):
if(i<n-1 and lista[i]==lista[i+1]):
s-=1
s+=1
t=1
while(t*lista[i]<=lista[n-1]):
listtf[lista[i]*t-1]=False
t+=1
print(s)
| n = int(input())
a = list(map(int, input().split()))
from collections import Counter
c = Counter(a)
vals = [True]*(max(a)+1)
ans = 0
m = max(a)+1
for i in range(1, m):
if i not in c:
continue
if c[i]>=2:
vals[i] = False
for j in range(2*i, m, i):
vals[j] = False
ans = 0
for i in range(1, max(a)+1):
if vals[i] and i in c:
ans += 1
print(ans) | 1 | 14,520,503,689,848 | null | 129 | 129 |
sm=0
for i in range(1,int(input())+1):
if (i%3)*(i%5)>0:
sm+=i
print(sm) | K=int(input())
S=input()
if K>=len(S):
print(S)
else:
for i in range(K):
print(S[i],end='')
print("...") | 0 | null | 27,242,062,350,988 | 173 | 143 |
import sys
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n = input()
k = int(input())
# 桁DP
def digit_DP(S, K):
L = len(S)
# dp[決定した桁数][未満フラグ][0以外の数字を使った個数]
dp = [[[0 for _ in range(4)] for _ in range(2)] for _ in range(101)]
dp[0][0][0] = 1
for i in range(L): # 今見ている桁
D = int(S[i])
for j in range(2): # 未満フラグ(0 or 1)
for k in range(4): # 0以外の数字を使った個数が0~3
for d in range(10 if j else D + 1):
cnt = k
if d != 0:
cnt += 1
if cnt > K:
continue
dp[i + 1][j or (d < D)][cnt] += dp[i][j][k]
return dp[L][0][K] + dp[L][1][K]
print(digit_DP(n, k))
if __name__ == '__main__':
resolve()
| from math import atan, degrees
a,b,x= map(int,input().split())
yoseki = a**2*b
if x <= yoseki/2:
# b*y*a/2==x
y= 2*x/b/a
# 90からシータを引けば良い
print(90-degrees(atan(y/b)))
else:
# a*y*a/2==yoseki-x
y = 2*(yoseki-x)/a**2
print(degrees(atan(y/a)))
| 0 | null | 119,316,465,483,322 | 224 | 289 |
x=list(map(int,input()))
if x[-1]==2 or x[-1]==4 or x[-1]==5 or x[-1]==7 or x[-1]==9:
print("hon")
elif x[-1]==0 or x[-1]==1 or x[-1]==6 or x[-1]==8:
print("pon")
else:
print("bon") | def resolve():
N = int(input())
print((N//2+1)/N if N%2==1 else 0.5)
if '__main__' == __name__:
resolve() | 0 | null | 98,571,007,247,428 | 142 | 297 |
S = int(input());print('{0}:{1}:{2}'.format(S//3600, S%3600//60, S%60))
| n, m = map(int, input().split())
in_list = input().split()
vote_list = [int(i) for i in in_list]
ans = 0
for ai in vote_list:
if ai >= 1/(4*m)*sum(vote_list):
ans += 1
if ans < m :
print('No')
else:
print('Yes') | 0 | null | 19,620,593,978,720 | 37 | 179 |
from collections import defaultdict
def solve():
N, M = map(int, input().split())
d = defaultdict(lambda: -1)
for i in range(M):
x,y = map(int, input().split())
if d[x]>=0 and d[x]!=y:
return -1
if N>1 and x==1 and y==0:
return -1
d[x] = y
if N==1 and d[1]==-1:
return 0
if d[1]==-1:
d[1] = 1
for i in range(2,N+1):
if d[i]==-1:
d[i] = 0
ans = ''
for i in range(1,N+1):
ans += str(d[i])
return ans
print(solve()) | n, m = map(int, input().split())
dic = {}
flag = True
for _ in range(m):
digit, num = map(int, input().split())
if dic.get(digit, None):
if dic[digit] != num:
flag = False
break
if digit == 1 and num == 0 and n != 1:
flag = False
break
if not dic.get(digit, None):
dic[digit] = num
if flag:
lst = [0] * n
for digit, num in dic.items():
lst[digit - 1] = num
s = ''
if lst[0] == 0 and n != 1:
lst[0] = 1
for ele in lst:
s += str(ele)
print(int(s))
else:
print(-1) | 1 | 60,672,622,282,152 | null | 208 | 208 |
n = int(input())
x = []
for i in range(n):
a, b = map(int, input().split())
x.append([a, b])
ans = 0
for i in range(len(x) - 1):
for j in range(i+1, len(x)):
ans += ((x[i][0]-x[j][0])**2 + (x[i][1]-x[j][1])**2)**0.5
print((2*ans)/n) | N = int(input())
cities = []
for n in range(N):
cities.append(tuple(map(int, input().split())))
import math
def distance(i, j):
xi,yi = cities[i]
xj,yj = cities[j]
xd,yd = xi - xj, yi - yj
return math.sqrt(xd * xd + yd * yd)
# path: これまでに通過した街の番号
def search(path):
total = 0
count = 0
for i in range(N):
if i not in path:
count += 1
path.append(i)
total += (0 if len(path) == 1 else distance(i, path[-2])) + search(path)
path.remove(i)
if count == 0:
return 0
else:
return total / count
print(search([])) | 1 | 148,128,430,164,132 | null | 280 | 280 |
n = int(input())
a = list(map(int, input().split()))
l = [0] * n
for i in a:
l[i - 1] += 1
for k in range(n):
print(l[k]) | s=input()
n=len(s)
k=int(input())
if s[0]==s[n-1]:
d=1
ans=0
for i in range(1,n):
if s[i-1]==s[i] and d==1:
ans+=1
d=0
elif s[i-1]==s[i] and d==0:
d=1
elif s[i-1]!=s[i]:
d=1
if d==0:
print(ans*k)
else:
ans2=0
for i in range(n):
if s[i-1]==s[i] and d==1:
ans2+=1
d=0
elif s[i-1]==s[i] and d==0:
d=1
elif s[i-1]!=s[i]:
d=1
if d==1:
print(ans+ans2*(k-1))
else:
if k%2==0:
print((ans+ans2)*k//2)
else:
print((ans+ans2)*(k//2)+ans)
elif s[0]!=s[n-1]:
d=1
ans=0
for i in range(1,n):
if s[i-1]==s[i] and d==1:
ans+=1
d=0
elif s[i-1]==s[i] and d==0:
d=1
elif s[i-1]!=s[i]:
d=1
print(ans*k) | 0 | null | 103,651,124,056,128 | 169 | 296 |
a,b=map(int,input().split())
list=[[0 for i in range(2)] for j in range(a)]
for i in range(a):
list[i]=input().split()
n=0
s=0
while n!=a:
if int(list[n][1])>b:
list[n][1]=int(list[n][1])-b
list.append(list[n])
list.pop(n)
s+=b
else:
print(list[n][0],s+int(list[n][1]))
s+=int(list[n][1])
n+=1
| from collections import deque
n, q = map(int, input().split())
ps = [input().split() for _ in range(n)]
que = deque(ps)
time = 0
while que:
p, rest = que.popleft()
elapsed = min(q, int(rest))
time += elapsed
rest = str(int(rest) - elapsed)
if int(rest) > 0:
que.append([p, rest])
else:
print(p, time)
| 1 | 44,724,793,482 | null | 19 | 19 |
n=int(input())
ans=0
tmp=0
p=1
if n%2==0:
k=n//2
while True:
tmp =k//pow(5,p)
ans+=tmp
p+=1
if tmp==0:
break
print(ans)
| import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])
n = inp()
ans = 0
if n % 2 == 1:
ans = 0
else:
div = 10
while div <= n:
ans += n // div
div *= 5
print(ans) | 1 | 115,598,089,621,440 | null | 258 | 258 |
S = list(input())
K = int(input())
ans = 0
#2つ並べて一般性を確かめる
S2 = S*2
for i in range(1,len(S2)):
if S2[i-1]== S2[i]:
S2[i] = '@'
#2回目以降はk-1回任意の@へ直す
if i>=len(S2)//2:
ans+= K-1
else:
ans+=1
if len(set(S)) ==1 and len(S)%2 !=0:
ans = len(S)*(K//2)+(K%2==1)*(len(S)//2)
print(ans) | import math
n, d, a = map(int,input().split())
e = [[] for i in range(n)]
for i in range(n):
x, h = map(int,input().split())
e[i] = [x,h]
num = 0
e.sort()
sd = [0 for i in range(n)]
l = [i for i in range(n)]
for i in range(n):
for j in range(l[i-1],i):
if e[i][0]-e[j][0] <= 2*d:
l[i] = j
break
for i in range(n):
res = e[i][1] - sd[i-1] + sd[l[i]-1]
if res < 0:
sd[i] = sd[i-1]
else:
k = math.ceil(res/a)
sd[i] = sd[i-1]+k*a
num += k
print(num)
| 0 | null | 129,039,703,321,594 | 296 | 230 |
import math
K = int(input())
A, B = map(int, input().split())
largest = math.floor(B/K)*K
print("OK") if largest >= A else print("NG") | s=input()
res=""
for t in s:
if t.islower()==True:
res+=t.upper()
else:
res+=t.lower()
print(res)
| 0 | null | 14,068,514,861,158 | 158 | 61 |
from fractions import gcd
A, B = map(int, input().split())
print((A * B) // gcd(A, B))
| import sys
a = sys.stdin.readlines()
for i in range(len(a)):
b = a[i].split()
m =int(b[0])
f =int(b[1])
r =int(b[2])
if m*f < 0 :
print "F"
elif m==-1 and f==-1 and r==-1:
break
else:
if m+f >= 80:
print "A"
elif 80 > m+f >=65:
print "B"
elif 65 > m+f >=50:
print "C"
elif 50 > m+f >=30 and r < 50:
print "D"
elif 50 > m+f >=30 and r >= 50:
print "C"
elif 30 > m+f:
print "F" | 0 | null | 57,126,418,686,876 | 256 | 57 |
x = list(map(int,input().split()))
ans = 0
for y in x:
if y == 1:
ans+=3
elif y == 2:
ans+=2
elif y == 3:
ans+=1
if sum(x)==2:
ans += 4
print(ans * 100000) | import math
N = int(input())
N_d = []
N1_d = []
answer = set()
for i in range(1,math.ceil(N**0.5)+1):
if N%i == 0:
N_d.append(i)
N_d.append(N//i)
for i in range(len(N_d)):
temp = N
while temp % N_d[i] == 0:
temp = temp//N_d[i]
if N_d[i] == 1:
break
if temp % N_d[i] == 1:
answer.add(N_d[i])
for i in range(1,math.ceil((N-1)**0.5)+1):
if (N-1) %i == 0:
answer.add(i)
answer.add((N-1)//i)
answer.remove(1)
print(len(answer)) | 0 | null | 90,660,034,659,852 | 275 | 183 |
arr = map(int,raw_input().split())
if arr[0] < arr[1] and arr[1] < arr[2] :
print "Yes"
else :
print "No" | a, b, c = (int(i) for i in ((input()).split()))
if a < b < c:
print('Yes')
else:
print('No') | 1 | 392,976,136,168 | null | 39 | 39 |
class dice:
value = [0] * 6
def __init__(self, list):
self.value = list
def dice_roll(self,dir):
if dir == "S":
prev = self.value
self.value = [prev[4],prev[0],prev[2],prev[3],prev[5],prev[1]]
elif dir == "N":
prev = self.value
self.value = [prev[1],prev[5],prev[2],prev[3],prev[0],prev[4]]
elif dir == "W":
prev = self.value
self.value = [prev[2],prev[1],prev[5],prev[0],prev[4],prev[3]]
elif dir == "E":
prev = self.value
self.value = [prev[3],prev[1],prev[0],prev[5],prev[4],prev[2]]
mydice = dice(list(map(int, input().split())))
x = str(input())
for i in x:
mydice.dice_roll(i)
print(mydice.value[0])
| N = int(input())
a = list(map(int, input().split()))
x = 0
for r in a:
x ^= r
print(' '.join([str(x^r) for r in a])) | 0 | null | 6,350,139,480,678 | 33 | 123 |
while(1):
H, W = map(int, input().split())
if H == 0 and W == 0:
break
for i in range(H):
for j in range(W):
print("#", end='')
print("\n", end='')
print("\n", end='') | import sys
input = sys.stdin.readline
N, M = map(int, input().split())
S = input().rstrip()
A = [0]*N
cnt = 0
S = reversed(S)
for i, s in enumerate(S):
if s=="1":
cnt += 1
A[i] = cnt
else:
cnt = 0
dp = 0
res = []
while dp+M <= N-1:
t = M - A[dp+M]
if t>0:
dp += t
else:
print(-1)
exit()
res.append(t)
res.append(N-dp)
res.reverse()
print(*res) | 0 | null | 69,920,928,046,208 | 49 | 274 |
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])
| from collections import deque
N = int(input()) # 頂点数
conj = [[] for _ in range(N)]
for i in range(N): # 各頂点での進める先の候補の集合
L = list(map(int, input().split()))
for a in L[2:]:
conj[i].append(a-1) # 頂点番号をデクリメント
start = 0
visit = [False]*N # 訪れたかどうかをメモ
second_visit = [False]*N # 二度目に訪れた事をメモ
next_set = deque() # 次に進む候補を列挙する。スタック(右から取り出す = pop)だと深さ優先になり、キュー(左から取り出す = popleft)だと幅優先になる
next_set.append((start, 0)) # スタート地点を決める。第二成分は時刻を表す。
visit[start] = True # スタート地点は最初から踏んでいる。
res = [-1 for _ in range(N)]
res[0] = 0
while next_set: # p = [] になったら止める
p, t = next_set.popleft() # 要素を消去して、消去した要素を出力
for q in conj[p]: # 頂点 p から進める経路の候補から一つ選ぶ
if not visit[q]: # 訪れた事がない場所のみ進む。壁などの条件がある場合は、ここに " grid[p] != 壁 " 等を追加する
visit[q] = True # 頂点 q に一度訪れた事をメモ (for の前に書くと、ここで選ばれた q が最短であるはずなのに、違う経路でvisit = Trueを踏んでしまう可能性がある)
res[q] = t + 1
next_set.append((q, t + 1)) # p から q へと移動。時刻を 1 進める
for i in range(N):
print(i+1, res[i])
| 1 | 4,250,968,530 | null | 9 | 9 |
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]
print('ACL'*inp()) | #!/usr/bin/env python3
def pow(N,R,mod):
ans = 1
i = 0
while((R>>i)>0):
if (R>>i)&1:
ans *= N
ans %= mod
N = (N*N)%mod
i += 1
return ans%mod
def main():
N,K = map(int,input().split())
li = [0 for _ in range(K)]
mod = 10**9+7
ans = 0
for i in range(K,0,-1):
mm = K//i
mm = pow(mm,N,mod)
for j in range(2,(K//i)+1):
mm -= li[i*j-1]
li[i-1] = mm
ans += (i*mm)%mod
ans %= mod
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 19,424,609,128,780 | 69 | 176 |
W = input()
num = 0
while True:
line = input()
if line == 'END_OF_TEXT':
break
for word in line.split():
if word.lower() == W.lower():
num += 1
print(num) | w = input().casefold()
c = 0
while True:
_ = input()
if(_ == "END_OF_TEXT"):
break
t = _.casefold().split()
c += t.count(w)
print(c) | 1 | 1,852,047,704,772 | null | 65 | 65 |
n,k = map(int,input().split())
# Function of Combination mod M
# set n >= max p expected
M = 10**9+7
if k >= n-1:
fac = [1]+[0]*(2*n-1)
for i in range(1,2*n): fac[i] = fac[i-1]*i %M
comb = lambda p,q:(fac[p]*pow(fac[q],M-2,M)*pow(fac[p-q],M-2,M))%M
y = comb(2*n-1,n-1)
else:
y = 1
c1,c2=1,1
for i in range(1,k+1):
p = pow(i,M-2,M)
c1 = c1*(n+1-i)*p%M
c2 = c2*(n-i)*p%M
y += c1*c2%M
# y = (y+c1*c2)%M
print(y%M)
| class Combination:
"""
O(n)の前計算を1回行うことで,O(1)でnCr mod mを求められる
n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
使用例:
comb = Combination(1000000)
print(comb(5, 3)) # 10
"""
def __init__(self, n_max, mod=10**9+7):
self.mod = mod
self.modinv = self.make_modinv_list(n_max)
self.fac, self.facinv = self.make_factorial_list(n_max)
def __call__(self, n, r):
if n < r:
return 0
return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod
def make_factorial_list(self, n):
# 階乗のリストと階乗のmod逆元のリストを返す O(n)
# self.make_modinv_list()が先に実行されている必要がある
fac = [1]
facinv = [1]
for i in range(1, n+1):
fac.append(fac[i-1] * i % self.mod)
facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
return fac, facinv
def make_modinv_list(self, n):
# 0からnまでのmod逆元のリストを返す O(n)
modinv = [0] * (n+1)
modinv[1] = 1
for i in range(2, n+1):
modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
return modinv
def main():
n, k = map(int,input().split())
MOD = 10 ** 9 + 7
comb = Combination(10 ** 6)
ans = 0
if n - 1 <= k:
ans = comb(n + n - 1, n)
else:
ans = 1
for i in range(1,k+1):
ans += comb(n,i) * comb(n - 1, i)
ans %= MOD
print(ans)
if __name__ == "__main__":
main() | 1 | 66,953,138,562,450 | null | 215 | 215 |
N = input()
dp = [0, 1]
for n in N:
n = int(n)
dp[0], dp[1] = min(dp[0]+n, dp[1]+10-n), min(dp[0]+n+1, dp[1]+10-(n+1))
print(dp[0]) | k=int(input())
e="ACL"
et=""
for i in range(k):
et+=e
print(et) | 0 | null | 36,742,042,485,810 | 219 | 69 |
a,b,c=map(int,input().split())
x="Yes" if a<b and a<c and b<c else "No"
print(x)
| import sys
X = int(input())
#%%
for a in range(1000):
for b in range(-1000, 1000):
if pow(a, 5) - pow(b, 5) == X:
print(a,b)
sys.exit( ) | 0 | null | 13,087,430,479,108 | 39 | 156 |
def gcd(a, b):
while b:
a, b = b, a % b
return a
def div_ceil(x, y): return (x + y - 1) // y
N, M = map(int, input().split())
*A, = map(int, input().split())
A = list(set([a // 2 for a in A]))
L = A[0]
for a in A[1:]:
L *= a // gcd(L, a)
for a in A:
if (L // a) % 2 == 0:
ans = 0
break
else:
ans = div_ceil(M // L, 2)
print(ans) | t_HP, t_A, a_HP, a_A = map(int,input().split())
ans = False
while True:
a_HP -= t_A
if a_HP <= 0:
ans = True
break
t_HP -= a_A
if t_HP <= 0:
break
if ans == True:
print("Yes")
else:
print("No") | 0 | null | 66,153,298,668,168 | 247 | 164 |
fibo = [0] * 45
N = int(input())
fibo[0] = 1
fibo[1] = 1
for i in range(2,N+1):
fibo[i] = fibo[i-1]+fibo[i-2]
print(fibo[N])
# N = int(input()) 再帰のみ
# def fibo(n):
# if n == 0 or n == 1:
# return 1
# return fibo(n-1)+fibo(n-2)
# print(fibo(N))
# N = int(input()) メモ化再帰 5596KB 635B
# def fibo(n):
# return fib2(1,1,n-1)
# def fib2(a,b,c):
# if c < 1:
# return a
# return fib2(a+b,a,c-1)
# print(fibo(N))
| n = int(input())
def fib(i):
if i<=1: return 1
else:
a=0
b=1
for i in range(n+1):
c=b+a
b=a
a=c
return c
print(fib(n))
| 1 | 1,745,843,140 | null | 7 | 7 |
def num2alpha(num):
if num<=26:
return chr(64+num)
elif num%26==0:
return num2alpha(num//26-1)+chr(90)
else:
return num2alpha(num//26)+chr(64+num%26)
n = int(input())
ans = num2alpha(n)
print(ans.lower()) | n,m,k = map(int, input().split())
g = [[] for i in range(n+1)]
f = [0]*(n+1)
b = []
for i in range(m):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
f[u] += 1
f[v] += 1
for i in range(k):
b.append(map(int, input().split()))
count = 0
renketu_list = [-1]*(n+1)
renketu_size = [0]
stack = []
for i in range(1, n+1):
if renketu_list[i] == -1:
count += 1
renketu_list[i] = count
renketu_size.append(1)
while len(g[i]) > 0:
stack.append(g[i].pop())
while len(stack) > 0:
v=stack.pop()
if renketu_list[v] == -1:
renketu_list[v] = count
renketu_size[count] += 1
while len(g[v])>0:
stack.append(g[v].pop())
s = [0] * (n+1)
for i in range(1, n+1):
s[i] += renketu_size[renketu_list[i]] - f[i] - 1
for i in range(k):
u, v = b[i]
if renketu_list[u] == renketu_list[v]:
s[u] -= 1
s[v] -= 1
print(" ".join(list(map(str, s[1:]))))
| 0 | null | 37,007,228,338,820 | 121 | 209 |
n = int(input())
Ai = list(map(int, input().split()))
sum_ans = sum(Ai)
ans = 0
mod = 1000000007
for i in range(n-1):
sum_ans -= Ai[i]
ans += sum_ans * Ai[i]
ans %= mod
print(ans) | n = int(input())
s=0
a = list(map(int, input().split()))
b = sum(a)
for i in range(n):
b -= a[i]
s += a[i]*b
s = s % ((10**9)+7)
print(s) | 1 | 3,804,490,882,310 | null | 83 | 83 |
import math
N = int(input())
n_max = int(math.sqrt(N))
a = []
for i in range(1,n_max + 1):
if N % i == 0:
a.append(i)
ans = 2 * N
for i in a:
if (i + (N // i) - 2) < ans:
ans = i + (N // i) - 2
print(ans)
| import math
N=int(input())
M =int(math.sqrt(N))
for i in range(M):
if N%(M-i)==0:
K=N//(M-i)
L=M-i
break
print(L+K-2)
| 1 | 161,198,512,458,464 | null | 288 | 288 |
X, N = map(int, input().split())
P = list(map(int, input().split()))
st = set(P)
ans = 111
for i in range(111):
if i in st:
continue
if abs(ans - X) > abs(i - X):
ans = i
print(ans)
| from collections import deque
N = int(input())
que = deque(['a'])
while len(que) > 0:
s = que.popleft()
if len(s) == N:
print(s)
else:
for i in range(ord(sorted(s)[-1]) - 95):
que.append(s + chr(97 + i)) | 0 | null | 33,138,407,805,188 | 128 | 198 |
import sys
input = sys.stdin.readline
def main():
a, b, k = map(int, input().split())
if a >= k:
print(a-k, b)
return
if a+b >= k:
k -= a
print(0, b-k)
return
if a+b < k:
print(0, 0)
return
if __name__ == "__main__":
main()
| A, B, K = map(int, input().split())
if A-K >= 0:
print(A-K, B)
else:
if B-(K-A)<0:
print(0, 0)
quit()
print(0, B-(K-A)) | 1 | 104,300,419,532,234 | null | 249 | 249 |
R, C, K = map(int, input().split())
Items = [[0] * (C + 1) for _ in range(R + 1)]
for _ in range(K):
r, c, v = map(int, input().split())
Items[r][c] = v
DP = [[0] * 4 for _ in range(R + 1)]
for x in range(1, C+1):
for y in range(1, R+1):
v = Items[y][x]
if v:
DP[y][3] = max(DP[y][3], DP[y][2] + v)
DP[y][2] = max(DP[y][2], DP[y][1] + v)
DP[y][1] = max(max(DP[y][1], DP[y][0] + v), max(DP[y-1]) + v)
DP[y][0] = max(DP[y][0], max(DP[y-1]))
else:
DP[y][0] = max(DP[y][0], max(DP[y-1]))
print(max(DP[R])) | import sys
input = sys.stdin.readline
def solve(N):
res = 0
flg = 0
for n in reversed(N):
if flg == 2:
if n >= 5:
flg = 1
else:
flg = 0
m = flg + n
if m == 5:
res += 5
flg = 2
elif m < 5:
res += m
flg = 0
else:
res += (10 - m)
flg = 1
return res + flg%2
N = list(map(int, input().strip()))
print(solve(N))
# N = 1
# q = 0
# while True:
# p = solve(list(map(int, str(N))))
# if abs(p-q) > 1:
# print(N, "OK")
# exit()
# q = p
# N += 1 | 0 | null | 38,185,833,837,552 | 94 | 219 |
N = int(input())
P = list(map(int, input().split()))
min_value = P[0]
cnt = 1
for i in range(1, N):
if P[i] < min_value :
min_value = P[i]
cnt += 1
print(cnt)
| x, y, z = input().split()
x, y = y, x
x, z = z, x
print(x, y, z) | 0 | null | 61,758,185,954,596 | 233 | 178 |
H,N = map(int,input().split())
i = map(int,input().split())
if sum(i) >= H :
print("Yes")
else :
print("No") | n,d=list(map(int,input().split()))
s=list(map(int,input().split()))
v=0
for i in s:
v+=int(i)
if v>=n:
print("Yes")
else:
print("No")
| 1 | 77,854,470,609,630 | null | 226 | 226 |
def main():
N = int(input())
x, y = map(int, input().split())
a1 = x + y
a2 = x + y
b1 = y - x
b2 = y - x
N -= 1
while N != 0:
x, y = map(int, input().split())
a1 = max(a1, x + y)
a2 = min(a2, x + y)
b1 = max(b1, y - x)
b2 = min(b2, y - x)
N = N - 1
print(max(a1 - a2, b1 - b2))
main()
| import math
def primes(N):
p = [False, False] + [True] * (N - 1)
for i in range(2, int(N ** 0.5) + 1):
if p[i]:
for j in range(i * 2, N + 1, i):
p[j] = False
return [i for i in range(N + 1) if p[i]]
N = int(input())
A = list(map(int, input().split()))
maxA = max(A) + 1
g, Ac = A[0], [0] * maxA
P = primes(maxA)
for a in A:
Ac[a] += 1
ans = "pairwise"
for p in P:
if sum(Ac[p:maxA:p]) > 1:
ans = "setwise"
for a in A:
g = math.gcd(g, a)
print(ans if g == 1 else "not", "coprime")
| 0 | null | 3,732,423,800,532 | 80 | 85 |
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()
| import sys
input = sys.stdin.readline
def main():
N, M, X = map(int, input().split())
ca = [list(map(int, input().split())) for _ in range(N)]
ans = sys.maxsize
for mask in range(2 ** N):
C = 0
A = [0] * N
for i in range(N):
if ((mask >> i) & 1):
C += ca[i][0]
A = [a + c for a, c in zip(A, ca[i][1:])]
if all([i >= X for i in A]):
ans = min(ans, C)
if ans != sys.maxsize:
print(ans)
else:
print("-1")
main() | 1 | 22,097,194,399,760 | null | 149 | 149 |
a, b=map(int,input().split())
print(str(a)*b if a<b else str(b)*a) |
a,b=map(int,input().split())
c=a>b
print(str(b if c else a)*(a if c else b))
| 1 | 84,277,898,637,768 | null | 232 | 232 |
H, N = map(int, input().split())
A = map(int, input().split())
A_sum = sum(A)
print("Yes" if H - A_sum <= 0 else "No") | import sys
input = sys.stdin.readline
from collections import *
H, N = map(int, input().split())
A = list(map(int, input().split()))
if H-sum(A)<=0:
print('Yes')
else:
print('No') | 1 | 77,796,217,983,260 | null | 226 | 226 |
def main():
# import sys
# readline = sys.stdin.readline
# readlines = sys.stdin.readlines
N = int(input())
A = [(a, i) for i, a in enumerate(map(int, input().split()))]
A.sort(reverse=True)
dp = [[-float('inf')] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
for k in range(N):
for i in range(k + 1):
j = k - i
here = dp[i][j]
a, idx = A[k]
dp[i + 1][j] = max(dp[i + 1][j], here + a * ((N - 1 - i) - idx))
dp[i][j + 1] = max(dp[i][j + 1], here + a * (idx - j))
# print(k, i)
ans = 0
for i in range(N + 1):
j = N - i
ans = max(ans, dp[i][j])
print(ans)
if __name__ == "__main__":
main()
| n, m = map(int, input().split())
a = list(map(int, input().split()))
x = 0
for i in range(m):
x += a[i]
if n - x >= 0:
print(n - x)
else:
print(-1) | 0 | null | 32,699,097,820,192 | 171 | 168 |
x = int(input())
ans = x*x*x
print(ans) |
number = int(input())
number3 = number*number*number
print(number3) | 1 | 272,705,127,452 | null | 35 | 35 |
from itertools import accumulate as acc
H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
acc_list = [list(acc([0] + s)) for s in S]
def solve(l, x, limit):
part_num = len(l)-1
ok, ng = x, W+1
while abs(ok-ng) > 1:
mid = (ok+ng)//2
score = 0
for i in range(part_num):
s = 0
for j in range(l[i], l[i+1]):
# print(i, j, mid)
s += acc_list[j][mid] - acc_list[j][x]
score = max(s, score)
if score <= limit:
ok = mid
else:
ng = mid
return ok
min_num = 10**18
for i in range(1<<(H-1)):
l = []
for j in range(H-1):
if (i>>j)&1:
l.append(j+1)
h_split_num = len(l)
l = [0] + l + [H]
x = solve(l, 0, K)
w_split_num = 0
flag = True
while x < W:
w_split_num += 1
r = solve(l, x, K)
if x == r:
flag = False
break
x = r
if flag:
split_num = w_split_num + h_split_num
min_num = min(split_num, min_num)
print(min_num)
| from collections import Counter
N, M = map(int, input().split())
class UnionFind:
def __init__(self, n):
self.root = [i for i in range(n)]
def unite(self, x, y):
if not self.same(x, y):
self.root[self.find(y)] = self.find(x)
def find(self, x):
if x == self.root[x]:
return x
else:
self.root[x] = self.find(self.root[x])
return self.root[x]
def same(self, x, y):
return self.find(x) == self.find(y)
UF = UnionFind(N)
ans = N-1
for _ in range(M):
x, y = map(int, input().split())
if not UF.same(x-1, y-1):
UF.unite(x-1, y-1)
ans -= 1
print(ans) | 0 | null | 25,356,842,943,630 | 193 | 70 |
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#l = list(map(int, input().split()))
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):
pass
else:
ans = "DENIED"
break
print(ans)
| import sys
input()
numbers = map(int, input().split())
evens = [number for number in numbers if number % 2 == 0]
if len(evens) == 0:
print('APPROVED')
sys.exit()
for even in evens:
if even % 3 != 0 and even % 5 != 0:
print('DENIED')
sys.exit()
print('APPROVED')
| 1 | 69,047,004,599,440 | null | 217 | 217 |
#!/usr/bin/env python
#-*- coding:utf-8 -*-
def selectionSort(arr, N):
counter = 0
for i in range(N):
minj = i
for j in range(i,N):
if arr[minj] > arr[j]:
minj = j
if minj != i: counter += 1
arr[i], arr[minj] = arr[minj],arr[i]
return counter,arr
def main():
N = int(raw_input())
A = map(int, raw_input().split())
counter,sortedA = selectionSort(A, N)
print ' '.join(map(str,sortedA))
print counter
#def test():
if __name__ == '__main__':
main() | n = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(n):
minj = i
for j in range(i, n):
if A[j] < A[minj]:
minj = j
else:
pass
A[i], A[minj] = A[minj], A[i]
if i != minj:
count += 1
print(*A)
print(count)
| 1 | 21,023,367,560 | null | 15 | 15 |
def main():
S, W = list(map(int, input().split()))
print("unsafe" if S <= W else "safe")
pass
if __name__ == '__main__':
main() | import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline ####
int1 = lambda x: 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)]
INF = float('inf')
def solve():
s, w = MI()
if w >= s:
print('unsafe')
else:
print('safe')
if __name__ == '__main__':
solve()
| 1 | 29,293,581,699,548 | null | 163 | 163 |
N, X, T = map(int,input().split())
n = 0
if N % X == 0:
n = N / X
else:
n = N // X + 1
print(int(n * T)) | def resolve():
n = int(input())
ans = [0]*n
a = list(map(int,input().split()))
for i in a:
ans[i-1] += 1
for i in ans:
print(i)
resolve() | 0 | null | 18,322,963,698,468 | 86 | 169 |
import math
a, b, C = map(int, input().split())
rad = C/180*math.pi
print('%.8f' % (a*b*math.sin(rad)/2))
print('%.8f' % ((a**2-2*a*b*math.cos(rad)+b**2)**0.5 + a + b))
print('%.8f' % (b * math.sin(C/180*math.pi)))
| a,b,c = [int(x) for x in input().split()]
import math
si = math.sin(math.radians(c))
co = math.cos(math.radians(c))
s = a*b*si/2
l = a + b + (a**2 + b**2 - 2*a*b*co)**(1/2)
h = 2*s/a
print("{:.6f}".format(s))
print("{:.6f}".format(l))
print("{:.6f}".format(h))
| 1 | 175,294,386,036 | null | 30 | 30 |
a, b = input().split()
print(sorted([a*int(b), b*int(a)])[0])
| import sys
sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7
def input():
return sys.stdin.readline().strip()
def main():
N, P = map(int, input().split())
S = input()
if P == 2 or P == 5:
ans = 0
for i in range(N):
if int(S[i]) % P == 0:
ans += i + 1
else:
val = [0] * P
cur = 0
tenfactor = 1
val[cur] += 1
for i in range(N):
cur = (cur + int(S[N - i - 1]) * tenfactor) % P
tenfactor *= 10
tenfactor %= P
val[cur] += 1
ans = 0
for v in val:
ans += v * (v - 1) // 2
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 71,461,898,404,390 | 232 | 205 |
num = int(input())
print('ACL'*num) | import math
x,k,d = map(int,input().split())
abs_x = abs(x)
max_len = math.ceil(abs_x/d)*d
min_len = max_len-d
if abs_x-k*d >= 0:
print(abs_x-k*d)
exit()
if (math.ceil(abs_x/d)-1)%2 == k%2:
print(abs(abs_x-min_len))
else:
print(abs(abs_x-max_len)) | 0 | null | 3,665,988,851,398 | 69 | 92 |
# D - I hate Factorization
M = 1000
N = [0]
for i in range(1,M+1):
N.append(i)
N.append(-i)
def main(X):
global N
for i in N:
for j in N:
if i**5-j**5==X:
return i,j
X = int(input())
A,B = main(X)
print(A,B) | X = int(input())
A = []
for i in range(200):
A.append(pow(i, 5))
for i in range(200):
for j in range(200):
a = abs(A[i] - A[j])
b = A[i] + A[j]
if a == X:
if i <= j:
B = [j, i]
else:
B = [i, j]
print(' '.join(map(str, B)))
exit()
elif b == X:
B = [i, -j]
print(' '.join(map(str, B)))
exit()
| 1 | 25,653,941,261,164 | null | 156 | 156 |
from collections import defaultdict
N, u, v = map(int, input().split())
d = defaultdict(list)
for _ in range(N-1):
A, B = map(int, input().split())
d[A].append(B)
d[B].append(A)
def get_dist(s):
dist = [-1]*(N+1)
dist[s] = 0
q = [s]
while q:
a = q.pop()
for b in d[a]:
if dist[b]!=-1:
continue
dist[b] = dist[a] + 1
q.append(b)
return dist
du, dv = get_dist(u), get_dist(v)
ds = [(i,j[0], j[1]) for i,j in enumerate(zip(du, dv)) if j[0]<j[1]]
ds.sort(key=lambda x:-x[2])
a, b, c = ds[0]
print(c-1) | # -*- coding: utf-8 -*-
from collections import deque
################ DANGER ################
test = ""
#test = \
"""
9 6 1
1 2
2 3
3 4
4 5
5 6
4 7
7 8
8 9
ans 5
"""
"""
5 4 1
1 2
2 3
3 4
3 5
ans 2
"""
"""
5 4 5
1 2
1 3
1 4
1 5
ans 1
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
def input2():
return test.pop()
else:
def input2():
return input()
########################################
n, taka, aoki = map(int, input2().split())
edges = [0] * (n-1)
for i in range(n-1):
edges[i] = tuple(map(int, input2().split()))
adjacencyL = [[] for i in range(n+1)]
for edge in edges:
adjacencyL[edge[0]].append(edge[1])
adjacencyL[edge[1]].append(edge[0])
################ DANGER ################
#print("taka", taka, "aoki", aoki)
#import matplotlib.pyplot as plt
#import networkx as nx
#G = nx.DiGraph()
#G.add_edges_from(edges)
#nx.draw_networkx(G)
#plt.show()
########################################
takaL = [None] * (n+1)
aokiL = [None] * (n+1)
takaL[taka] = 0
aokiL[aoki] = 0
takaQ = deque([taka])
aokiQ = deque([aoki])
for L, Q in ((takaL, takaQ), (aokiL, aokiQ)):
while Q:
popN = Q.popleft()
for a in adjacencyL[popN]:
if L[a] == None:
Q.append(a)
L[a] = L[popN] + 1
#print(L)
print(max(aokiL[i] for i in range(1, n+1) if takaL[i] < aokiL[i]) - 1)
| 1 | 116,980,299,684,260 | null | 259 | 259 |
N = int(input())
S = input()
ans = ['' for _ in range(len(S))]
a = ord('A')
for i, c in enumerate(S):
ans[i] = chr((ord(c) - a + N) % 26 + a)
print(''.join(ans)) | N,M=map(int ,input().split())
A=input().split()
s=0
for i in range(M):
s+=int(A[i])
ans=N-s
if ans>=0:
print(ans)
else:
print("-1") | 0 | null | 82,904,181,110,692 | 271 | 168 |
#!/usr/bin/env python3
import sys
import math
# import re # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict # subclass of dict. defaultdict(facroty)
# from collections import Counter # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product # product(iter, repeat=n)
# from itertools import accumulate # accumulate(iter[, f])
# from functools import reduce # reduce(f, iter[, init])
# from functools import lru_cache # @lrucache ...arguments of functions should be able to be keys of dict
from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy # to copy multi-dimentional matrix without reference
# from fractions import gcd # for Python 3.4
def main():
mod = 1000000007 # 10^9+7
inf = float('inf')
sys.setrecursionlimit(10**6) # 1000 -> 1000000
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def mi_0(): return map(lambda x: int(x)-1, input().split())
def lmi(): return list(map(int, input().split()))
def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
def li(): return list(input())
n, d, a = mi()
monsters = sorted([lmi() for _ in range(n)])
monster_points = []
monster_hps= []
for pair in monsters:
monster_points.append(pair[0])
monster_hps.append(pair[1])
# どの monster_points[ind] での ind の表すモンスターまで巻き込めるか (右端)
right_end_monster_ind = [0] * (n)
for i in range(n):
right_end_monster_ind[i] = bisect_right(monster_points, monster_points[i]+2*d) - 1
# print(right_end_monster_ind)
imos_list = [0] * (n+1)
damage = 0
ans = 0
for i in range(n):
damage += imos_list[i]
rest_hp = max(0, monster_hps[i] - damage)
# print("rest {}".format(rest_hp))
cnt = math.ceil(rest_hp / a)
ans += cnt
imos_list[i+1] += (a * cnt)
imos_list[right_end_monster_ind[i]+1] -= (a * cnt)
# print(imos_list)
print(ans)
if __name__ == "__main__":
main() | import heapq
import sys
input = sys.stdin.readline
def dijkstra_heap(s,edge,n):
#始点sから各頂点への最短距離
d = [10**20] * n
used = [True] * n #True:未確定
d[s] = 0
used[s] = False
edgelist = []
for a,b in edge[s]:
heapq.heappush(edgelist,a*(10**6)+b)
while len(edgelist):
minedge = heapq.heappop(edgelist)
#まだ使われてない頂点の中から最小の距離のものを探す
if not used[minedge%(10**6)]:
continue
v = minedge%(10**6)
d[v] = minedge//(10**6)
used[v] = False
for e in edge[v]:
if used[e[1]]:
heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
return d
def main():
N, u, v = map(int, input().split())
u -= 1
v -= 1
edge = [[] for i in range(N)]
for _ in range(N-1):
A, B = map(int, input().split())
A -= 1
B -= 1
edge[A].append([1,B])
edge[B].append([1,A])
d1 = dijkstra_heap(u,edge, N)
d2 = dijkstra_heap(v,edge, N)
ans = 0
for i in range(N):
if d1[i] < d2[i]:
ans = max(ans, d2[i]-1)
print(ans)
if __name__ == "__main__":
main() | 0 | null | 99,659,788,277,838 | 230 | 259 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.