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
|
---|---|---|---|---|---|---|
import math
n = int(input())
m = 100000.0
for i in range(n):
m = math.ceil((m * 1.05)/1000)*1000
print(int(m))
|
N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = 0
for k in range(1, N): ans += A[N-k//2-1]
print(ans)
| 0 | null | 4,623,842,773,788 | 6 | 111 |
from collections import deque
n,limit = map(int,input().split())
total_time = 0
queue = deque()
for _ in range(n):
p,t = input().split()
queue.append([p,int(t)])
while len(queue) > 0:
head = queue.popleft()
if head[1] <= limit:
total_time += head[1]
print('{} {}'.format(head[0],total_time))
else:
head[1] -= limit
total_time += limit
queue.append(head)
|
# -*- coding: utf-8 -*-
def round_robin_scheduling(process_name, process_time, q):
result = []
elapsed_time = 0
while len(process_name) > 0:
# print process_name
# print process_time
if process_time[0] <= q:
elapsed_time += process_time[0]
process_time.pop(0)
result.append(process_name.pop(0) + ' ' + str(elapsed_time))
else:
elapsed_time += q
process_name.append(process_name.pop(0))
process_time.append(process_time.pop(0) - q)
return result
if __name__ == '__main__':
N, q = map(int, raw_input().split())
process_name, process_time = [], []
for i in xrange(N):
process = raw_input().split()
process_name.append(process[0])
process_time.append(int(process[1]))
# N, q = 5, 100
# process_name = ['p1', 'p2', 'p3', 'p4', 'p5']
# process_time = [150, 80, 200, 350, 20]
result = round_robin_scheduling(process_name, process_time, q)
for i in xrange(len(result)):
print result[i]
| 1 | 42,365,572,370 | null | 19 | 19 |
SIZE = 2**20 # 2**20 > N=500000
class SegmentTree:
def __init__(self, size):
self.size = size
self.seg = [0] * (2 * size)
def update(self, pos, ch):
# update leaf
i = self.size + pos - 1
self.seg[i] = 1 << (ord(ch)-ord('a'))
# update tree
while i > 0:
i = (i - 1) // 2
self.seg[i] = self.seg[i*2+1] | self.seg[i*2+2]
def _query(self, a, b, k, left, right):
if right<a or b<left:
return 0
if a<=left and right<=b:
return self.seg[k]
vl = self._query(a,b,k*2+1, left, (left+right)//2)
vr = self._query(a,b,k*2+2, (left+right)//2+1, right)
return vl | vr
def query(self, a, b):
return self._query(a,b,0,0,self.size-1)
def resolve():
N = int(input())
S = input().strip()
Q = int(input())
table = [[] for _ in range(26)]
sg = SegmentTree(SIZE)
for i,ch in enumerate(S):
sg.update(i, ch)
for i in range(Q):
query = input().strip().split()
if query[0] == '1':
pos = int(query[1])-1
sg.update(pos, query[2])
else:
left = int(query[1])-1
right = int(query[2])-1
bits = sg.query(left, right)
count = 0
for j in range(26):
count += (bits>>j) & 1
print(count)
resolve()
|
def bubblesort(n, a):
r = a[:]
for i in range(n):
for j in range(n-1, i, -1):
if int(r[j][1]) < int(r[j-1][1]):
r[j], r[j-1] = r[j-1], r[j]
print(*r)
print("Stable")
def selectionsort(n, a):
r = a[:]
ret = "Stable"
for i in range(n):
tmp = i
for j in range(i, n):
if int(r[j][1]) < int(r[tmp][1]):
tmp = j
r[i], r[tmp] = r[tmp], r[i]
for i in range(n):
for j in range(i+1, n):
for k in range(n):
for l in range(k+1, n):
if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
ret = "Not stable"
print(*r)
print(ret)
def main():
n = int(input())
a = list(input().split())
bubblesort(n, a)
selectionsort(n, a)
if __name__ == '__main__':
main()
| 0 | null | 31,400,686,990,118 | 210 | 16 |
N=input()
i=0
while i<len(N):
print("x" ,end='')
i+=1
|
s,w=input().split()
s=int(s)
w=int(w)
if s>w:
print("safe")
if w>s:
print("unsafe")
if w==s:
print("unsafe")
| 0 | null | 51,089,327,499,472 | 221 | 163 |
import sys
def main():
input=sys.stdin.readline
S=input().strip()
dp=[[0,0] for i in range(len(S)+1)]
dp[0][1]=1
for i in range(1,len(S)+1):
for j in (0,1):
if j==0:
dp[i][0]=min(dp[i-1][0]+int(S[i-1]),dp[i-1][1]+10-int(S[i-1]))
elif j==1:
dp[i][1]=min(dp[i-1][0]+int(S[i-1])+1,dp[i-1][1]+10-int(S[i-1])-1)
print(dp[len(S)][0])
if __name__=="__main__":
main()
|
import math
h,w = map(int, input().split())
print(1 if h==1 or w==1 else math.ceil(h*w/2))
| 0 | null | 60,808,236,977,390 | 219 | 196 |
a=input()
print(a.replace('?','D'))
|
a, b, c = [int(n) for n in input().split()]
d = c-a-b
if d < 0:
print("No")
elif 4*a*b < d*d:
print("Yes")
else:
print("No")
#a + 2sqrt(ab) + b < c
#sqrt(4ab) < c-a-b
#4ab < d^2
| 0 | null | 35,076,827,669,748 | 140 | 197 |
import sys
import re
stri = sys.stdin.readline()
ret = re.match( '^(hi)+$', stri)
if ret:
print("Yes")
else:
print("No")
sys.stdout.flush()
|
from bisect import bisect_right as br
from itertools import accumulate as ac
n,m,k=map(int,input().split())
a=list(ac([0]+list(map(int,input().split()))))
b=list(ac([0]+list(map(int,input().split()))))
l=0
for i in range(n+1):
if k<a[i]:
break
else:
l=max(l,i+br(b,k-a[i])-1)
print(l)
| 0 | null | 31,835,371,184,928 | 199 | 117 |
N = int(input())
A = list(map(int, input().split()))
if N == 0:
if A[0] == 1:
print(1)
else:
print(-1)
exit()
elif A[0] != 0:
print(-1)
exit()
leaf = A
not_leaf = [0] * (N + 1)
not_leaf[0] = 1
for i, a in enumerate(A[1:-1], 1):
not_leaf[i] = not_leaf[i - 1] * 2 - a
for i in reversed(range(1, N + 1)):
total = not_leaf[i] + leaf[i]
prev = not_leaf[i - 1]
if prev > total:
not_leaf[i - 1] -= prev - total
if not_leaf[i - 1] * 2 < total:
print(-1)
break
else:
print(sum(leaf) + sum(not_leaf))
|
import sys
n = int(raw_input())
R = [int(raw_input()) for i in xrange(n)]
minimum = sys.maxint
maximum = -sys.maxint - 1
for x in R:
p = x - minimum
if maximum < p:
maximum = p
if minimum > x:
minimum = x
print maximum
| 0 | null | 9,388,391,940,070 | 141 | 13 |
f = lambda: int(input())
d = -float('inf')
n = f()
l = f()
for _ in range(n-1):
r = f()
d = max(d, r-l)
l = min(l, r)
print(d)
|
n = int(input())
R =[]
for i in range(n):
R.append(int(input()))
maxR = R[1]
minR = R[0]
maxB = R[1] - R[0]
for i in range(1,n):
if R[i] < minR:
minR = R[i]
maxR = R[i] - 1
elif minR <= R[i] and R[i] <= maxR:
continue
else:
maxR = R[i]
if maxR - minR > maxB:
maxB = maxR - minR
print(str(maxB))
| 1 | 13,348,111,348 | null | 13 | 13 |
N,M = [int(x) for x in input().split()]
S = list(input())
S1 = S[::-1]
ind = 0
ans = []
while ind!=N:
ind1 = ind
for i in range(M,0,-1):
if ind+i<=N and S1[ind+i]=="0":
ind = ind+i
ans.append(i)
break
if ind1==ind:
print(-1)
exit()
print(*ans[::-1])
|
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N, M = mapint()
S = list(input())
def solve():
now = N
choice = []
while 1:
if now==0:
return choice[::-1]
for m in range(M, 0, -1):
nx = now-m
if nx<0: continue
if S[nx]=='1': continue
now = nx
choice.append(m)
break
else:
return [-1]
print(*solve())
| 1 | 138,660,587,313,194 | null | 274 | 274 |
n = int(input())
A = list(map(int, input().split()))
total = 0
min_ = A[0]
for i in range(n):
if A[i] > min_:
min_ = A[i]
else:
total += (min_ - A[i])
print(total)
|
(h, n), *m = [[*map(int, i.split())] for i in open(0)]
dp = [0] + [10**9] * h
for i in range(1, h + 1):
for a, b in m:
dp[i] = min(dp[i], dp[max(i-a,0)]+b)
print(dp[-1])
| 0 | null | 42,888,943,952,800 | 88 | 229 |
#!/usr/bin/env python
# encoding: utf-8
import sys
class Solution:
def __init__(self):
self.count = 0
def shell_sort(self):
array_length = int(input())
# array = []
# array_length = 10
# array = [15, 12, 8, 9, 3, 2, 7, 2, 11, 1]
array = list(map(int, sys.stdin.readlines()))
G = [1]
while True:
g = G[0] * 3 + 1
if g > array_length:
break
G.insert(0, g)
g_length = len(G)
result = []
for j in range(g_length):
result = self.insertion_sort(array=array, array_length=array_length, g=G[j])
# print(result, 'r', G[j])
print(g_length)
print(" ".join(map(str, G)))
print(self.count)
for k in range(array_length):
print(result[k])
def insertion_sort(self, array, array_length, g):
# write your code here
for i in range(g, array_length):
v = array[i]
j = i - g
while j >= 0 and array[j] > v:
array[j + g] = array[j]
j -= g
self.count += 1
array[j + g] = v
# print(" ".join(map(str, array)))
return array
if __name__ == '__main__':
solution = Solution()
solution.shell_sort()
|
n = int(input())
a = [int(x) for x in input().split()]
sm_e = []
sm_o = []
e = 0
o = 0
for i in range(n):
if i % 2 == 0:
e += a[i]
sm_e.append(e)
else:
o += a[i]
sm_o.append(o)
tot_e = sm_e[-1]
tot_o = sm_o[-1]
m = len(sm_o)
if n % 2 == 0:
ans = max(tot_e, tot_o)
for i in range(m):
ans = max(ans, sm_e[i] + tot_o - sm_o[i])
# for i in range(m-1):
# ans = max(ans, sm_o[i] + tot_e - sm_e[i+1])
print(ans)
exit()
ans = tot_o
for i in range(m):
ans = max(ans, sm_e[i]+tot_o-sm_o[i])
l = [0]*m
for i in range(m):
l[i] = sm_e[i]-sm_o[i]
val = l[0]
tmp = 0
for i in range(m-1):
ans = max(ans, sm_o[i] + tot_e - sm_e[i+1])
if i != 0:
if val < l[i]:
val = l[i]
ans = max(ans, sm_o[i] + tot_e - sm_e[i+1] + val)
for i in range(n):
if i % 2 == 0:
ans = max(ans, tot_e - a[i])
print(ans)
| 0 | null | 18,838,361,341,300 | 17 | 177 |
mol=[]
for i in range(10):
mol.append(int(input()))
mol.sort(key=None,reverse=True)
for i in range(3):
print(mol[i])
|
list1=[]
for i in range(10):
list1.append(int(input()))
list1=sorted(list1)
list1.reverse()
for i in range(3):
print(list1[i])
| 1 | 31,693,440 | null | 2 | 2 |
L = int(input())
ans = L**3/27
print(ans)
|
l = int(input())
x = float(l / 3)
ans = float(x**3)
print(ans)
| 1 | 47,060,248,514,650 | null | 191 | 191 |
L = int(input())
num = L / 3
print(num**3)
|
L=int(input())
a=L/3
if L%3==0:
print((L//3)**3)
else:
print(a**3)
| 1 | 47,257,616,788,358 | null | 191 | 191 |
n = int(input())
d = {}
for i in range(n):
s = input()
if s not in d:
d[s] = 1
else:
d[s] += 1
m = max(d.values())
ans = [_ for _ in d if d[_] == m]
ans.sort()
[print(_) for _ in ans]
|
# -*- coding: utf-8 -*-
# C
import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline
# 再起回数上限変更
# sys.setrecursionlimit(1000000)
import math
a, b, c = map(int, input().split())
a1 = a + b - c
if a1 >0:
print('No')
sys.exit()
if a1**2 - 4 * a * b > 0:
print('Yes')
else:
print('No')
| 0 | null | 60,599,175,241,220 | 218 | 197 |
n = int(input())
s = input()
cnt = 0
for i in range(len(s)):
if s[i] == "A" and i+2 <= len(s)-1:
if s[i+1] == "B":
if s[i+2] == "C":
cnt += 1
print(cnt)
|
# encoding:utf-8
while True:
L = list(map(int, input().split(" ")))
if L[0] == 0 and L[1] == 0:
break
L.sort()
print("{0} {1}".format(L[0],L[1]))
| 0 | null | 49,863,155,310,440 | 245 | 43 |
def gcd(a, b):
while a != 0:
b = b%a
a, b = b, a
return b
def lcm(a, b):
return (a*b)//gcd(a,b)
n, m = map(int, input().split())
a = list(map(int, input().split()))
x = 1
for y in a:
x = lcm(x, y//2)
for y in a:
if (x//(y//2)) % 2 == 0:
print(0)
exit()
max_n = (m-x)//(2*x)
print(max_n+1)
|
a, b, c = map(int, input().split())
if 2*(a*b + (a+b)*c) < a**2+b**2+c**2 and c > a + b:
print('Yes')
else:
print('No')
| 0 | null | 76,667,843,494,748 | 247 | 197 |
H, N = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
print("Yes") if H - sum(A) <= 0 else print("No")
|
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline().strip()
h, w = [int(x) for x in input().split()]
g = [input() for _ in range(h)]
ans = 0
for si in range(h):
for sj in range(w):
if g[si][sj] != '#':
d = [[-1 for j in range(w)] for i in range(h)]
d[si][sj] = 0
q = [(si, sj)]
for i, j in q:
ans = max(ans, d[i][j])
for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
ni, nj = i + di, j + dj
if 0 <= ni < h and 0 <= nj < w and g[ni][nj] != '#' and d[ni][nj] == -1:
d[ni][nj] = d[i][j] + 1
q.append((ni, nj))
print(ans)
| 0 | null | 86,290,755,763,652 | 226 | 241 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
MOD = 1000000007
def smax(a,b):
if a>b:
return a
else:
return b
def smin(a,b):
if a<b:
return a
else:
return b
def main():
N = int(readline())
A = list(map(int,readline().split()))
if N%2==0:
A.append(0)
DP = [[[-10**27]*2 for j in range(2)] for i in range(N+2)]
DP[0][0][0] = 0
for i in range(N+1):
DP[i+1][0][0] = DP[i][0][1]
DP[i+1][1][0] = smax(DP[i][0][0],DP[i][1][1])
DP[i+1][0][1] = DP[i][0][0]+A[i]
DP[i+1][1][1] = DP[i][1][0]+A[i]
#print(DP[i+1][1][0])
print(DP[N+1][1][0])
else:
A.append(0)
DP = [[[-10**27]*2 for j in range(3)] for i in range(N+2)]
DP[0][0][0] = 0
for i in range(N+1):
DP[i+1][0][0] = DP[i][0][1]
DP[i+1][1][0] = smax(DP[i][0][0],DP[i][1][1])
DP[i+1][2][0] = smax(DP[i][1][0],DP[i][2][1])
DP[i+1][0][1] = DP[i][0][0]+A[i]
DP[i+1][1][1] = DP[i][1][0]+A[i]
DP[i+1][2][1] = DP[i][2][0]+A[i]
#print(DP[i+1][1][0])
print(DP[N+1][2][0])
if __name__ == '__main__':
main()
|
l,r,d = [int(x) for x in input().split()]
nums = [int(x) for x in range(l,r+1)]
ans = []
for i in range(len(nums)):
if nums[i]%d == 0:
ans.append(nums[i])
print(len(ans))
| 0 | null | 22,601,197,219,240 | 177 | 104 |
def bisect_left(L,x):
lo=0
hi=N
while lo<hi:
mid=(lo+hi)//2
if L[mid]<x: lo=mid+1
else: hi=mid
return lo
N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
ans=0
for i in range(N-2):
a=L[i]
for j in range(i+1,N-1):
b=L[j]
"""
lo=0
hi=N
x=a+b
while lo<hi:
mid=(lo+hi)//2
if L[mid]<x: lo=mid+1
else: hi=mid
ans+=lo-(j+1)
"""
ans+=bisect_left(L,a+b)-(j+1)
print(ans)
|
class BIT:
def __init__(self, n):
self.n = n
self.data = [0]*(n+1)
def to_sum(self, i):
s = 0
while i > 0:
s += self.data[i]
i -= (i & -i)
return s
def add(self, i, x):
while i <= self.n:
self.data[i] += x
i += (i & -i)
def get(self, i, j):
return self.to_sum(j)-self.to_sum(i-1)
N = int(input())
A = list(map(int,input().split()))
A.sort()
M = pow(10,6)+ 100
Tree = BIT(M)
for x in A:
Tree.add(x,1)
ans = 0
for i in range(N):
for j in range(i+1,N):
x = A[i]; y = A[j]
Tree.add(x,-1);Tree.add(y,-1)
MIN = abs(x-y)+1; MAX = (x+y)-1
#print(x,y,MIN,MAX)
ans += Tree.get(MIN,MAX)
Tree.add(x,1);Tree.add(y,1)
#print(ans)
print(ans//3)
| 1 | 171,994,481,362,112 | null | 294 | 294 |
for m, f, r in ((int(j) for j in i.split()) for i in iter(input, "-1 -1 -1")):
if m == -1 or f == -1:
print ('F')
elif m + f >= 80:
print ('A')
elif m + f >= 65:
print ('B')
elif m + f >= 50 or (m + f >= 30 and r >= 50):
print ('C')
elif m + f >= 30:
print ('D')
else:
print ('F')
|
import sys
num = int(sys.stdin.readline().strip())
r0 = int(sys.stdin.readline().strip())
r1 = int(sys.stdin.readline().strip())
max_dis = r1 - r0
min_num = min(r0, r1)
for i in range(0, num-2):
r = int(sys.stdin.readline().strip())
max_dis = max(max_dis, r - min_num)
min_num = min(min_num, r)
print max_dis
| 0 | null | 628,208,583,420 | 57 | 13 |
import sys
def resolve(in_):
s, w = map(int, next(in_).split())
return 'safe' if s > w else 'unsafe'
def main():
answer = resolve(sys.stdin.buffer)
print(answer)
if __name__ == '__main__':
main()
|
from fractions import gcd
def lcm(a, b):
return a * b // gcd(a, b)
n = int(input())
a = list(map(int,input().split()))
mod = 10 ** 9 + 7
LCM = 1
a_mod = 0
for i in range(n):
LCM = lcm(LCM, a[i])
a_mod = (a_mod + pow(a[i], mod - 2, mod)) % mod
LCM = LCM % mod
ans = LCM * a_mod
print(ans % mod)
| 0 | null | 58,500,430,002,068 | 163 | 235 |
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
manhattan = 0
euclid = 0
euclid2 = 0
chebishef = 0
for i, j in zip(x, y):
tmp = abs(i - j)
manhattan += tmp
euclid += tmp * tmp
euclid2 += tmp * tmp * tmp
chebishef = max(chebishef, tmp)
print(manhattan)
print(math.sqrt(euclid))
print(math.pow(euclid2, 1 / 3))
print(chebishef)
|
def dist(x,y,p):
tmp = 0
for k,j in zip(x,y):
k,j = sorted((k,j))
tmp += (j-k)**p
print tmp**(1/float(p))
def cheb(x,y):
maxjk = None
for k,j in zip(x,y):
k,j = sorted((k,j))
if maxjk is None:
maxjk = j-k
elif j-k > maxjk:
maxjk = j-k
print float(maxjk)
n = int(raw_input())
x = map(int,raw_input().split())
y = map(int,raw_input().split())
dist(x,y,1)
dist(x,y,2)
dist(x,y,3)
cheb(x,y)
| 1 | 209,472,182,150 | null | 32 | 32 |
N = int(input())
Numbers = list(map(int,input().split()))
temp_min = Numbers[0]
count = 0
for i in range(N):
if Numbers[i] <= temp_min:
temp_min = Numbers[i]
count = count+1
print(count)
|
def sep():
return map(int,input().strip().split(" "))
def lis():
return list(sep())
s=input()
if s[-1]=="s":
print(s+"es")
else:
print(s+"s")
| 0 | null | 43,940,767,922,240 | 233 | 71 |
n = int(input())
a = [list(map(int,input().split())) for i in range(n)]
sum = 0
for i in range(n):
for j in range(i+1,n):
sum += ((a[i][0]-a[j][0])**2+(a[i][1]-a[j][1])**2)**0.5
print(2*sum/n)
|
line = input()
s, t = line.split(" ")
ans = t + s
print(ans)
| 0 | null | 125,622,549,402,648 | 280 | 248 |
import sys
import numpy as np
from scipy.sparse.csgraph import shortest_path
from scipy.sparse import csr_matrix
input = sys.stdin.readline
n, m, l = list(map(int, input().split()))
g_dense = np.zeros((n, n))
for _ in range(m):
a, b, c = map(int, input().split())
g_dense[a-1][b-1] = c
q = int(input())
st = [list(map(int, input().split())) for _ in range(q)]
path = shortest_path(g_dense, directed=False)
g_dense = np.zeros((n, n))
for i in range(n):
for j in range(n):
if path[i][j] <= l:
g_dense[i][j] = 1
path = shortest_path(g_dense, directed=False)
INF = float('inf')
for s,t in st:
ans = path[s-1][t-1]
if ans == INF:
print(-1)
else:
print(int(ans) - 1)
|
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx): return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
toret = {}
for tup in temp:
toret[tup[0]] = tup[1]
return toret
def sortDictWithKey(passedDic):
return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
n = INT()
a = LIST()
flag = 0
for el in a:
if(el%2==0):
if(el%3==0 or el%5==0):
continue
else:
flag = 1
break
if(flag==0):
print("APPROVED")
else:
print("DENIED")
| 0 | null | 120,838,928,497,408 | 295 | 217 |
n = int(input())
P = list(map(int, input().split()))
minSoFar = 2 * 10**5 + 1
ans = 0
for i in range(n):
if P[i] < minSoFar:
ans += 1
minSoFar = min(minSoFar, P[i])
print(ans)
|
N = int(input())
N_List = list(map(int,input().split()))
ct = 0
Current_Min = 2*(10**5) + 1
for i in range(N):
if N_List[i] <= Current_Min:
ct += 1
Current_Min = N_List[i]
print(ct)
| 1 | 85,409,113,116,218 | null | 233 | 233 |
A, B, C, K = [int(_) for _ in input().split()]
print(K if K <= A else A if K <= A + B else A - (K - A - B))
|
tmp = input().split()
a = int(tmp[0])
b = int(tmp[1])
c = int(tmp[2])
k = int(tmp[3])
if(a>=k):
print(k)
elif(a+b>=k):
print(a)
else:
print(a-(k-a-b))
| 1 | 21,879,152,456,996 | null | 148 | 148 |
from functools import lru_cache
@lru_cache(maxsize=None)
def solve(i, count):
global ans
global a
if count >= N:
return
if ans[i][0] == -1:
ans[i][0] = count
elif ans[i][0] > count:
ans[i][0] = count
for x in range(a[i][1]):
solve(a[i][2 + x] - 1, count + 1)
N = int(input())
a = []
for _ in range(N):
a.append([int(x) for x in input().split()])
ans = [[-1 for i in range(1)] for j in range(N)]
solve(0, 0)
for i, x in enumerate(ans):
print(i + 1, *x)
|
from math import gcd
from itertools import product
N = int(input())
print(sum(gcd(gcd(a,b),c) for a, b, c in product(range(1,N+1), repeat=3)))
| 0 | null | 17,760,937,326,900 | 9 | 174 |
N=int(input())
alpha='abcdefghijklmnopqrstuvwxyz'
ans=''
while 26<N:
s=(N-1)%26
ans=alpha[s]+ans
N=int(N-1)//26
ans=alpha[N-1]+ans
print(ans)
|
# -*- coding: utf-8 -*-
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)
def base_10_to_n(X, n):
if (int(X/n)):
return base_10_to_n(int(X/n), n)+str(X % n)
return str(X % n)
def main():
n = int(input())
alpha = num2alpha(n)
print(alpha.lower())
if __name__ == "__main__":
main()
| 1 | 11,964,035,445,210 | null | 121 | 121 |
n = int(input()) / 2 + 0.01
print(int(round(n)))
|
D, T, S = map(float, input().split())
if D / S <= T:
print("Yes")
else:
print("No")
| 0 | null | 31,256,170,562,902 | 206 | 81 |
n,k,s = map(int,input().split())
rem = s+1
if rem > 10**9:
rem = 1
li = []
for i in range(n):
if i < k:
li.append(s)
else:
li.append(rem)
print(*li)
|
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 # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
def readInts():
return list(map(int,input().split()))
def main():
n,k,s = readInts()
ans = []
for i in range(k):
ans.append(s)
for i in range(k,n):
if s >= 100:
ans.append(s-1)
else:
ans.append(s+1)
print(*ans)
if __name__ == '__main__':
main()
| 1 | 90,903,106,070,500 | null | 238 | 238 |
k = int(input())
x = 7 % k
for i in range(1, k+1):
if x == 0:
print(i)
exit()
x = (x*10+7)%k
print(-1)
|
import sys
RS = lambda: sys.stdin.readline()[:-1]
RI = lambda x=int: x(RS())
RA = lambda x=int: map(x, RS().split())
RSS = lambda: RS().split()
def solve():
ans = []
for i in s:
if i == "P":
ans.append("P")
else:
ans.append("D")
print("".join(ans))
return
s = RS()
solve()
| 0 | null | 12,302,802,536,090 | 97 | 140 |
H = int(input())
W = int(input())
N = int(input())
n = max(H, W)
ans = 1
while True:
if n >= N:
break
n += max(H, W)
ans += 1
print(ans)
|
h = int(input())
cnt = 1
ans = 0
while h:
ans += cnt
h //= 2
cnt *= 2
print(ans)
| 0 | null | 84,345,553,800,690 | 236 | 228 |
n=int(input())
a=list(map(int,input().split()))
m=len(a)
a=set(a)
n=len(a)
if m==n:
print("YES")
else:
print("NO")
|
n=int(input())
a=[int(v) for v in input().split()]
check=1
a.sort()
for i in range (0,n-1):
if a[i]==a[i+1]:
check=0
break
if check==0:
print("NO")
else:
print("YES")
| 1 | 74,005,208,404,622 | null | 222 | 222 |
k=int(input())
s=list(input())
if len(s)<=k:
print("".join(s))
else:
s_short=""
for i in range(k):
s_short+=s[i]
print((s_short+"..."))
|
#Scoring
import numpy as np
D = int(input())
C = np.array(list(map(int, input().split())))
S = np.zeros((D,26),int)
for i in range(D):
S[i] = list(map(int, input().split()))
t = np.zeros(D,int)
for i in range(D):
t[i] = int(input())
score = int(0)
ld = -np.ones(26,int)
tscore = np.zeros(26,int)
for i in range(D):
cid = t[i]-1
ld[cid] = i
score -= np.sum(C*(i-ld))
score += S[i,cid]
print(score)
| 0 | null | 14,842,597,796,780 | 143 | 114 |
x = int(input())
b = 100
for i in range(1,10**18):
b += b//100
if b >= x:
print(i)
break
|
N,K=map(int,input().split())
pi=list(map(int,input().split()))
pi_sort=sorted(pi,reverse=False)
ans=0
for i in range(K):
ans+=pi_sort[i]
print(ans)
| 0 | null | 19,415,682,486,460 | 159 | 120 |
Ss = input().rstrip()
print('x' * len(Ss))
|
s = input()
l = len(s)
print('x'*l)
| 1 | 73,238,362,467,710 | null | 221 | 221 |
import bisect
import math
N, D, A = map(int, input().split())
XH_array = [list(map(int, input().split())) for _ in range(N)]
XH_array = sorted(XH_array, key=lambda x: x[0])
X_array = [xh[0] for xh in XH_array]
# print(X_array)
ans = 0
bomb_count = [0] * N
now_bomb = 0
for i, xh in enumerate(XH_array):
# print(now_bomb, bomb_count)
x, h = xh
remain = h - A * now_bomb
if remain <= 0:
now_bomb += bomb_count[i]
continue
bomb_add = math.ceil(remain / A)
ans += bomb_add
bomb_count[i] += bomb_add
bomb_end = bisect.bisect_right(X_array, x + 2 * D)
bomb_count[bomb_end - 1] -= bomb_add
now_bomb += bomb_count[i]
print(ans)
|
a = list(map(int, input().split()))
print('un' * (a[0] <= a[1]) + 'safe')
| 0 | null | 55,636,861,419,842 | 230 | 163 |
N=list(input())
print('Yes' if '7' in N else 'No')
|
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()
| 0 | null | 85,098,901,118,738 | 172 | 272 |
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions import gcd
#input=sys.stdin.readline
import bisect
n=int(input())
s=input()
ans=n
for i in range(1,n):
if s[i]==s[i-1]:
ans-=1
print(ans)
|
#!/usr/bin/env python3
def main():
_ = int(input())
A = [int(x) for x in input().split()]
flag = True
for a in A:
if a % 2 == 0:
if a % 3 == 0 or a % 5 == 0:
pass
else:
flag = False
break
if flag:
print('APPROVED')
else:
print('DENIED')
if __name__ == '__main__':
main()
| 0 | null | 119,878,723,581,892 | 293 | 217 |
import sys
input = sys.stdin.buffer.readline
r, c, k = map(int, input().split())
v = [[0]*(c+1) for i in range(r+1)]
for i in range(k):
cr, cc, cv = map(int, input().split())
v[cr][cc] = cv
dp = [[0]*4 for _ in range(c+1)]
last = [[0]*4 for _ in range(c+1)]
for cr in range(1, r+1):
for cc in range(1, c+1):
upper = max(last[cc][0], last[cc][1], last[cc][2], last[cc][3])
dp[cc][0] = max(upper, dp[cc-1][0], dp[cc][0])
dp[cc][1] = max(dp[cc-1][1], dp[cc-1][0]+v[cr][cc], upper+v[cr][cc])
dp[cc][2] = max(dp[cc-1][2], dp[cc-1][1]+v[cr][cc])
dp[cc][3] = max(dp[cc-1][3], dp[cc-1][2]+v[cr][cc])
last = dp
print(max(dp[c]))
|
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
from functools import reduce
n,k = list(map(int, input().split()))
a = n*(n+k+1)*(n-k+2)//2
b = n-k+2
c = -((n+2)*(n+1)*n - k*(k-1)*(k-2))//3
print((a+b+c) % (10**9+7))
| 0 | null | 19,218,808,984,970 | 94 | 170 |
a, b = map(int, input().split())
c = str(min(a, b))
print(c*max(a, b))
|
x = list(map(int, input().split()))
ans = 0
for i in range(5):
if x[i] == 0:
ans = i+1
print(ans)
| 0 | null | 49,009,684,798,890 | 232 | 126 |
N = int(input())
c = 0
for i in range(1,N):
if i**2 >= N:
break
c += 1
for i in range(1,N):
for j in range(i+1,N):
if i*j >= N:
break
c += 2
print(c)
|
N=int(input())
count=0
#N未満の値がaで割り切れさえすればいい
for a in range(1,N):
count = count + (N-1)//a
print(count)
| 1 | 2,554,934,206,078 | null | 73 | 73 |
x = input()
x = x.split(" ")
x = [int(z) for z in x]
print("%d %d %f" % (x[0]//x[1], x[0]%x[1], x[0]/x[1]))
|
n, k = map(int, input().split())
MOD = 1000000007
ans = 0
c = {}
for i in range(k, 0, -1):
t = pow(k // i, n, MOD)
m = 2
while i * m <= k:
t -= c[i * m]
m += 1
c[i] = t % MOD
print(sum([k * v for k, v in c.items()]) % MOD)
| 0 | null | 18,829,217,648,230 | 45 | 176 |
n,k =list(map(int,input().split()))
for i in range(31):
if n < k ** i:
print(i)
break
|
N,K=map(int,input().split())
def ans156(N:int, K:int):
length=1#0桁はないため、1からスタート
while True:
if N<K:
break
N=int(N/K)#割り切れるごとに1桁ずつ増えていく。
length+=1
return length
print(ans156(N,K))
| 1 | 64,364,173,271,552 | null | 212 | 212 |
def gcd(x, y):
while y > 0:
r = x % y
x = y
y = r
return x
def lcm(x, y):
return x // gcd(x, y) * y
n, m = map(int, input().split())
a = list(map(int, input().split()))
l = 1
for i in range(n):
a[i] //= 2
l = lcm(a[i], l)
flg = True
for x in a:
if (l // x) % 2 == 0:
flg = False
break
if flg:
print(m // l - m // (l * 2))
else:
print(0)
|
from math import gcd, ceil
N,M = map(int,input().split())
A = list(map(int,input().split()))
A = [a//2 for a in A]
B = 1
for a in A:
B*=a//gcd(B,a)
for a in A:
if B//a%2==0:
print(0)
exit()
print(ceil((M//B)/2))
| 1 | 101,453,946,224,500 | null | 247 | 247 |
m1 = 0;m2 = 0;m3 = 0
for i in range(1,11):
m = int(input())
if m>=m1:
m3 = m2;m2 = m1;m1 = m
elif m2<=m<m1:
m3 = m2;m2 = m
elif m3<=m<m2:
m3 = m
print(m1)
print(m2)
print(m3)
|
p1 = 0
p2 = 0
p3 = 0
for i in range(10):
d = int(raw_input())
if (d > p1):
p3 = p2
p2 = p1
p1 = d
elif (d > p2):
p3 = p2
p2 = d
elif (d > p3):
p3 = d
print p1
print p2
print p3
| 1 | 34,759,168 | null | 2 | 2 |
A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
X = []
Y = []
C = []
for _ in range(M):
x,y,c = map(int,input().split())
X.append(x)
Y.append(y)
C.append(c)
ans = min(a) + min(b)
for i in range(M):
ans = min(ans,a[X[i]-1] + b[Y[i] - 1] - C[i])
print(ans)
|
# author: Taichicchi
# created: 20.09.2020 11:13:28
import sys
from math import factorial
from scipy.special import comb
MOD = 10 ** 9 + 7
S = int(input())
m = S // 3
cnt = 0
for n in range(1, m + 1):
cnt += int(comb(S - 3 * n + 1, n - 1, exact=True, repetition=True)) % MOD
cnt %= MOD
print(cnt)
| 0 | null | 28,597,827,023,680 | 200 | 79 |
import bisect
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
B = [0] * N
B[-1] = A[-1]
for i in range(N - 2, -1, -1):
B[i] = B[i+1]+A[i]
def C(mid):
tmp = 0
for a in A:
pos = bisect.bisect_right(A, mid - a)
tmp += N-pos
return tmp > M
lb = 0
rb = 10**6
while rb - lb > 1:
happy = (lb + rb) // 2
if C(happy):
lb = happy
else:
rb = happy
ans = 0
cnt = 0
for a in A:
pos = bisect.bisect_right(A, rb - a)
if pos == N:
continue
ans += B[pos]+(N-pos)*a
cnt += N - pos
ans += (M-cnt)*rb
print(ans)
|
import sys
read = sys.stdin.read
#readlines = sys.stdin.readlines
def main():
n, *a = map(int, read().split())
r = 0
maxtall = a[0]
for i1 in range(1, n):
if a[i1] > maxtall:
maxtall = a[i1]
else:
r += maxtall - a[i1]
print(r)
if __name__ == '__main__':
main()
| 0 | null | 56,187,657,635,922 | 252 | 88 |
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 = list(map(int, input().split()))
l = []
for i in range(n):
l.append([a[i], i])
l.sort(reverse=True)
dp = [[0]*(n+1) for _ in range(n)]
for i in range(n):
if i == 0:
dp[i][0] = l[i][0] * (n-1-l[i][1])
dp[i][1] = l[i][0] * l[i][1]
else:
for j in range(n, -1, -1):
if dp[i-1][j] > 0:
dp[i][j+1] = max(dp[i][j+1], dp[i-1][j] + l[i][0] * abs(j-l[i][1]))
dp[i][j] = max(dp[i][j], dp[i-1][j] + l[i][0] * abs(n-i+j-1-l[i][1]))
print(max(dp[-1]))
if __name__ == '__main__':
main()
|
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
infants = []
for i in range(n):
infants.append((a[i],i))
infants.sort()
dp = [defaultdict(int) for _ in range(n+1)]
dp[0][0] = 0
for i in range(n):
score,initial = infants.pop()
for right in dp[i].keys():
dp[i+1][right] = max(dp[i][right]+abs(initial-(i-right))*score,dp[i+1][right])
dp[i+1][right+1] = max(dp[i][right]+abs((n-1-right)-initial)*score,dp[i+1][right+1])
print(max(dp[n].values()))
| 1 | 33,785,048,009,188 | null | 171 | 171 |
n = int(input())
s, t = input().split()
a = []
for i in range(n):
a.append(s[i])
a.append(t[i])
print(''.join(a))
|
N=int(input());S,T=map(str,input().split())
for i in range(N):print(S[i],end='');print(T[i],end='')
| 1 | 112,099,944,316,460 | null | 255 | 255 |
# ALDS_2_B.
# バブルソート。
from math import sqrt, floor
def intinput():
a = input().split()
for i in range(len(a)):
a[i] = int(a[i])
return a
def show(a):
# 配列の中身を出力する。
_str = ""
for i in range(len(a) - 1):
_str += str(a[i]) + " "
_str += str(a[len(a) - 1])
print(_str)
def selection_sort(a):
# 選択ソート。その番号以降の最小値を順繰りにもってくる。
# 交換回数は少ないが比較回数の関係で結局O(n^2)かかる。
count = 0
for i in range(len(a)):
# a[i]からa[len(a) - 1]のうちa[j]が最小っていうjを取る。
# このときiとjが違うならカウントする。
# というかa[i]とa[j]が違う時カウントでしたね。
minj = i
for j in range(i, len(a)):
if a[j] < a[minj]: minj = j
if a[i] > a[minj]: count += 1; a[i], a[minj] = a[minj], a[i]
return count
def main():
N = int(input())
A = intinput()
count = selection_sort(A)
show(A); print(count)
if __name__ == "__main__":
main()
|
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
A,B,M = mi()
a = li()
b = li()
xyc = [li() for i in range(M)]
ans = min(a)+min(b)
for i in range(M):
ans = min(ans,a[xyc[i][0]-1]+b[xyc[i][1]-1] - xyc[i][2])
print(ans)
| 0 | null | 26,953,057,854,956 | 15 | 200 |
s = str(input())
sm = 0
for c in s:
sm += int(c)
if sm % 9 == 0:
print('Yes')
else:
print('No')
|
N=list(input())
n=[int(s) for s in N]
if n[-1]==2 or n[-1]== 4 or n[-1]== 5 or n[-1]== 7 or n[-1]== 9:
print('hon')
elif n[-1]==0 or n[-1]==1 or n[-1]== 6 or n[-1]== 8:
print('pon')
else :
print('bon')
| 0 | null | 11,757,912,071,840 | 87 | 142 |
# 2020/08/16
# AtCoder Beginner Contest 030 - A
# Input
h = int(input())
w = int(input())
n = int(input())
# Calc
ans = n // max(h, w)
if n % max(h, w) > 0:
ans = ans + 1
# Output
print(ans)
|
N,M = (int(x) for x in input().split())
AC = [False]*N
WA_count = [0]*N
AC_count = 0
for i in range(M):
p,S = (y for y in input().split())
if AC[int(p)-1] == False:
if S == 'AC':
AC[int(p)-1] = True
AC_count += 1
else:
WA_count[int(p)-1] += 1
for i in range(N):
if AC[i] == False:
WA_count[i] = 0
print(AC_count,sum(WA_count))
| 0 | null | 91,090,598,795,348 | 236 | 240 |
s=input()
n=len(s)//2
s1=s[:n]
s2=s[-n:]
s2=s2[::-1]
cnt=0
for i in range(len(s1)):
if s1[i]!=s2[i]:
cnt+=1
print(cnt)
|
S=input()
cnt=0
for i in range(len(S)//2):
cnt+=S[i]!=S[-i-1]
print(cnt)
| 1 | 119,805,373,244,790 | null | 261 | 261 |
A,B=map(int,input().split())
if (A>9)|(B>9):
print(-1)
else:
print(A*B)
|
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A, reverse = True)
for i in range(min(K, N)):
A[i] = 0
ans = sum(A)
print(ans)
| 0 | null | 118,165,074,579,970 | 286 | 227 |
from scipy.sparse.csgraph import floyd_warshall
n, m, l = map(int, input().split())
inf = 10**12
d = [[inf for i in range(n)] for i in range(n)]
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(m):
x,y,z = map(int,input().split())
d[x-1][y-1] = z
d[y-1][x-1] = z
for i in range(n):
d[i][i] = 0 #自身のところに行くコストは0
dd = floyd_warshall(d)
ddd = [[inf for i in range(n)] for i in range(n)]
for i in range(n):
for j in range(n):
if dd[i][j] <= l:
ddd[i][j] = 1
dddd =floyd_warshall(ddd)
q = int(input())
for i in range(q):
s, t = map(int, input().split())
if dddd[s-1][t-1] == inf:
print(-1)
else:
print(int(dddd[s-1][t-1]-1))
|
N,M,L=map(int, input().split())
def warshall_floyd(d):
#d[i][j]: iからjへの最短距離
for k in range(N):
for i in range(N):
for j in range(N):
d[i][j]=min(d[i][j],d[i][k] + d[k][j])
return d
d=[[float("inf")]*N for i in range(N)]
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(M):
x,y,z=map(int,input().split())
d[x-1][y-1]=z
d[y-1][x-1]=z
for i in range(N):
d[i][i]=0 #自身のところに行くコストは0
D=warshall_floyd(d)
E=[[float("inf")]*N for i in range(N)]
for i in range(N):
for j in range(N):
if D[i][j]==0:
E[i][j]=0
elif D[i][j]<=L:
E[i][j]=1
F=warshall_floyd(E)
N=int(input())
for i in range(N):
x,y=map(int,input().split())
if F[x-1][y-1]==float("inf"):
print(-1)
else:
print(F[x-1][y-1]-1)
| 1 | 173,624,078,440,680 | null | 295 | 295 |
num = 0
def marge_sort(array):
global num
if len(array) < 2: return array
mid = len(array) // 2
left = marge_sort(array[:mid])
right = marge_sort(array[mid:])
len_l, len_r = len(left), len(right)
left += [float("inf")]
right += [float("inf")]
marray = [0] * (len_l + len_r)
l, r = 0, 0
for i in range(len_l + len_r):
num += 1
if left[l] <= right[r]:
marray[i] = left[l]
l += 1
else:
marray[i] = right[r]
r += 1
return marray
n = int(input())
a = list(map(int, input().split()))
print(" ".join(map(str, marge_sort(a))))
print(num)
|
def merge(A, left, mid, right):
L = A[left:mid] + [2147483648]
R = A[mid:right] + [2147483648]
i = 0
j = 0
for k in range(left, right):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
global c
c += right - left
def mergeSort(A, left, right):
if left + 1 < right:
mid = (left + right) // 2
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
c = 0
n = int(input())
A = list(map(int, input().split()))
mergeSort(A, 0, n)
print(" ".join(map(str, A)))
print(c)
| 1 | 112,548,200,512 | null | 26 | 26 |
N=int(input())
count=0
for A in range(1,N):
count+=(N-1)//A
print(count)
|
N = int(input())
r = 0
for A in range(1, N+1):
for B in range(1, N+1):
if A * B >= N: break
r += 1
print(r)
| 1 | 2,593,025,916,272 | null | 73 | 73 |
l = int(input())
print(l**3 / 27)
|
n = int(input())
print((n+1)//2)
| 0 | null | 52,767,973,132,700 | 191 | 206 |
n,k=map(int,input().split())
p=list(map(int,input().split()))
sw=0
sum=0
for i in range(n):
for j in range(n-1):
if p[j]>=p[j+1]:
sw=p[j]
p[j]=p[j+1]
p[j+1]=sw
for i in range(k):
sum+=p[i]
print(sum)
|
n=int(input())
l=list(map(int,input().split()))
a=[0]*(10**6+1)
for i in range(n):
if(a[l[i]]==0):
for j in range(2*l[i],10**6+1,l[i]):
a[j]=2
a[l[i]]+=1
print(a.count(1))
| 0 | null | 13,141,513,776,742 | 120 | 129 |
n = input()
if n != 0:
l = [int(x) for x in raw_input().split()]
print min(l), max(l), sum(l)
else:
print "0 0 0"
|
n = int(input())
nums = list(map(int,input().split()))
nums.sort()
ans = 0
cnt = {}
flag = [0]*1000005
for i in nums:
cnt[i] = cnt.get(i,0) + 1
for i in nums:
if flag[i] == 0 and cnt[i] == 1:
ans += 1
if flag[i] == 1:
continue
for j in range(i,1000001,i):
flag[j] = 1
print(ans)
| 0 | null | 7,529,679,082,240 | 48 | 129 |
import sys
input = sys.stdin.readline
N = int(input())
S = input().rstrip()
Q = int(input())
qs = [input().split() for i in range(Q)]
class BinaryIndexedTree:
def __init__(self,size):
self.N = size
self.bit = [0]*(size+1)
def add(self,x,w): # 0-indexed
x += 1
while x <= self.N:
self.bit[x] += w
x += (x & -x)
def _sum(self,x): # 1-indexed
ret = 0
while x > 0:
ret += self.bit[x]
x -= (x & -x)
return ret
def sum(self,l,r): # [l,r)
return self._sum(r) - self._sum(l)
def __str__(self): # for debug
arr = [self.sum(i,i+1) for i in range(self.N)]
return str(arr)
bits = [BinaryIndexedTree(N+1) for i in range(26)]
for i,c in enumerate(S):
ci = ord(c) - ord('a')
bits[ci].add(i,1)
now = list(S)
for a,b,c in qs:
if a=='1':
b = int(b)
pi = ord(now[b-1]) - ord('a')
bits[pi].add(b-1, -1)
ci = ord(c) - ord('a')
bits[ci].add(b-1, 1)
now[b-1] = c
else:
b,c = int(b),int(c)
cnt = 0
for i in range(26):
cnt += int(bits[i].sum(b-1,c) > 0)
print(cnt)
|
import sys
input = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**9)
#from functools import lru_cache
def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')
#####segfunc#####
def segfunc(x, y):
return x*y
#################
#####ide_ele#####
ide_ele = 1
#################
class SegTree:
"""
init(init_val, ide_ele): 配列init_valで初期化 O(N)
update(k, x): k番目の値をxに更新 O(logN)
query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
"""
def __init__(self, init_val, segfunc, ide_ele):
"""
init_val: 配列の初期値
segfunc: 区間にしたい操作
ide_ele: 単位元
n: 要素数
num: n以上の最小の2のべき乗
tree: セグメント木(1-index)
"""
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = ide_ele
self.num = 1 << (n - 1).bit_length()
self.tree = [ide_ele] * 2 * self.num
# 配列の値を葉にセット
for i in range(n):
self.tree[self.num + i] = init_val[i]
# 構築していく
for i in range(self.num - 1, 0, -1):
self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
def update(self, k, x):
"""
k番目の値をxに更新
k: index(0-index)
x: update value
"""
k += self.num
self.tree[k] = x
while k > 1:
self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
k >>= 1
def query(self, l, r):
"""
[l, r)のsegfuncしたものを得る
l: index(0-index)
r: index(0-index)
"""
res = self.ide_ele
l += self.num
r += self.num
while l < r:
if l & 1:
res = self.segfunc(res, self.tree[l])
l += 1
if r & 1:
res = self.segfunc(res, self.tree[r - 1])
l >>= 1
r >>= 1
return res
def main():
n=II()
s=input().rstrip().decode()
S=[]
for i in s:
S.append(ord(i)-ord("a"))
seg=[SegTree([1]*n,segfunc,ide_ele) for _ in range(26)]
#print(S)
#print(seg)
for i in range(n):
seg[S[i]].update(i,0)
q=II()
for _ in range(q):
a,b,c=input().split()
if int(a)==1:
b=int(b)-1
seg[S[b]].update(b,1)
t=ord(c)-ord("a")
seg[t].update(b,0)
S[b]=t
else:
#print(S)
b=int(b)-1
c=int(c)
cnt=0
for se in seg:
if se.query(b,c)==0:
cnt+=1
print(cnt)
if __name__=="__main__":
main()
| 1 | 62,506,130,137,412 | null | 210 | 210 |
n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
a[i],b[i] = map(int,input().split())
a.sort()
b.sort()
if n % 2 == 1:
print(b[n//2] - a[n//2] + 1)
else:
front = n//2
back = (n-1)//2
print((b[front] + b[back] - (a[front] + a[back]) + 1))
|
N = int(input())
A = [0]*N
B = [0]*N
for n in range(N):
A[n],B[n] = map(int,input().split())
A.sort()
B.sort()
if N % 2 == 1:
print(B[N//2]-A[N//2]+1)
else:
Am = (A[N//2]+A[N//2-1])/2
Bm = (B[N//2]+B[N//2-1])/2
print(int((Bm-Am)*2)+1)
| 1 | 17,258,208,300,630 | null | 137 | 137 |
import sys
from io import StringIO
import unittest
import os
# union find木
# 参考:https://note.nkmk.me/python-union-find/
class UnionFind:
def __init__(self, n):
"""
コンストラクタ
:要素数 n:
"""
self.n = n
# 添字x: 値yとすると・・
# root以外の場合: 要素xは集合yに所属する。
# rootの場合 : 集合xの要素数はy個である。(負数で保持する)
self.parents = [-1] * n
def getroot(self, x):
"""
所属する集合(ルートの番号)を取得する。
:調査する要素 x:
"""
# 値が負数 = ルートに到達したので、ルート木の番号を返す。
if self.parents[x] < 0:
return x
else:
# 値が正数 = ルートに到達していない場合、さらに親の情報を確認する。
# 下の行は経路圧縮の処理。
self.parents[x] = self.getroot(self.parents[x])
return self.parents[x]
def union(self, x, y):
"""
2つの要素が所属する集合をを同じ集合に結合する。
:結合する要素(1つ目) x:
:結合する要素(2つ目) y:
"""
# 既に同じ集合に存在するなら何もせず終了。
x = self.getroot(x)
y = self.getroot(y)
if x == y:
return
# 集合を結合する。
# ※ここ、「要素数の大きい集合に、少ない集合を結合」しているが、こうすることで以後の「処理量を削減」するテクニックである。
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
"""
指定した集合に属する要素数を取得する。
:調査する集合 x:
"""
# 添え字[root]には、要素数が負数で格納されている。そのため、取得する際は-反転する。
return -self.parents[self.getroot(x)]
def is_same(self, x, y):
return self.getroot(x) == self.getroot(y)
def members(self, x):
root = self.getroot(x)
return [i for i in range(self.n) if self.getroot(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 resolve():
# 数値取得サンプル
# 1行N項目 x, y = map(int, input().split())
# N行N項目 x = [list(map(int, input().split())) for i in range(n)]
n, m, k = map(int, input().split())
friends = [list(map(int, input().split())) for i in range(m)]
blocks = [list(map(int, input().split())) for i in range(k)]
# uf 作成(要素数を+1しているのは添え字と人番号を合わせるため)
uf = UnionFind(n + 1)
# 除外リスト作成(これは、本問題特有の処理)
# 添字x: 値yとすると・・ 要素xの友達候補を数えるとき、y(list)に該当する人は除外する。(要素数を+1しているのは添え字と人番号を合わせるため)
omits = [[] for i in range(n + 1)]
# UnionFindにて集合を明確にする。
for friend in friends:
uf.union(friend[0], friend[1])
# 除外リストを更新(これは、本問題特有の処理)
omits[friend[0]].append(friend[1])
omits[friend[1]].append(friend[0])
# ブロックリストの情報を除外リストに加える(これは、本問題特有の処理)
for block in blocks:
# 同じ集合に属するウ場合のみ、リストに追加
if uf.is_same(block[0], block[1]):
omits[block[0]].append(block[1])
omits[block[1]].append(block[0])
# 友達候補数を出力して終了(人は1始まりなので1からループを行う
# ans = []
# for i in range(1, n + 1):
# 友達候補 = 集合数 - 除外リスト(自分の友人数 + 自分がブロックしている人数) - 1(集合に含まれる自分自身を除く)
# ans.append(uf.size(i) - len(omits[i]) - 1)
# print(" ".join(ans))
# ans = [str(uf.size(i) - len(omits[i]) - 1) for i in range(1, n + 1)]
ans = [uf.size(i) - len(omits[i]) - 1 for i in range(1, n + 1)]
print(*ans)
# テストクラス
class TestClass(unittest.TestCase):
def assertIO(self, assert_input, output):
stdout, sat_in = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, sat_in
self.assertEqual(out, output)
def test_input_1(self):
test_input = """4 4 1
2 1
1 3
3 2
3 4
4 1"""
output = """0 1 0 1"""
self.assertIO(test_input, output)
def test_input_2(self):
test_input = """5 10 0
1 2
1 3
1 4
1 5
3 2
2 4
2 5
4 3
5 3
4 5"""
output = """0 0 0 0 0"""
self.assertIO(test_input, output)
def test_input_3(self):
test_input = """10 9 3
10 1
6 7
8 2
2 5
8 4
7 3
10 9
6 4
5 8
2 6
7 5
3 1"""
output = """1 3 5 4 3 3 3 3 1 0"""
self.assertIO(test_input, output)
# 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
def tes_t_1original_1(self):
test_input = """データ"""
output = """データ"""
self.assertIO(test_input, output)
# 実装orテストの呼び出し
if __name__ == "__main__":
if os.environ.get("USERNAME") is None:
# AtCoder提出時の場合
resolve()
else:
# 自PCの場合
unittest.main()
|
n=int(input())
C = list(input())
ans = 0
N = n-1
for l in range(n):
if C[l] == "W":
for r in range(N,0,-1):
if r<=l:
print(ans)
exit()
if C[r] == "R":
#print(C)
C[l] = "R"
C[r] = "W"
#print(C)
#print("-----------------")
ans += 1
N = r-1
break
print(ans)
| 0 | null | 34,156,188,127,952 | 209 | 98 |
def abc178c():
n = int(input())
print((pow(10, n) - pow(9, n) - pow(9, n) + pow(8, n)) % (pow(10, 9) + 7))
abc178c()
|
def solve(A, i, m, n):
"""Return if it is possible to make value m with some elements in A.
n is length of A.
i is index.
R is the record of answer i, m.
Using Divide-and-Conquer method.
"""
if R[i][m] != None:
return R[i][m]
if m == 0:
R[i][m] = True
return True
elif i >= n:
R[i][m] = False
return False
else:
ans = solve(A, i + 1, m, n) or solve(A, i + 1, m - A[i], n)
R[i][m] = ans
return ans
import sys
n = int(sys.stdin.readline())
A = tuple(map(int, sys.stdin.readline().split()))
q = int(sys.stdin.readline())
M = tuple(map(int, sys.stdin.readline().split()))
s_A = sum(A)
R = [[None] * 2000 for i in range(n + 1)]
ans = ''
for m in M:
if s_A < m:
ans += 'no\n'
elif solve(A, 0, m, n):
ans += 'yes\n'
else:
ans += 'no\n'
print(ans, end = '')
| 0 | null | 1,651,034,321,270 | 78 | 25 |
K = int(input())
num = 0
for i in range(0,K+1):
num = (num*10+7)%K
if num==0:
print(i+1,'\n')
break
if num:
print("-1\n")
|
k = int(input())
a = [7%k]
for i in range(1, k):
a.append((a[i-1]*10+7)%k)
for i in range(k):
if a[i] == 0:
print(i+1)
exit()
print(-1)
| 1 | 6,136,586,755,310 | null | 97 | 97 |
r = input()
r = int(r)
result = r**2
print(result)
|
import sys
a = int(sys.stdin.readline().rstrip())
print(a**2)
| 1 | 145,191,481,083,424 | null | 278 | 278 |
import queue
n,u,v = map(int, input().split())
u -= 1
v -= 1
path = [[] for i in range(n)]
for i in range(n-1):
a,b = map(int, input().split())
a -= 1
b -= 1
path[a].append(b)
path[b].append(a)
t = [10**9]*n
t[u]=0
q = queue.Queue()
q.put(u)
while not q.empty():
p = q.get()
for x in path[p]:
if t[x]>t[p]+1:
q.put(x)
t[x]=t[p]+1
a = [10**9]*n
a[v]=0
q = queue.Queue()
q.put(v)
while not q.empty():
p = q.get()
for x in path[p]:
if a[x]>a[p]+1:
q.put(x)
a[x]=a[p]+1
c = [a[i] for i in range(n) if a[i]>t[i]]
print(max(c)-1)
|
import sys
# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
# import heapq
import decimal
# import statistics
import queue
# import numpy as np
sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353
ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
# ===CODE===
def main():
n, u, v = ns()
edge = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = ns()
a -= 1
b -= 1
edge[a].append(b)
edge[b].append(a)
dist_takahashi = [-1] * n
dist_aoki = [-1] * n
def bfs(pos, dist):
que = queue.Queue()
que.put(pos)
dist[pos] = 0
while not que.empty():
current = que.get()
for e in edge[current]:
if dist[e] == -1:
dist[e] = dist[current] + 1
que.put(e)
return dist
dist_takahashi = bfs(u - 1, dist_takahashi)
dist_aoki = bfs(v - 1, dist_aoki)
ans = 0
for dt, da in zip(dist_takahashi, dist_aoki):
if dt < da:
ans = max(ans, da - 1)
print(ans)
if __name__ == '__main__':
main()
| 1 | 117,446,842,016,572 | null | 259 | 259 |
A = list(map(int,input().split()))
if A[0] < A[1]*2:
print(0)
else:
print(A[0]-A[1]*2)
|
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools as fts
import itertools as its
import math
import sys
INF = float('inf')
def solve(A: int, B: int):
return max(A-2*B, 0)
def main():
sys.setrecursionlimit(10 ** 6)
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
A = int(next(tokens)) # type: int
B = int(next(tokens)) # type: int
print(f'{solve(A, B)}')
if __name__ == '__main__':
main()
| 1 | 166,460,458,534,130 | null | 291 | 291 |
import queue
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
ini = 0
if s[0][0] == '#':
ini = 1
dist = [[ini for _ in range(W)] for _ in range(H)]
start = (0,0)
qq = queue.Queue()
for i in range(H):
for j in range(W):
tgt = []
if j == 0:
pass
else:
if s[i][j-1] == '.' and s[i][j] == '#':
tgt.append(dist[i][j-1] + 1)
else:
tgt.append(dist[i][j-1])
if i == 0:
pass
else:
if s[i-1][j] == '.' and s[i][j] == "#":
tgt.append(dist[i-1][j] + 1)
else:
tgt.append(dist[i-1][j])
if i + j == 0:
continue
dist[i][j] = min(tgt)
print(dist[H-1][W-1])
|
N, M, X = map(int, input().split())
Ca = [list(map(int, input().split())) for _ in range(N)]
pattern = []
for i in range(2**N):
lis = []
for j in range(N):
if ((i>>j)&1):
lis.append(Ca[j])
pattern.append(lis)
cnt = []
for i in range(2**N):
g = [0] * (M+1)
for j in range(len(pattern[i])):
for k in range(M+1):
g[k] += pattern[i][j][k]
if M==1 and g[1]< X:
continue
elif M>1 and min(g[1:M+1]) < X:
continue
else:
cnt.append(g[0])
if len(cnt) ==0:
print(-1)
else:
print(min(cnt))
| 0 | null | 35,549,085,861,070 | 194 | 149 |
n = int(input())
c0 = 0
c1 = 0
c2 = 0
c3 = 0
for i in range(n):
c = input()
if c == 'AC':
c0 += 1
elif c == 'WA':
c1 += 1
elif c == 'TLE':
c2 += 1
else:
c3 += 1
print(f'AC x {c0}')
print(f'WA x {c1}')
print(f'TLE x {c2}')
print(f'RE x {c3}')
|
n = int(input())
ans_count = [0] * 4
ans = ['AC','WA','TLE','RE']
for s in range(n):
tmp = input()
for t in range(4):
if tmp == ans[t]:
ans_count[t] += 1
for s in range(4):
print('%s x %d' % (ans[s],ans_count[s]))
| 1 | 8,720,060,506,932 | null | 109 | 109 |
x, y=map(int, input().split())
s=0
if (x==3): s+=100000
if (x==2): s+=200000
if (x==1): s+=300000
if (y==3): s+=100000
if (y==2): s+=200000
if (y==1): s+=300000
if (s==600000): s+=400000
print(s)
|
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
@njit((i8[:],i8[:],i8[:],i8[:]), cache=True)
def main(lis_h,lis_w,bomb_h,bomb_w):
bomb = {}
for h,w in zip(bomb_h,bomb_w):
bomb[(h,w)] = 1
m_h = max(lis_h)
m_w = max(lis_w)
m_h_lis = []
m_w_lis = []
for i,l in enumerate(lis_h):
if l==m_h:
m_h_lis.append(i)
for i,l in enumerate(lis_w):
if l==m_w:
m_w_lis.append(i)
ans = m_h+m_w
for h in m_h_lis:
for w in m_w_lis:
if (h,w) not in bomb:
return ans
return ans-1
H, W, M = map(int, input().split())
bombh = np.zeros(M, np.int64)
bombw = np.zeros(M, np.int64)
lis_h = np.zeros(H, np.int64)
lis_w = np.zeros(W, np.int64)
for i in range(M):
h,w = map(int, input().split())
h -= 1
w -= 1
bombh[i], bombw[i] = h,w
lis_h[h] += 1
lis_w[w] += 1
print(main(lis_h,lis_w,bombh,bombw))
| 0 | null | 72,515,650,747,388 | 275 | 89 |
s = input().split(" ")
r = int(s[0])
c = int(s[1])
a = [[0 for i in range(c)] for j in range(r+1)]
for i in range(r):
s = input().split(" ")
for j in range(c):
a[i][j] = int(s[j])
for i in range(c):
sk = 0
for j in range(r):
sk += a[j][i]
a[r][i] = sk
for i in range(r+1):
sk = 0
for j in range(c):
print(a[i][j],end=" ")
sk += a[i][j]
print(sk)
|
s=input()
k=int(input())
s0=s[0]
s1=s[-1]
cs=[]
p=s[0]
c=1
for i in range(1,len(s)):
if p==s[i]:
c+=1
else:
cs.append(c)
p=s[i]
c=1
cs.append(c)
if len(cs)==1:
print((cs[0]*k)//2)
exit()
ans=0
if s0!=s1:
for csi in cs:
ans+=csi//2
ans*=k
print(ans)
exit()
else:
for i in range(1,len(cs)-1):
ans+=cs[i]//2
ans*=k
ans+=cs[0]//2+cs[-1]//2
ans+=((cs[0]+cs[-1])//2)*(k-1)
print(ans)
| 0 | null | 88,705,379,203,900 | 59 | 296 |
n,m= map(int,input().split())
A_line =[]
row=[]
for i in range(n):
line = [int(i) for i in input().split()]
A_line.append(line)
for i in range(m):
row += [int(i) for i in input().split()]
for i in range(n):
c=0
for j in range(m):
c += A_line[i][j]*row[j]
print(c)
|
def main():
a,b,c = map(int,input().split())
k = int(input())
i = 0
while a >= b:
b *= 2
i += 1
while b >= c:
c *= 2
i += 1
print("Yes" if i <= k else "No")
if __name__ == '__main__':
main()
| 0 | null | 4,043,391,688,028 | 56 | 101 |
x=input()
2
a=x*x*x
print(a)
|
x = raw_input()
Y = int(x)
Y = Y*Y*Y
print Y
| 1 | 280,099,805,508 | null | 35 | 35 |
import itertools
from collections import deque
from sys import stdin
input = stdin.readline
def main():
H, W = list(map(int, input().split()))
M = [input()[:-1] for _ in range(H)]
def bfs(start):
dist = [[float('inf')]*W for _ in range(H)]
dist[start[0]][start[1]] = 0
is_visited = [[0]*W for _ in range(H)]
is_visited[start[0]][start[1]] = 1
q = deque([start])
max_ = 0
while len(q):
now_h, now_w = q.popleft()
if M[now_h][now_w] == '#':
return
for next_h, next_w in ((now_h+1, now_w),
(now_h-1, now_w),
(now_h, now_w-1),
(now_h, now_w+1)):
if not(0 <= next_h < H) or not(0 <= next_w < W) or \
(is_visited[next_h][next_w] == 1) or \
M[next_h][next_w] == '#':
# (dist[next_h][next_w] != float('inf')) or \
continue
dist[next_h][next_w] = dist[now_h][now_w] + 1
is_visited[next_h][next_w] = 1
max_ = max(max_, dist[next_h][next_w])
q.append((next_h, next_w))
return max_
max_ = 0
for h in range(H):
for w in range(W):
if M[h][w] == '.':
max_ = max(bfs((h, w)), max_)
print(max_)
if(__name__ == '__main__'):
main()
|
INF = float('inf')
H, W = map(int, input().split())
Sss = ['#'*(W+2)] + ['#'+input()+'#' for _ in range(H)] + ['#'*(W+2)]
def convGrid2Graph(Sss):
dxys = [(-1,0), (1,0), (0,-1), (0,1)]
sizeX, sizeY = len(Sss)-2, len(Sss[0])-2
numV = sizeX * sizeY
adjL = [[] for _ in range(numV)]
for x in range(1, sizeX+1):
for y in range(1, sizeY+1):
if Sss[x][y] != '.': continue
z = (x-1)*sizeY + (y-1)
for dx, dy in dxys:
x2, y2 = x+dx, y+dy
if Sss[x2][y2] != '.': continue
z2 = (x2-1)*sizeY + (y2-1)
adjL[z].append(z2)
return adjL
def WarshallFloyd(adjList, INF):
numV = len(adjList)
D = [[INF]*numV for _ in range(numV)]
for u, adj in enumerate(adjList):
for v in adj:
D[u][v] = 1
D[u][u] = 0
for k in range(numV):
Dk = D[k]
for i in range(numV):
Di = D[i]
Dik = Di[k]
for j in range(numV):
D2 = Dik + Dk[j]
if D2 < Di[j]:
D[i][j] = D2
return D
adjL = convGrid2Graph(Sss)
distss = WarshallFloyd(adjL, INF)
ans = 0
for i in range(H*W):
for j in range(H*W):
if distss[i][j] != INF:
ans = max(ans, distss[i][j])
print(ans)
| 1 | 94,295,391,580,800 | null | 241 | 241 |
import bisect
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
ans = 0
for i in range(n-2):
for j in range(i+1,n-1):
x = arr[i]+arr[j]
ind = bisect.bisect_left(arr,x)
ans+=(ind-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,532,851,726,600 | null | 294 | 294 |
import sys
def compoundInterest(x, i, n):
if n == 0:
return x
ans = int(x * (1 + i))
hasu = ans % 1000
if hasu != 0:
ans -= hasu
ans += 1000
return compoundInterest(ans, i, n-1)
if __name__ == "__main__":
n = int(input())
ans = compoundInterest(100000, 0.05, n)
print (ans)
|
import sys
S = sys.stdin.readline().strip()
ls = len(S)
ans = 0
h = ls // 2
for i in range(h):
if S[i] != S[ls - 1 - i]:
ans += 1
print(ans)
| 0 | null | 60,393,815,277,038 | 6 | 261 |
N = int(input())
S = input()
judge = [1]*N
for i in range(1,N):
if S[i-1] == S[i]:
judge[i] = 0
print(sum(judge))
|
n = int(input())
s = input()
count = 1
for h in range(len(s)-1):
if s[h] != s[h+1]:
count += 1
print(count)
| 1 | 170,519,340,289,362 | null | 293 | 293 |
n = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if arr[i] != arr[j] and arr[i] != arr[k] and arr[j] != arr[k]:
if (arr[i] + arr[j]) > arr[k] and (arr[i] + arr[k]) > arr[j] and (arr[j] + arr[k]) > arr[i]:
ans += 1
print(ans)
|
x,k,d = map(int,input().split())
def judge():
l = x if x > 0 else -x
i = l//d
r = l%d
if (k - i)%2 == 0:
print(r)
else:
print(d - r)
'''
if x > 0:
for r in range(d):
if (x - r)/d == (x - r)//d:
i = (x - r)//d
if (k - i)%2 == 0:
print(r)
else:
print(d-r)
exit()
else:
l = -x
for r in range(d):
if (l - r)/d == (l - r)//d:
i = (l - r)//d
if (k - i)%2 == 0:
print(r)
else:
print(d-r)
exit()
'''
if x == 0:
if k%2 == 0:
print(0)
else:
print(d)
elif x < 0:
if k*d + x > 0:
judge()
else:
print(abs(k*d + x))
else:
if x - k*d < 0:
judge()
else:
print(abs(x - k*d))
| 0 | null | 5,155,305,615,908 | 91 | 92 |
n, k, c = map(int, input().split())
s = input()
work = [1]*n
rest = 0
for i in range(n):
if rest:
rest -= 1
work[i] = 0
continue
if s[i] == 'x':
work[i] = 0
elif s[i] == 'o':
rest = c
rest = 0
for i in reversed(range(n)):
if rest:
rest -= 1
work[i] = 0
continue
if s[i] == 'o':
rest = c
if k < sum(work):
print()
else:
for idx, bl in enumerate(work):
if bl:
print(idx+1)
|
n, k, c = map(int, input().split())
s = str(input())
for i in range(n):
if s[i] == 'o':
ant = i+1
antlist = [ant]
break
for i in range(n):
if s[n-i-1] == 'o':
post = n-i
postlist = [post]
break
for i in range(k-1):
ant += c+1
post -= c+1
for i in range(n):
if s[ant+i-1] == 'o':
ant += i
antlist.append(ant)
break
for i in range(n):
if s[post-i-1] == 'o':
post -= i
postlist.append(post)
break
postlist.reverse()
#print(antlist)
#print(postlist)
for i in range(k):
if antlist[i] == postlist[i]:
print(antlist[i])
| 1 | 40,563,401,912,740 | null | 182 | 182 |
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)
|
N = int(input())
ans = N - 1
for i in range(2, int((N ** 0.5) + 1)):
if N % i == 0:
j = N // i
m = i + j - 2
ans = min(ans, m)
print(ans)
| 1 | 161,557,710,349,792 | null | 288 | 288 |
from math import gcd
K = int(input())
ans = 0
for h in range(1, K+1):
for i in range(1, K+1):
for j in range(1, K+1):
ans += gcd(h, gcd(i, j))
print(ans)
|
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
k = int(input())
k2 = np.arange(1, k+1)
k2gcd = np.gcd.outer(k2, np.gcd.outer(k2, k2))
print(k2gcd.sum())
if __name__ == '__main__':
main()
| 1 | 35,616,677,777,120 | null | 174 | 174 |
h,n=map(int,input().split())
a,b=[],[]
for i in range(n):
A,B=map(int,input().split())
a.append(A)#ダメージ量
b.append(B)#消費魔力
dp=[float('inf')]*(h+1)
dp[0]=0
for i in range(h):
for j in range(n):
next=i+a[j] if i+a[j]<=h else h
dp[next]=min(dp[next],dp[i]+b[j])
print(dp[-1])
|
import sys
def main(args):
h, n = map(int,input().split())
magic = [0]*n
maxim = 0
for i in range(n):
dam, mp = map(int,input().split())
magic[i] = (dam, mp)
maxim = max(maxim, dam)
damage = [float('inf')]*(2*10**4)
damage[0] = 0
for i in range(max(2*h, 2*maxim)):
for m in magic:
dam, mp = m
damage[i] = min(damage[i-dam]+mp, damage[i])
print(min(damage[h:]))
if __name__ == '__main__':
main(sys.argv[1:])
| 1 | 81,185,136,063,880 | null | 229 | 229 |
n, x, t = input().split()
z = ((int(n)+int(x))-1)//int(x)
ans = z * int(t)
print(ans)
|
N, S = map(int, input().split())
As = list(map(int, input().split()))
P = 998244353
memo = [[0 for _ in range(N+1)] for _ in range(S+1)]
for i in range(N+1):
memo[0][i] = (2**i) % P
for i, a in enumerate(As):
for j in range(S+1):
if j - a < 0:
memo[j][i+1] = memo[j][i]*2 % P
else:
memo[j][i+1] = (memo[j][i]*2 + memo[j-a][i]) % P
print(memo[S][N])
| 0 | null | 10,980,604,698,854 | 86 | 138 |
K = int(input())
A,B = map(int,input().split())
li = []
for i in range(A,B+1):
li.append(i)
ans = False
for j in li:
if j%K == 0:
ans = True
break
else:
ans = False
if ans == True:print("OK")
else:print("NG")
|
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
A = LI()
ans_list = [0] * N
for a in A:
ans_list[a - 1] += 1
for i in range(N):
print(ans_list[i])
| 0 | null | 29,543,313,545,570 | 158 | 169 |
def make_divisors(n):
lower_divisors , upper_divisors = [], []
i = 1
while i*i <= n:
if n % i == 0:
lower_divisors.append(i)
if i != n // i:
upper_divisors.append(n//i)
i += 1
return lower_divisors + upper_divisors[::-1]
X = int(input())
S = make_divisors(X)
t = 0
for s in S:
a = s//2
b = a-s
while a**5 - b**5 <= X:
if a**5 - b**5 == X:
print(a,b)
break
a += 1
b += 1
if a**5 - b**5 == X:
break
|
n, k = map(int, input().split())
xs = [i for i in range(1, k + 1)]
xs.reverse()
dict_x = {}
mod = 10 ** 9 + 7
def pow(x, y):
global mod
a = 1
b = x
c = y
while c > 0:
if c & 1:
a = (a * b) % mod
b = (b * b) % mod
c = c >> 1
return a
answer = 0
for x in xs:
num = k // x
a = pow(num, n)
# print(a)
s = 2
while x * s <= k:
a -= dict_x[x * s]
s += 1
dict_x[x] = a
answer = (answer + a * x) % mod
print(answer)
| 0 | null | 31,126,606,713,370 | 156 | 176 |
from collections import deque
N, X, Y = map(int, input().split())
ans = [0] * N
for i in range(N-1):
dist_list = [-1] * N
d = deque([[i]])
dist = 0
while d[0]:
tag = d.popleft()
next_tag = []
for t in tag:
if dist_list[t] == -1:
dist_list[t] = dist
if t - 1 >= 0:
if dist_list[t - 1] == -1:
next_tag.append(t-1)
if t + 1 < N:
if dist_list[t + 1] == -1:
next_tag.append(t + 1)
if t == X-1:
if dist_list[Y-1] == -1:
next_tag.append(Y-1)
if t == Y-1:
if dist_list[X-1] == -1:
next_tag.append(X-1)
d.append(next_tag)
dist += 1
for j in range(i+1, N):
ans[dist_list[j]] += 1
for i in range(1,N):
print(ans[i])
|
def input_int():
return map(int, input().split())
def one_int():
return int(input())
def one_str():
return input()
def many_int():
return list(map(int, input().split()))
N=one_int()
S=one_str()
bit_count = S.count("1")
S_num = int(S,2)
S_min = S_num % (bit_count-1) if bit_count-1!=0 else 0
S_plu = S_num % (bit_count+1)
# calc_dict = {i:-1 for i in range(10000)}
# res_dict = {i:-1 for i in range(10**4)}
# for i in range(1, 10000):
# if calc_dict[i]==-1:
# calc_dict[i] = bin(i).count("1")
# res_dict[i] = i%calc_dict[i]+1
def mods(temp, count):
count+=1
if temp!=0:
# if temp in res_dict:
# return count+res_dict[temp]
# if temp not in calc_dict:
# calc_dict[temp]= bin(temp).count("1")
count = mods(temp%bin(temp).count("1") , count)
return count
for i in range(N):
if S[i]=="0":
part = pow(2, (N-i-1), bit_count+1)
num = (S_plu + part ) % (bit_count+1)
print(mods(num, 0))
else:
if S_num == 2**(N-i-1):
print(0)
continue
part = pow(2, (N-i-1), bit_count-1)
num = (S_min - part)%(bit_count-1)
print(mods(num, 0))
| 0 | null | 26,085,535,702,720 | 187 | 107 |
while True:
[a,b,c]=[x for x in input().split()]
[a,c]=[int(a),int(c)]
op=b
if op=="?":
break
elif op=="+":
print(a+c)
elif op=="-":
print(a-c)
elif op=="*":
print(a*c)
else:
print(a//c)
|
while True:
a = input()
if "?" in a:
break
print(eval(a.replace("/", "//")))
| 1 | 688,015,795,122 | null | 47 | 47 |
from time import time
from random import random
limit_secs = 2
start_time = time()
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
def calc_score(t):
score = 0
S = 0
last = [-1] * 26
for d in range(len(t)):
S += s[d][t[d]]
last[t[d]] = d
for i in range(26):
S -= c[i] * (d - last[i])
score += max(10 ** 6 + S, 0)
return score
def solution1():
return [i % 26 for i in range(D)]
def solution2():
t = None
score = -1
for i in range(26):
nt = [(i + j) % 26 for j in range(D)]
if calc_score(nt) > score:
t = nt
return t
def solution3():
t = []
for _ in range(D):
score = -1
best = -1
t.append(0)
for i in range(26):
t[-1] = i
new_score = calc_score(t)
if new_score > score:
best = i
score = new_score
t[-1] = best
return t
def optimize0(t):
return t
def optimize1(t):
score = calc_score(t)
while time() - start_time + 0.15 < limit_secs:
d = int(random() * D)
old = t[d]
t[d] = int(random() * 26)
new_score = calc_score(t)
if new_score < score:
t[d] = old
else:
score = new_score
return t
def optimize2(t):
score = calc_score(t)
while time() - start_time + 0.15 < limit_secs:
d1 = int(random() * D)
q1 = int(random() * 26)
d2 = int(random() * D)
q2 = int(random() * 26)
old1 = t[d1]
old2 = t[d2]
t[d1] = q1
t[d2] = q2
new_score = calc_score(t)
if new_score < score:
t[d2] = old2
t[d1] = old1
else:
score = new_score
return t
t = solution3()
t = optimize2(t)
print('\n'.join(str(e + 1) for e in t))
|
import random
import time
import copy
start = time.time()
D = int(input())
s = [[0 for j in range(26)] for i in range(D)]
c = list(map(int, input().split(" ")))
result = []
total = -10000000
for i in range(D):
a = list(map(int, input().split(" ")))
for j, k in enumerate(a):
s[i][j] = int(k)
while(1):
result_tmp = []
total_tmp=0
last = [0 for i in range(26)]
for i in range(D):
score = [0 for i in range(26)]
for j in range(26):
score[j] = s[i][j]
for k in range(26):
if j != k:
score[j] -= (i + 1 - last[k]) * c[k]
score_sort = sorted(score, reverse=True)
score_max = score_sort[0]
tmp = []
for j in range(26):
if score_max >= 0 and score_max*0.95 <= score[j]:
tmp.append(j)
if score_max < 0 and score_max*1.05 <= score[j]:
tmp.append(j)
score_max = random.choice(tmp)
result_tmp.append(score_max)
last[score_max] = i + 1
total_tmp += score[score_max]
if total < total_tmp:
total=total_tmp
result = result_tmp.copy()
end = time.time()
if end - start > 1.8:
break
for i in range(D):
print(result[i]+1)
| 1 | 9,613,418,817,666 | null | 113 | 113 |
md = list(map(int,input().split()))
mmd = list(map(int,input().split()))
if md[0] != mmd[0]:
print(1)
else:
print(0)
|
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7
def main():
M1, D1 = map(int, readline().split())
M2, D2 = map(int, readline().split())
if M1 != M2:
print('1')
else:
print('0')
if __name__ == '__main__':
main()
| 1 | 124,231,615,592,660 | null | 264 | 264 |
dp = [-1] * 2005
mod = 1000000007
def DP(S):
if S == 0:
return 1
if(dp[S] != -1):
return dp[S]
ret = 0
for i in range(3, S+1):
ret = (ret + DP(S-i)) % mod
dp[S] = ret
return ret
n = int(input())
print(DP(n))
|
import math
s = int(input())
maxn = s//3
ans=0
for i in range(1,maxn+1):
b = s-3*i
ans += math.factorial(b+i-1)//(math.factorial(b)*math.factorial(i-1))
print(ans%(10**9+7))
| 1 | 3,256,084,364,392 | null | 79 | 79 |
N, K = map(int, input().split())
a = list(map(int, input().split()))
for i in range(N-K):
if a[i] < a[i + K]:
print('Yes')
else:
print("No")
|
def main():
INF = 10**9
n,k = map(int,input().split())
P = [0] + list(map(int,input().split()))
C = [0] + list(map(int,input().split()))
scores = [0]*n
for s in range(1,n+1):
start = s
dp = [-INF]*(n+1)
dp[0] = 0
for i in range(n):
p = P[s]
dp[i+1] = dp[i] + C[p]
if start == p:
break
s = p
if k <= i+1:
scores[start-1] = max(dp[1:k+1])
elif dp[i+1]<=0:
scores[start-1] = max(dp[1:])
else:
r = k%(i+1)
q = k//(i+1)
if r>0:
scores[start-1] = max(max(dp[1:r+1])+dp[i+1]*q, max(dp[1:]))
else:
scores[start-1] = dp[i+1]*(q-1)+max(dp[1:])
print(max(scores))
main()
| 0 | null | 6,335,648,331,002 | 102 | 93 |
data = [int(x) for x in input().split(" ")]
for i in range(len(data)):
if data[i] == 0: print(i+1)
|
n,k = map(int,input().split())
li = list(map(int,input().split()))
licsum = [0]
for i in range(n):
licsum.append(licsum[i] + li[i])
lia = []
for i in range(n-k+1):
a = licsum[k+i] - licsum[i] + k
lia.append(float(a)/2)
print(max(lia))
| 0 | null | 44,305,195,306,880 | 126 | 223 |
s, k = input(), int(input())
if len(set(list(s))) == 1:
print(len(s) * k // 2)
exit()
ans, cnt = [], 1
for i in range(1, len(s)):
if s[i] == s[i - 1]:
cnt += 1
else:
ans.append(cnt)
cnt = 1
else:
ans.append(cnt)
if s[0] != s[-1]:
ans = [i//2 for i in ans]
print(sum(ans) * k)
elif s[0] == s[-1]:
f, m, l = ans[0] // 2, (ans[0] + ans[-1]) // 2, ans[-1] // 2
ans = [i//2 for i in ans[1:-1]]
print(f + sum(ans) * k + m * (k - 1) + l)
|
s=input()
n=int(input())
if len(s)==1:print(n//2);exit()
if len(set(s))==1:print(len(s)*n//2);exit()
c=1
l=[]
for x in range(1,len(s)):
if s[x-1]==s[x]:
c+=1
if x==len(s)-1:l.append(c)
else:l.append(c);c=1
t=0
if s[0]==s[-1] and l[0]%2==1 and l[-1]%2==1:t=n-1
print(sum([i//2 for i in l])*n+t)
| 1 | 174,903,354,894,212 | null | 296 | 296 |
import bisect
N = int(input())
L = [int(n) for n in input().split()]
L = sorted(L)
total = 0
for i in range(N - 2):
a = L[i]
for j in range(i + 1, N - 1):
b = L[j]
right_endpoint = bisect.bisect_left(L, a+b, j)
total += right_endpoint - j - 1
print(total)
|
import bisect
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
ans = 0
for i in range(n):
for j in range(i+1,n):
a,b = arr[i],arr[j]
cmin = abs(a-b)
cmax = a+b
l = bisect.bisect_left(arr,cmin+1,lo=j+1)
r = bisect.bisect_right(arr,cmax-1,lo=j+1)
# print(cmin,l,cmax,r)
if r-l > 0:
ans += r-l
print(ans)
| 1 | 171,351,549,479,938 | null | 294 | 294 |
class My_Queue:
def __init__(self, S):
self.S = S
self.q = [0 for i in range(S)]
self.head = 0
self.tail = 0
def enqueue(self, x):
if self.isFull():
print('overflow')
raise
else:
self.q[self.tail] = x
if self.tail + 1 == self.S:
self.tail = 0
else:
self.tail += 1
def dequeue(self):
if self.isEmpty():
print('underflow')
raise
else:
x = self.q[self.head]
self.q[self.head] = 0
if self.head + 1 == self.S:
self.head = 0
else:
self.head += 1
return x
def isEmpty(self):
return self.head == self.tail
def isFull(self):
return self.head == (self.tail + 1) % self.S
def main():
n, qms = map(int, input().split())
elapsed_time = 0
q = My_Queue(n + 1)
for i in range(n):
name, time = input().split()
time = int(time)
q.enqueue([name, time])
while(q.head != q.tail):
a = q.dequeue()
if a[1] <= qms:
elapsed_time += a[1]
print(a[0], elapsed_time)
else:
a[1] -= qms
elapsed_time += qms
q.enqueue(a)
if __name__ == "__main__":
main()
|
print((lambda x:'Yes' if x[0] == x[1] else 'No')(input().split()))
| 0 | null | 41,688,672,414,440 | 19 | 231 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.