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 numpy as np
n,t=map(int,input().split())
a=[list(map(int,input().split()))for i in range(n)]
b=np.zeros((n+1,t))
c=np.zeros((n+1,t))
for i in range(n):
b[i+1]=b[i]
b[i+1][a[i][0]:]=np.maximum(b[i+1][a[i][0]:],b[i][:-a[i][0]]+a[i][1])
c[-i-2]=c[-i-1]
c[-i-2][a[-i-1][0]:]=np.maximum(c[-i-2][a[-i-1][0]:],c[-i-1][:-a[-i-1][0]]+a[-i-1][1])
q=0
for i in range(n):
q=max(q,max(b[i]+c[i+1][::-1])+a[i][1])
print(int(q))
|
import sys
input = sys.stdin.readline
read = sys.stdin.read
n, t = map(int, input().split())
m = map(int, read().split())
AB = sorted(zip(m, m))
A, B = zip(*AB)
dp = [[0]*t for _ in range(n+1)]
for i, a in enumerate(A[:-1]):
for j in range(t):
if j < a:
dp[i+1][j] = dp[i][j]
else:
dp[i+1][j] = max(dp[i][j-a]+B[i], dp[i][j])
ans = 0
maxB = [B[-1]]*n
for i in range(n-2, 0, -1):
maxB[i] = max(B[i], maxB[i+1])
for i in range(n-1):
ans = max(ans, dp[i+1][-1] + maxB[i+1])
print(ans)
| 1 | 151,884,462,271,360 | null | 282 | 282 |
import fractions
n,num=map(int,input().split())
num_set=set(map(lambda x:int(x),input().split()))
two_times_set=set()
for i in num_set:
for j in range(1,30):
i//=2
if i%2!=0:
two_times_set.add(j)
break
if len(two_times_set)!=1:
print(0)
break
else:
num_list=list(num_set)
lcm=num_list[0]
for i in range(1,len(num_list)):
lcm = lcm * num_list[i] // fractions.gcd(lcm, num_list[i])
#print((num-lcm//2)//(lcm)+1)
lcm//=2
print(int(((num/lcm)+1)/2))
|
def gcd(x, y):
if x == 0:
return y
return gcd(y % x, x)
def lcm(x, y):
return x // gcd(x, y) * y
n, m = map(int, input().split())
a = list(map(int, input().split()))
aa = [e // 2 for e in a]
for i, e in enumerate(aa):
if i == 0:
base = 0
while e % 2 == 0:
e //= 2
base += 1
else:
cnt = 0
while e % 2 == 0:
e //= 2
cnt += 1
if cnt != base:
print(0)
exit()
c = 1
for e in aa:
c = lcm(c, e)
ans = (m - c) // (2 * c) + 1
print(ans)
| 1 | 101,396,478,869,790 | null | 247 | 247 |
while True:
try:
m,f,r = map(int,input().split(" "))
if m == -1:
if f == -1:
if r == -1:
break
if m == -1 or f == -1:
print("F")
continue
if m+f >= 80:
print("A")
if m+f >= 65 and m+f < 80:
print("B")
if m+f >= 50 and m+f < 65:
print("C")
if m+f >= 30 and m+f < 50:
if r >= 50:
print("C")
else:
print("D")
if m+f < 30:
print("F")
except EOFError:
break
|
m=f=r=0
while 1:
m,f,r=map(int,input().split())
x=m+f
if m==f==r==-1:break
elif m==-1 or f==-1 or x<30:print('F')
elif x>=80:print('A')
elif 65<=x<80:print('B')
elif 50<=x<65 or r>=50:print('C')
else:print('D')
| 1 | 1,222,547,402,296 | null | 57 | 57 |
N = int(input())
A = list(map(int, input().split()))
nSwap = 0
for i in range(N):
for j in range(N-1, i, -1):
if A[j] < A[j-1]:
A[j-1], A[j] = A[j], A[j-1]
nSwap += 1
print(' '.join(map(str, A)))
print(nSwap)
|
print("No" if input() in ["AAA","BBB"] else "Yes")
| 0 | null | 27,524,089,064,432 | 14 | 201 |
X = int(input())
for a in range(-118,119):
for b in range(-119,118):
if a**5 - b**5 == X:
break
else:
continue
break
print(str(a)+" "+str(b))
|
# -*- coding:utf-8 -*-
import sys
array = []
for i in sys.stdin:
array.append(i)
for i in range(len(array)):
data = array[i].split()
a = int(data[0])
op = str(data[1])
b = int(data[2])
if op == '?':
break
if op == '+':
print(a+b)
if op == '-':
print(a-b)
if op == '/':
print(int(a/b))
if op == '*':
print(a*b)
| 0 | null | 13,072,456,828,900 | 156 | 47 |
gcd=list(map(int,input().split()))
if gcd[0]<gcd[1]:
gcd[0],gcd[1]=gcd[1],gcd[0]
while gcd[1]>0:
r=gcd[0]%gcd[1]
gcd[0]=gcd[1]
gcd[1]=r
print(gcd[0])
|
def GCD_calc(A,B):
if(A>=B):
big = A
small = B
else:
big = B
small = A
r=big%small
if r == 0:
return small
else:
return GCD_calc(small,r)
a,b = map(int,input().split())
print(GCD_calc(a,b))
| 1 | 7,992,101,632 | null | 11 | 11 |
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
INF = 10 ** 9 + 7
D = defaultdict(int)
for a in A:
for i in range(61):
# a の i ビット目がたっているか否か
D[1 << i] += bool(a & (1 << i))
ans = 0
for key, value in D.items():
ans += key * value * (N - value)
ans %= INF
print(ans % INF)
|
import math
def main():
mod = 1000000007
N = int(input())
A = list(map(int, input().split()))
A_max = sorted(A)[-1]
if A_max == 0:
print(0)
exit()
bit_max = int(math.ceil(math.log2(A_max)))
i = 0
bit = 1
res = 0
while i <= bit_max:
c_zero = 0
c_one = 0
for j in range(N):
if A[j] & bit == 0:
c_zero += 1
else:
c_one += 1
m_bit = bit % mod
res += m_bit*c_zero*c_one
res %= mod
i += 1
bit<<=1
print(res)
if __name__ == "__main__":
main()
| 1 | 123,390,866,071,540 | null | 263 | 263 |
n,k = map(int,input().split())
a = list(map(int,input().split()))
f = list(map(int,input().split()))
a.sort()
f.sort(reverse = True)
def ok(p):
result = 0
for i in range(n):
result += max(a[i]-p//f[i],0)
if result <= k:
return True
return False
L = 0
U = a[-1]*f[0]
while U-L >= 1:
M = (U+L)//2
if ok(M):
U = M
else:
L = M+1
print(U)
|
N,K = map(int,input().split())
A = list(map(int,input().split()))
F = list(map(int,input().split()))
A.sort()
F.sort(reverse=True)
ng = -1
ok = 10**12+100
while ok - ng > 1:
mid = (ok + ng) // 2
if sum(max(0,a-mid//f) for a,f in zip(A,F)) <= K:
ok = mid
else:
ng = mid
print(ok)
| 1 | 165,188,953,084,362 | null | 290 | 290 |
def main():
num = int(input())
print(1-((int(num/2)/num)))
main()
|
s = str(input())
num = [int(s[-1])]
for i in range(1, len(s)):
tmp = num[-1] + pow(10, i, 2019) * int(s[-i - 1])
num.append(tmp % 2019)
mod = [1] + [0] * 2018
ans = 0
for i in num:
m = i % 2019
ans += mod[m]
mod[m] += 1
print(ans)
| 0 | null | 104,306,861,861,212 | 297 | 166 |
n,m=map(int,input().split())
a=list(map(int,input().split()))
import math
def lcm(x, y):
return (x * y) // math.gcd(x, y)
x=1
for i in range(n):
x=lcm(x,a[i]//2)
power=[0]*n
for i in range(n):
while a[i]%2==0:
a[i]//=2
power[i]+=1
if power.count(power[0])==n:
print((m+x)//(2*x))
else:
print(0)
|
import fractions
from functools import reduce
def lcm_base(x, y):
return (x * y) // fractions.gcd(x, y)
def lcm(*numbers):
return reduce(lcm_base, numbers, 1)
N,M=map(int,input().split())
A=[int(i)//2 for i in input().split()]
q=lcm(*A)
#print(q)
for i in A:
#print(i)
if q//i%2==0:
print(0)
exit()
print((M//q+1)//2)
| 1 | 101,746,449,461,888 | null | 247 | 247 |
n = int(input())
print(1000 - n%1000 if n%1000 else 0)
|
x = int(input())
print((1000 - x % 1000) % 1000)
| 1 | 8,421,459,099,680 | null | 108 | 108 |
A, B, K = list(map(int, input().split()))
A_res = max(0, A - K)
B_res = max(0, B - K + A - A_res)
print("{} {}".format(A_res, B_res))
|
a,b,n= map(int, input().split())
if a<n:
if (n-a)>b:
print(0,0)
else:
print(0,b-(n-a))
else:
print(a-n,b)
| 1 | 104,446,946,786,124 | null | 249 | 249 |
import sys
from math import gcd
def input():
return sys.stdin.readline().strip()
sys.setrecursionlimit(20000000)
MOD = 10 ** 9 + 7
INF = float("inf")
def main():
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
P = (A1 - B1) * T1
Q = (A2 - B2) * T2
if P > 0:
P = P * (-1)
Q = Q * (-1)
if P + Q < 0:
print(0)
return
elif P + Q == 0:
print("infinity")
return
else:
S = (-P) // (P + Q)
T = (-P) % (P + Q)
if T == 0:
print(2 * S)
else:
print(2 * S + 1)
if __name__ == "__main__":
main()
|
def solve(t1, t2, a1, a2, b1, b2):
if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
return 'infinity'
if a1 * t1 + a2 * t2 > b1 * t1 + b2 * t2:
a1, a2, b1, b2 = b1, b2, a1, a2
at = a1 * t1 + a2 * t2
bt = b1 * t1 + b2 * t2
ans = 0
dg = bt - at
if a1 > b1:
ans += ((a1 - b1) * t1 - 1) // dg
ans += (a1 - b1) * t1 // dg + 1
return ans
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
print(solve(t1, t2, a1, a2, b1, b2))
| 1 | 131,448,866,295,240 | null | 269 | 269 |
# coding: utf-8
a, b = map(int, input().split())
ans = a - b * 2
if ans < 0:
ans = 0
print(ans)
|
A,B = map(int,input().split())
if A <= 2*B:
x = 0
else: x = A - 2*B
print(x)
| 1 | 166,701,508,768,772 | null | 291 | 291 |
N = int(input())
ST = [list(input().split()) for _ in [0]*N]
X = input()
ans = 0
for i,st in enumerate(ST):
s,t = st
if X==s:
break
for s,t in ST[i+1:]:
ans += int(t)
print(ans)
|
import bisect
from typing import List
def main():
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(t(n, m, k, a, b))
def t(n: int, m: int, k: int, a: List[int], b: List[int]) -> int:
aa = [0]
for i in range(n):
aa.append(aa[i] + a[i])
bb = [0]
for i in range(m):
bb.append(bb[i] + b[i])
ret = 0
for i in range(n + 1):
x = aa[i]
s = k - x
if s < 0:
break
j = bisect.bisect(bb, s) - 1
ret = max(ret, i + j)
return ret
if __name__ == '__main__':
main()
| 0 | null | 54,023,352,160,900 | 243 | 117 |
N=int(input())
st=[]
for i in range(N):
s,t=input().split()
t=int(t)
st.append((s,t))
X=input()
#st = sorted(st,key=lambda x:x[1])
flag=0
ans=0
for i in range(N):
if flag: ans += st[i][1]
if st[i][0]==X:flag=1
print(ans)
|
import sys, math
from functools import lru_cache
sys.setrecursionlimit(500000)
MOD = 10**9+7
def input():
return sys.stdin.readline()[:-1]
def mi():
return map(int, input().split())
def ii():
return int(input())
def i2(n):
tmp = [list(mi()) for i in range(n)]
return [list(i) for i in zip(*tmp)]
def main():
N = ii()
s, t = [list(i) for i in zip(*[input().split() for i in range(N)])]
t = list(map(int, t))
k = s.index(input())
print(sum(int(t[i]) for i in range(k+1, N)))
if __name__ == '__main__':
main()
| 1 | 97,002,731,634,470 | null | 243 | 243 |
md1=list(map(int,input().split()))
md2=list(map(int,input().split()))
if md1[0]==md2[0]:
print(0)
else:
print(1)
|
n = int(input())
data = [list(map(int, input().split())) for _ in range(n)]
nums = [[[0 for i in range(10)] for i in range(3)] for i in range(4)]
for d in data:
nums[d[0] - 1][d[1] - 1][d[2] - 1] += d[3]
for i in range(4):
for j in range(3):
for k in range(10):
print(' ' + str(nums[i][j][k]), end='')
print('')
if i != 3:
print('#' * 20)
| 0 | null | 62,951,069,649,678 | 264 | 55 |
a,b,c=map(int,input().split())
d=[i for i in range(a,b+1)]
count=0
for i in d:
if c%i==0:
count+=1
print(count)
|
ins = input().split()
a = int(ins[0])
b = int(ins[1])
c = int(ins[2])
print(len([x for x in list(range(a, b + 1)) if c % x == 0]))
| 1 | 564,055,926,670 | null | 44 | 44 |
n = int(input())
ss = []
tt = []
for _ in range(n):
s,t = input().split()
ss.append(s)
tt.append(int(t))
x = input()
for i in range(n):
if ss[i] == x:
ans = sum(tt[i+1:])
break
print(ans)
|
n = int(input())
cn= 1
an= [1]
i = 1
while cn < n / 4:
an.append(cn*3+1)
cn=cn*3+1
an.sort(reverse=True)
def insertionSort(a,n,g):
cnt=0
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 = j-g
cnt+=1
a[j + g]=v
return cnt
def shellSort(a,n,an):
cnt=0
m = len(an)
g = an
for i in an:
cnt += insertionSort(a,n,i)
return cnt
x=[]
for i in range(n):
x.append(int(input()))
print(len(an))
print(" ".join([str(i) for i in an]))
y= shellSort(x,n,an)
print(y)
for i in range(n):
print(x[i])
| 0 | null | 48,773,362,331,360 | 243 | 17 |
s = input()
if s == "RRR":
print(3)
elif s == "RRS" or s == "SRR":
print(2)
elif s == "RSS" or s == "RSR" or s == "SRS" or s == "SSR":
print(1)
else:
print(0)
|
x = raw_input()
a = int (x)**3
print a
| 0 | null | 2,561,694,053,408 | 90 | 35 |
N = int(input())
memo1 = []
memo2 = []
for i in range(N):
S = input()
sum_n = min_n = 0
for s in S:
if s == ')':
sum_n -= 1
min_n = min(min_n, sum_n)
else:
sum_n += 1
if sum_n >= 0:
memo1.append([min_n, sum_n])
else:
memo2.append([min_n-sum_n, -sum_n])
memo1.sort(reverse=True)
left = 0
for min_n, sum_n in memo1:
if left + min_n < 0:
print('No')
quit()
else:
left += sum_n
memo2.sort(reverse=True)
right = 0
for min_n, sum_n in memo2:
if right + min_n < 0:
print('No')
quit()
else:
right += sum_n
if left == right:
print('Yes')
else:
print('No')
|
A,B = list(input().split())
A = int(A)
B = int((float(B)+0.005)*100)
print(A*B//100)
| 0 | null | 20,158,654,566,532 | 152 | 135 |
import math
x = int(input())
n = 100
y = 0
while x > n:
# 小数点以下切り捨て
n += n // 100
y += 1
print(y)
|
N = int(input())
C = [list(map(int, input().split())) for _ in range(N)]
counter = 0
for d1, d2 in C:
if d1 == d2:
counter += 1
else:
counter = 0
if counter == 3:
print('Yes')
exit()
print('No')
| 0 | null | 14,894,465,381,532 | 159 | 72 |
import collections
N = int(input())
L = [input() for i in range(N)]
c = collections.Counter(L)
max_L = max(list(c.values()))
keys = [k for k, v in c.items() if v == max_L]
keys.sort()
for key in keys:
print(key)
|
from collections import Counter as Ct
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
n=INT()
dic=[]
for i in range(n):
s=STR()
dic.append(s)
ctd=Ct(dic)
ctm=max(ctd.values())
ans=[]
for key,value in ctd.items():
if value==ctm:
ans.append(key)
ans=sorted(ans)
for i in ans:
print(i)
if __name__=='__main__':
do()
| 1 | 70,275,405,851,170 | null | 218 | 218 |
n = int(input())
print(" ".join(reversed(input().split())))
|
cnt = int(input())
li = list(map(int,input().split()))
re_li = li[::-1]
print(' '.join(map(str,re_li)))
| 1 | 991,240,166,938 | null | 53 | 53 |
def abc164_d():
# 解説放送
s = str(input())
n = len(s)
m = 2019
srev = s[::-1] # 下の位から先に見ていくために反転する
x = 1 # 10^i ??
total = 0 # 累積和 (mod 2019 における累積和)
cnt = [0] * m # cnt[k] : 累積和がkのものが何個あるか
ans = 0
for i in range(n):
cnt[total] += 1
total += int(srev[i]) * x
total %= m
ans += cnt[total]
x = x*10 % m
print(ans)
abc164_d()
|
# ALDS1_11_C.py
import queue
def printNode():
for i, node in enumerate(glaph):
print(i, node)
N = int(input())
glaph = [[] for i in range(N+1)]
for i in range(1, N+1):
glaph[i] = list(map(int, input().split()))[2:]
# printNode()
step = [-1]*(N+1)
step[1] = 0
q = queue.Queue()
q.put(1)
while not q.empty():
now = q.get()
next_node = glaph[now]
# print("now is ", now, ", next is", next_node)
for i in next_node:
if step[i] != -1:
continue
q.put(i)
step[i] = step[now] + 1
for i, v in enumerate(step):
if i != 0:
print(i, v)
| 0 | null | 15,421,714,270,810 | 166 | 9 |
#入力:N,M(int:整数)
def input2():
return map(int,input().split())
d,t,s=input2()
if d/s >t:
print("No")
else:
print("Yes")
|
for i in range(9):
i=i+1
for j in range(9):
j=j+1
print(str(i)+'x'+str(j)+'='+str(i*j))
| 0 | null | 1,743,601,400,450 | 81 | 1 |
x=int(raw_input())
print x*x*x
|
n, m = map(int, raw_input().split())
c = map(int, raw_input().split())
dp = [n+1] * (n+1)
dp[n] = 0
for rest in xrange(n, 0, -1):
for i in xrange(m):
if c[i] <= rest:
dp[rest - c[i]] = min(dp[rest - c[i]], 1 + dp[rest])
print dp[0]
| 0 | null | 212,643,439,930 | 35 | 28 |
# coding: utf-8
# Your code here!
class UnionFind:
def __init__(self, size):
self.rank = [0 for i in range(size)]
self.parent = [-1 for i in range(size)]
self.children = [[i] for i in range(size)]
def Find(self, x):
parent = self.parent[x]
while parent >= 0:
x = parent
parent = self.parent[x]
return x
def Union(self, x, y):
root_x = self.Find(x)
root_y = self.Find(y)
if root_x == root_y:
return
else:
if self.rank[root_x] >= self.rank[root_y]:
self.parent[root_x] += self.parent[root_y]
self.parent[root_y] = root_x
self.rank[root_x] = max(self.rank[root_y] + 1, self.rank[root_x])
self.children[root_x] += self.children[root_y]
else:
self.parent[root_y] += self.parent[root_x]
self.parent[root_x] = root_y
self.rank[root_y] = max(self.rank[root_x] + 1, self.rank[root_y])
self.children[root_y] += self.children[root_x]
def Same(self, x, y):
return self.Find(x) == self.Find(y)
def FindRootAndSizeAndChildren(self):
return [(idx, -val, self.children[idx]) for idx, val in enumerate(self.parent) if val<0 ]
def print_lists(self):
print(self.rank)
print(self.parent)
print()
N, M = map(int, input().split())
unionfind = UnionFind(N)
for i in range(M):
a, b = map(int, input().split())
unionfind.Union(a-1, b-1)
max_size = 0
for idx, size, children in unionfind.FindRootAndSizeAndChildren():
if size > max_size:
max_size = size
print(max_size)
|
def cmb(n, r, mod):
if ( r<0 or r>n ):
return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
mod = 10**9+7
N = 10**6
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range( 2, N + 1 ):
g1.append( ( g1[-1] * i ) % mod )
inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
g2.append( (g2[-1] * inverse[-1]) % mod )
X,Y = list(map(int,input().split()))
if (X+Y)%3 != 0:
print(0)
exit()
N = (X+Y)//3
K = min(2*N-X,X-N)
print(cmb(N,K,mod))
| 0 | null | 76,778,731,613,248 | 84 | 281 |
n, m, x = list(map(int, input().split()))
books = []
for _ in range(n):
books.append(list(map(int, input().split())))
# 参考書の組み合わせ
price_min = pow(10, 6) * n
found = False
for bits in range(2 ** n):
level = [0] * m
price = 0
# ビットが立っている参考書の理解度と値段を加算
for book in range(n):
if (bits >> book) & 1:
price += books[book][0]
for alg in range(m):
level[alg] += books[book][alg + 1]
# 理解度がXに満たないアルゴリズムを抽出
level = list(filter(lambda lv: lv < x, level))
# 理解度の条件を満たし、かつ合計額が最低値を下回るときに最低値を更新
if len(level) == 0 and price < price_min:
found = True
price_min = price
print(price_min if found else "-1")
|
from copy import deepcopy
N, M, X = [int(i) for i in input().split()]
CAS = [[int(i) for i in input().split()] for _ in range(N)]
ok = False
mi = [float('inf'), *[0]*(M)]
for i in range(2**N):
t = [0]*(M+1)
for j in range(N):
if (i >> j) & 1:
t = [a+b for a, b in zip(CAS[j], t)]
if all(a >= X for a in t[1:]) and mi[0] > t[0]:
mi = t
ok = True
if ok:
print(mi[0])
else:
print(-1)
| 1 | 22,268,810,128,180 | null | 149 | 149 |
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)
|
H,W,M=map(int,input().split())
L=[]
bh=[0]*(H+1)
bw=[0]*(W+1)
for _ in range(M):
h,w=map(int,input().split())
L.append((h,w))
bh[h]+=1
bw[w]+=1
p,q=max(bh),max(bw)
num=len(list(filter(p.__eq__, bh)))*len(list(filter(q.__eq__,bw)))
num-=len(list(filter(lambda x: bh[x[0]]==p and bw[x[1]]==q, L)))
ans=p+q-1
if num>0:
ans+=1
print(ans)
| 1 | 4,679,850,671,320 | null | 89 | 89 |
H = []
W = []
i = 0
while True:
h, w = map(int, input().split())
H.append(h)
W.append(w)
if H[i] == 0 and W[i] == 0:
break
i += 1
i = 0
while True:
if H[i] == 0 and W[i] == 0:
break
for j in range(H[i]):
for k in range(W[i]):
print("#", end="")
print()
print()
i += 1
|
turn=int(input())
duel=[input().split(" ")for i in range(turn)]
point=[0,0]
for i in duel:
if i[0]==i[1]:
point[0],point[1]=point[0]+1,point[1]+1
if i[0]>i[1]:
point[0]+=3
if i[0]<i[1]:
point[1]+=3
print(" ".join(map(str,point)))
| 0 | null | 1,408,988,576,708 | 49 | 67 |
n=int(input())
zp=[]
zl=[]
for i in range(n):
a,b=map(int,input().split())
zp.append(a+b)
zl.append(a-b)
ans=max((max(zp)-min(zp)),(max(zl)-min(zl)))
print(ans)
|
N = int(input())
xs, ys = [], []
for _ in range(N):
x, y = map(int, input().split())
xs.append(x)
ys.append(y)
A = [x + y for x, y in zip(xs, ys)]
B = [x - y for x, y in zip(xs, ys)]
ans = max([max(A) - min(A), max(B) - min(B)])
print(ans)
| 1 | 3,446,510,559,970 | null | 80 | 80 |
n = int(input())
a,b = divmod(n,2)
if b == 0:
a -= 1
print(a)
|
n, k, c = map(int, input().split())
s = input()
l = []
tmp = 0
cnt = 0
while (tmp < n):
if s[tmp] == "o":
l.append(tmp)
tmp += c + 1
cnt += 1
else:
tmp += 1
if cnt > k:
exit()
l2 = []
tmp2 = n - 1
cnt2 = 0
while (tmp2 >= 0):
if s[tmp2] == "o":
l2.append(tmp2)
tmp2 -= c + 1
cnt2 += 1
else:
tmp2 -= 1
if cnt2 > k:
exit()
for i in range(k):
if l[i] == l2[k - 1 - i]:
print(l[i]+1)
| 0 | null | 96,989,272,215,640 | 283 | 182 |
import copy
def bubble_sort(C):
for i in range(len(C)):
for j in range(len(C)-1, i, -1):
if int(C[j][1]) < int(C[j-1][1]):
C[j], C[j-1] = C[j-1], C[j]
return C
def selection_sort(C):
for i in range(len(C)):
minj = i
for j in range(i, len(C)):
if int(C[minj][1]) > int(C[j][1]):
minj = j
C[minj], C[i], = C[i], C[minj]
return C
if __name__ == '__main__':
n = int(input())
B = input().split()
C_bubble = bubble_sort(copy.deepcopy(B))
print(*C_bubble)
print('Stable')
C_selection = selection_sort(copy.deepcopy(B))
print(*C_selection)
if C_bubble == C_selection:
print('Stable')
else:
print('Not stable')
|
s = list(input())
q = int(input())
for i in range(q):
cmd = list(input().split())
m = int(cmd[1])
n = int(cmd[2])
if cmd[0] == "print":
for i in range(m, n + 1):
print(s[i], end="")
print()
if cmd[0] == "reverse":
half = int((n - m) / 2)
while m <= half\
or m < n:
t = s[m]
s[m] = s[n]
s[n] = t
m += 1
n -= 1
if cmd[0] == "replace":
r = cmd[3]
j = 0
for i in range(m, n + 1):
s[i] = r[j]
j += 1
| 0 | null | 1,053,920,130,152 | 16 | 68 |
i = input()
i_l = i.split()
count = 0
for each in range(int(i_l[0]),int(i_l[1])+1):
if int(i_l[2]) % each == 0:
count += 1
else:
print(count)
|
x, y, z = input().split()
a = int(x)
b = int(y)
c = int(z)
count = 0
num = a
while num <= b:
if c % num == 0:
count += 1
else:
pass
num += 1
print(count)
| 1 | 547,994,750,070 | null | 44 | 44 |
def main():
n, k = [int(x) for x in input().split()]
scores = [int(x) for x in input().split()]
for index in range(n - k):
if scores[index] < scores[index + k]:
print('Yes')
else:
print('No')
if __name__ == '__main__':
main()
|
# C Marks
N, K = map(int, input().split())
A = list(map(int, input().split())) # リストがうまく作れてない?
j = 0
i = K
ct = 1
while ct <= (N - K):
if A[j] < A[i]:
print("Yes")
else:
print("No")
j += 1
i += 1
ct += 1
| 1 | 7,084,255,409,744 | null | 102 | 102 |
a,b,k=map(int,input().split())
if a>=k:
print(a-k,b)
else:
print(0,max(b+a-k,0))
|
A,B,K = map(int,input().split())
if K>A+B:
A,B = 0,0
elif K>A:
A,B = 0,B-K+A
else:
A -= K
print(f'{A} {B}')
| 1 | 104,430,741,604,630 | null | 249 | 249 |
#!/usr/bin/env python3
#coding: utf-8
# Volume0 - 0001 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0001)
li = []
for x in range(10):
s = input()
s = int(s)
li.append(s)
li.sort()
li.reverse()
for y in range(3):
print(li[y])
|
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
num = [i+1 for i in range(N)]
p_ord = 0
q_ord = 0
cnt = 0
for pre in itertools.permutations(num, N):
if P == list(pre):
p_ord = cnt
if Q == list(pre):
q_ord = cnt
cnt += 1
print(abs(p_ord-q_ord))
| 0 | null | 50,521,696,636,262 | 2 | 246 |
s = str(input())
n = len(s)
dp = [[0]*2 for _ in range(n+1)]
dp[0][1] = 1
for i in range(n):
p = int(s[i])
dp[i+1][0] = min(dp[i][1]+10-p,dp[i][0]+p)
dp[i+1][1] = min(dp[i][1]+10-p-1,dp[i][0]+p+1)
print(dp[-1][0])
|
import sys
S = int(sys.stdin.readline())
h = S / 3600
m = (S - h*3600)/60
s = S - h*3600 - m*60
print("%d:%d:%d" % (h,m,s))
| 0 | null | 35,599,455,555,972 | 219 | 37 |
#! /usr/local/bin/python3
# coding: utf-8
print(int(input()) ** 3)
|
#coding: UTF-8
input_number = int(input())
print input_number ** 3
| 1 | 286,604,764,090 | null | 35 | 35 |
import sys
N, M, K = map(int, input().split())
friends = [set() for _ in range(N)]
for _ in range(M):
a, b = map(int, sys.stdin.readline().split())
friends[a-1].add(b-1)
friends[b-1].add(a-1)
blocks = [set() for _ in range(N)]
for _ in range(K):
c, d = map(int, sys.stdin.readline().split())
blocks[c-1].add(d-1)
blocks[d-1].add(c-1)
done = set()
chains = []
todo = []
for s in range(N):
if s in done:
continue
chain = set()
todo.append(s)
while todo:
i = todo.pop()
for ni in friends[i]:
if ni in done:
continue
done.add(ni)
chain.add(ni)
todo.append(ni)
chains.append(chain)
ans = [0] * N
for chain in chains:
for i in chain:
blocks[i].intersection_update(chain)
ans[i] = len(chain) - len(blocks[i]) - len(friends[i]) - 1
print(" ".join(map(str, ans)))
|
from collections import Counter
class UnionFind:
from collections import deque
def __init__(self, v):
self.v = v
self._tree = list(range(v + 1))
def _root(self, a):
queue = self.deque()
while self._tree[a] != a:
queue.append(a)
a = self._tree[a]
while queue:
index = queue.popleft()
self._tree[index] = a
return a
def union(self, a, b):
root_a = self._root(a)
root_b = self._root(b)
self._tree[root_b] = root_a
def find(self, a, b):
return self._root(a) == self._root(b)
N, M, K = map(int, input().split(' '))
n_friends = Counter()
n_groups = Counter()
uf = UnionFind(N)
for _ in range(M):
a, b = map(int, input().split(' '))
a -= 1
b -= 1
uf.union(a, b)
n_friends[a] -= 1
n_friends[b] -= 1
for _ in range(K):
c, d = map(int, input().split(' '))
c -= 1
d -= 1
if uf.find(c, d):
n_friends[c] -= 1
n_friends[d] -= 1
for n in range(N):
n_groups[uf._root(n)] += 1
print(*[n_friends[n] + n_groups[uf._root(n)] - 1 for n in range(N)])
| 1 | 61,684,988,446,062 | null | 209 | 209 |
#174 A
X = input()
print('Yes') if int(X)> 29 else print('No')
|
x = int(input().rstrip())
if x >= 30:
print("Yes")
else:
print("No")
| 1 | 5,778,171,539,640 | null | 95 | 95 |
n, k, s = map(int, input().split())
A = [0] * n
if s == 10 ** 9:
for i in range(k):
A[i] = 10 ** 9
for i in range(k, n):
A[i] = 1
else:
for i in range(k):
A[i] = s
for i in range(k, n):
A[i] = s + 1
print(*A)
|
n,k,s=map(int,input().split())
if s!=10**9:ans=[s]*k+[10**9]*(n-k)
else:ans=[s]*k+[1]*(n-k)
print(*ans)
| 1 | 91,404,506,767,652 | null | 238 | 238 |
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mat = lambda x, y, v: [[v]*y for _ in range(x)]
ten = lambda x, y, z, v: [mat(y, z, v) for _ in range(x)]
mod = 1000000007
sys.setrecursionlimit(1000000)
N = ri()
C = rs()
l, r = 0, N-1
ans = 0
while l < r:
while C[l] == 'R' and l < N-1:
l += 1
while C[r] == 'W' and r > 0:
r -= 1
if l >= r: break
l += 1
r -= 1
ans += 1
print(ans)
|
N=int(input())
stones = list(input())
l = 0
r = N-1
count = 0
while(True):
while(stones[l] == "R" and l < N-1):
l += 1
while(stones[r] == "W" and r > 0):
r -= 1
if (l >= r):
print(count)
exit()
else:
count += 1
l += 1
r -= 1
| 1 | 6,345,432,979,392 | null | 98 | 98 |
a,b,c = map(int,input().split())
print("%d %d %d" %(c,a,b))
|
def ABC_142_A():
N = int(input())
guusuu=0
for i in range(N+1):
if i%2 == 0 and i !=0:
guusuu+=1
print(1-(guusuu/N))
if __name__ == '__main__':
ABC_142_A()
| 0 | null | 107,630,326,115,230 | 178 | 297 |
# https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
# https://matsu7874.hatenablog.com/entry/2019/12/01/230357
n = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
ans = 1
# 先頭はr/g/b
count = [3 if i == 0 else 0 for i in range(n + 1)]
for a in A:
ans = ans * count[a] % MOD
if ans == 0:
break
count[a] -= 1
count[a + 1] += 1
print(ans)
|
a,b,k=map(int,input().split())
taka=a-k
if taka < 0:
aoki = b-abs(taka)
taka = 0
if aoki < 0:
aoki = 0
else:
aoki=b
print(taka,aoki)
| 0 | null | 116,618,295,028,430 | 268 | 249 |
A, B, M = map(int, input().split())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))
prm_min = min(la) + min(lb)
lcost = list()
for i in range(M):
x, y, c = map(int, input().split())
lcost.append(la[x-1] + lb[y-1] - c)
print(min(min(lcost), prm_min))
|
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
n, m, k = map(int, input().split())
fr = UnionFind(n)
bl = [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
bl[a-1].append(b-1)
bl[b-1].append(a-1)
fr.union(a-1, b-1)
for _ in range(k):
c, d = map(int, input().split())
if fr.same(c-1, d-1):
bl[c-1].append(d-1)
bl[d-1].append(c-1)
ans = []
for i in range(n):
s = fr.size(i) - len(bl[i]) - 1
ans.append(s)
print(*ans)
| 0 | null | 57,804,922,265,850 | 200 | 209 |
S=str(input())
ans=''
for i in range(3):
ans+=S[i]
print(ans)
|
S=input()
S=str(S)
print(S[0]+S[1]+S[2])
| 1 | 14,666,093,532,192 | null | 130 | 130 |
mod = 10**9 + 7
from collections import deque
def main():
N = iip(False)
K = iip(False)
ret = solve(N, K)
print(ret)
def solve(N, K):
strn = str(N)
A = []
B = []
for i in range(len(strn), 0, -1):
if strn[len(strn)-i] != "0":
A.append(int(strn[len(strn)-i]))
B.append(i)
if K == 1:
return 9*(B[0]-1) + A[0]
if K == 2:
if len(strn) < 2:
return 0
ret = 0
ret += (B[0]-1) * (B[0]-2) // 2 * (9**2) #桁数がmaxじゃない場合
ret += (A[0]-1) * 9 * (B[0]-1) #桁数がmaxで先頭桁がmax以外の場合
if len(B) >= 2 and len(A) >= 2:
ret += (B[1]-1) * 9 + A[1] #先頭桁がmaxの場合
return ret
ret = 0
ret += (B[0] - 1) * (B[0] - 2) * (B[0] - 3) // 6 * 9**3 #桁数がmaxじゃない場合
ret += (A[0] - 1) * (B[0] - 1) * (B[0] - 2) // 2 * 9**2 #桁数がmaxで先頭桁がmaxじゃない場合
#以下、桁数はmaxで先頭桁はmaxとする
if len(strn) < 3:
return 0
if len(B) >= 2:
ret += (B[1]-1) * (B[1]-2) // 2 * 9**2 #有効2桁目の桁数がmaxじゃない場合
if len(B) >= 2 and len(A) >= 2:
ret += (A[1] - 1) * (B[1]-1) * 9 #有効2桁目の桁数がmaxで数字がmaxじゃない場合
if len(B) >= 3 and len(A) >= 3:
ret += (B[2] - 1) * 9 + A[2] #有効2桁目,3桁目がmaxの場合
return ret
#####################################################ライブラリ集ここから
def split_print_space(s):
print(" ".join([str(i) for i in s]))
def split_print_enter(s):
print("\n".join([str(i) for i in s]))
def searchsorted(sorted_list, n, side):
if side not in ["right", "left"]:
raise Exception("sideはrightかleftで指定してください")
l = 0
r = len(sorted_list)
if n > sorted_list[-1]:
#print(sorted_list)
return len(sorted_list)
if n < sorted_list[0]:
return 0
while r-l > 1:
x = (l+r)//2
if sorted_list[x] > n:
r = x
elif sorted_list[x] < n:
l = x
else:
if side == "left":
r = x
elif side == "right":
l = x
if side == "left":
if sorted_list[l] == n:
return r - 1
else:
return r
if side == "right":
if sorted_list[l] == n:
return l + 1
else:
return l
def iip(listed):
ret = [int(i) for i in input().split()]
if len(ret) == 1 and not listed:
return ret[0]
return ret
def soinsuu_bunkai(n):
ret = []
for i in range(2, int(n**0.5)+1):
while n % i == 0:
n //= i
ret.append(i)
if i > n:
break
if n != 1:
ret.append(n)
return ret
def conbination(n, r, mod, test=False):
if n <=0:
return 0
if r == 0:
return 1
if r < 0:
return 0
if r == 1:
return n
ret = 1
for i in range(n-r+1, n+1):
ret *= i
ret = ret % mod
bunbo = 1
for i in range(1, r+1):
bunbo *= i
bunbo = bunbo % mod
ret = (ret * inv(bunbo, mod)) % mod
if test:
#print(f"{n}C{r} = {ret}")
pass
return ret
def inv(n, mod):
return power(n, mod-2)
def power(n, p):
if p == 0:
return 1
if p % 2 == 0:
return (power(n, p//2) ** 2) % mod
if p % 2 == 1:
return (n * power(n, p-1)) % mod
if __name__ == "__main__":
main()
|
def zeroTrim(s):
while s[0] == '0':
if len(s) == 1:
break
s = s[1:]
return s
N = input()
K = int(input())
def calc(N, K):
digit = len(N)
res = 0
if K == 1:
if digit > 1:
res += (digit - 1) * 9
res += int(N[0])
elif K == 2:
if digit <= 1:
return 0
if digit > 2:
res += 9 * 9 * (digit - 1) * (digit - 2) // 2
for i in range(int(N[0])-1):
res += calc('9'*(digit-1), 1)
res += calc(zeroTrim(N[1:]), 1)
else:
if digit <= 2:
return 0
if digit > 3:
res += 9 * 9 * 9 * (digit - 1) * (digit - 2) * (digit - 3) // 6
for i in range(int(N[0])-1):
res += calc('9'*(digit-1), 2)
res += calc(zeroTrim(N[1:]), 2)
return res
print(calc(N, K))
| 1 | 75,795,209,786,352 | null | 224 | 224 |
n = int(input())
a = input()
s=list(map(int,a.split()))
q = int(input())
a = input()
t=list(map(int,a.split()))
i = 0
for it in t:
if it in s:
i = i+1
print(i)
|
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
from numba import njit
def getInputs():
D = int(readline())
CS = np.array(read().split(), np.int32)
C = CS[:26]
S = CS[26:].reshape((-1, 26))
return D, C, S
@njit('(i8, i4[:], i4[:, :], i4[:], )', cache=True)
def _compute_score1(D, C, S, out):
score = 0
last = np.zeros(26, np.int32)
for d in range(len(out)):
i = out[d]
score += S[d, i]
last[i] = d + 1
score -= np.sum(C * (d + 1 - last))
return last, score
def step1(D, C, S):
#out = np.zeros(D, np.int32)
out = []
LAST = 0
for d in range(D):
max_score = -10000000
best_i = 0
for i in range(26):
#out[d] = i
out.append(i)
last, score = _compute_score1(D, C, S, np.array(out, np.int32))
if max_score < score:
max_score = score
LAST = last
best_i = i
out.pop()
#out[d] = best_i
out.append(best_i)
return np.array(out), LAST, max_score
def output(out):
out += 1
print('\n'.join(out.astype(str).tolist()))
D, C, S = getInputs()
out, _, score = step1(D, C, S)
output(out)
| 0 | null | 4,819,244,807,808 | 22 | 113 |
import math
x = math.pi
r = float(input())
ans = r*2*x
print(ans)
|
def main():
N,M = map(int,input().split())
A = list(map(int,input().split()))
cnt = 0
for i in range(M):
cnt += A[i]
if N < cnt:
return -1
else:
return N - cnt
print(main())
| 0 | null | 31,774,349,052,380 | 167 | 168 |
n = int(input())
a = list(map(int, input().split()))
MAX = 10 ** 6 + 5
cnt = [0] * MAX
ok = [True] * MAX
for x in a:
cnt[x] += 1
ans = 0
for i in range(1,MAX):
if cnt[i] > 0:
for j in range(i*2, MAX, i):
ok[j] = False
if ok[i] and cnt[i] == 1:
ans += 1
print(ans)
|
n = int(input())
a = [int(x) for x in input().split()]
baisuu=[0]*(10**6+1)
for i in a:
if baisuu[i]>=1:
baisuu[i]=2
continue
for j in range(i,10**6+1,i):
baisuu[j]+=1
ans=0
for i in a:
if baisuu[i]==1:
ans+=1
print(ans)
| 1 | 14,431,822,225,696 | null | 129 | 129 |
import sys
import math
n = (int)(input())
money = 100000.0
for i in range(n):
money = math.ceil(money * 1.05 / 1000) * 1000
print((int)(money))
|
import sys
import math
r = float( sys.stdin.readline() )
print( "{:7f} {:7f}".format( r*r*math.pi, r*2*math.pi ) )
| 0 | null | 313,932,877,780 | 6 | 46 |
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque, defaultdict
from fractions import gcd
from functools import reduce
from itertools import groupby, combinations
from heapq import heapify, heappush, heappop
# sys.setrecursionlimit(5000)
n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]
def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return list(map(lambda x: x + d, A))
def ldec(A, d=1): return list(map(lambda x: x - d, A))
N = n1()
A = nn()
#MAX = 10**6
MAX = max(A)
dp = defaultdict(list)
p = 2
while p <= MAX:
i = p
while i <= MAX:
dp[i].append(p)
i += p
p += 1
while len(dp[p]) >= 1:
p += 1
if max(A) == 1:
print("pairwise coprime")
exit(0)
dic = defaultdict(list)
for ai in A:
fact = dp[ai]
for x in fact:
dic[x].append(ai)
maxlen = 0
for k, v in dic.items():
maxlen = max(len(v), maxlen)
if maxlen == 1:
print("pairwise coprime")
elif maxlen == N:
print("not coprime")
else:
print("setwise coprime")
|
import sys
import math
while True:
line = sys.stdin.readline()
if not line:
break
token = list(map(int, line.strip().split()))
print(int(math.log10(token[0] + token[1]) + 1))
| 0 | null | 2,065,938,108,322 | 85 | 3 |
#coding:utf-8
#1_2_D
def insertionSort(A, n, g):
global cnt
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
cnt += 1
A[j+g] = v
def shellSort(A, n):
G = [1]
while True:
g = 3*G[0] + 1
if g > n:
break
G.insert(0, g)
m = len(G)
for i in range(m):
insertionSort(A, n, G[i])
return [m, G]
n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
m, G = shellSort(A, n)
print(m)
print(" ".join(map(str, G)))
print(cnt)
print("\n".join(map(str, A)))
|
H,W=map(int,input().split())
S=[list(input())for _ in range(H)]
from collections import deque
def bfs(h,w,sy,sx,S):
maze=[[10**9]*(W)for _ in range(H)]
maze[sy-1][sx-1]=0
que=deque([[sy-1,sx-1]])
count=0
while que:
y,x=que.popleft()
for i,j in [(1,0),(0,1),(-1,0),(0,-1)]:
nexty,nextx=y+i,x+j
if 0<=nexty<h and 0<=nextx<w:
dist1=S[nexty][nextx]
dist2=maze[nexty][nextx]
else:
continue
if dist1!='#':
if dist2>maze[y][x]+1:
maze[nexty][nextx]=maze[y][x]+1
count=max(count,maze[nexty][nextx])
que.append([nexty,nextx])
return count
ans=0
for sy in range(H):
for sx in range(W):
if S[sy][sx]=='.':
now=bfs(H,W,sy+1,sx+1,S)
ans=max(ans,now)
print(ans)
| 0 | null | 47,552,724,126,400 | 17 | 241 |
n, k = map(int, input().split())
a = [i-1 for i in map(int, input().split())]
left, right = 1, 10**9
while left < right:
middle = (left + right)//2
count = sum(i//middle for i in a)
if count <= k:
right = middle
else:
left = middle + 1
print(left)
|
import sys,math
inputs = list()
for n in sys.stdin:
inputs.append(list(map(int,n.split())))
for n in inputs:
print(math.floor(math.log10(n[0]+n[1]))+1)
| 0 | null | 3,210,397,941,410 | 99 | 3 |
import sys
readline = sys.stdin.readline
inf = float('inf')
def main():
H, W = map(int, readline().split())
grid = []
grid.append(['*'] * (W+2))
for _ in range(H):
grid.append(['*'] + list(readline()[:-1]) + ['*'])
grid.append(['*']*(W+2))
DP = [[inf] * (W+2) for _ in range(H+2)]
DP[1][1] = (grid[1][1] == '#')*1
for i in range(1, H+1):
for j in range(1, W+1):
if i == 1 and j == 1:
continue
k = i
gridkj = grid[k][j]
if gridkj == '.':
DP[k][j] = min(DP[k][j-1], DP[k-1][j])
if gridkj == '#':
DP[k][j] = min(DP[k][j-1]+(grid[k][j-1] in ['.', '*']), DP[k-1][j] + (grid[k-1][j] in ['.', '*']))
ans = DP[H][W]
print(ans)
if __name__ == "__main__":
main()
|
h,w=map(int,input().split())
if h==1 or w==1:
print(1)
exit()
w-=1
even=int(w/2)+1
odd=int((w+1)/2)
if h%2==0:
print( (even+odd)*h//2 )
else:
print( (even+odd)*(h-1)//2 + even )
| 0 | null | 50,238,093,757,248 | 194 | 196 |
for i in range(1,10):
for j in range(1,10):
a=i*j
print(f'{i}x{j}={a}')
|
i = range(1,10)
for i in ['%dx%d=%d' % (x,y,x*y) for x in i for y in i]:
print i
| 1 | 2,009,800 | null | 1 | 1 |
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
K, X = input_nums()
if 500*K >= X:
print('Yes')
else:
print('No')
|
# coding: utf-8
# Your code here!
def main():
n, k = map(int, input().split())
a = []
for i in range(k):
d = int(input())
tmp_a = list(map(int, input().split()))
a.extend(tmp_a)
ans = len(set(range(1, n+1)) - set(a))
print(ans)
main()
| 0 | null | 61,522,608,591,260 | 244 | 154 |
#!/usr/bin/env python3
import sys
def solve(N: int, A: "List[int]"):
dp = [[0,0] for _ in range(N+1)]
dp[2] = [A[0],A[1]]
for i in range(2,N):
if (i+1)%2 ==0:
dp[i+1][0] = dp[i-1][0]+A[i-1]
else:
dp[i+1][0] = max(dp[i])
dp[i+1][1] = max(dp[i-1])+A[i]
print(max(dp[N]))
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
A = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
solve(N, A)
if __name__ == '__main__':
main()
|
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)
from pprint import pprint
def solve(N: int, A: "List[int]"):
memo = [{} for _ in range(N + 1)]
def rec(idx, rest, can_use=True):
k = (rest, can_use)
if rest * 2 >= N - idx + 2:
return 0
if rest == 0:
return 0
if k in memo[idx]:
return memo[idx][k]
ret = 0
if not can_use:
ret = rec(idx + 1, rest)
elif rest * 2 > (N - idx):
ret = A[idx] + rec(idx + 1, rest - 1, False)
else:
a = A[idx] + rec(idx + 1, rest - 1, False)
b = rec(idx + 1, rest)
ret = max(a, b)
memo[idx][k] = ret
return ret
ret = rec(0, N // 2)
print(ret)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
A = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
solve(N, A)
if __name__ == '__main__':
main()
| 1 | 37,549,055,703,268 | null | 177 | 177 |
N,M = map(int,input().split())
S = input()
c = 0
flag = 0
for i in S:
if i == '0':
if c >= M:
flag = 1
break
c = 0
else:
c += 1
if flag == 1:
print(-1)
else:
T = S[::-1]
now = 0
ans = []
while now != N:
delta = 0
for i in range(1,M+1):
if now + i == N:
delta = i
break
if T[now+i] == '0':
delta = i
ans.append(delta)
now += delta
Answer = ans[::-1]
print(*Answer)
|
def main():
l, r, d = map(int, input().split())
res = r // d - (l-1) // d
print(res)
if __name__ == '__main__':
main()
| 0 | null | 73,359,007,890,784 | 274 | 104 |
count = 0
W = input().lower()
T = [0]
while True:
try:
if T[0] == "END_OF_TEXT":
break
for t in T:
if W == t:
count += 1
T = [x.lower() for x in input().split()]
except EOFError:
break
except IndexError:
break
print(count)
|
w = raw_input()
t = []
while 1:
x = raw_input()
if x =="END_OF_TEXT":break
t+=x.lower().split()
print t.count(w)
| 1 | 1,830,431,828,092 | null | 65 | 65 |
from sys import stdin
input = stdin.readline
def rec(H):
if H == 1:
return 1
# print(H)
return 1 + 2*rec(int(H/2))
def main():
H = int(input())
print(rec(H))
if(__name__ == '__main__'):
main()
|
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
#from operator import itemgetter
#from heapq import heappush, heappop
#import numpy as np
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
#from scipy.sparse import csr_matrix
#from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
import sys
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
stdin = sys.stdin
ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip() # ignore trailing spaces
K = ni()
S = ns()
if len(S) <= K:
print(S)
else:
print(S[:K] + '...')
| 0 | null | 50,003,861,583,728 | 228 | 143 |
h, n = map(int, input().split())
aa = [int(a) for a in input().split()]
all = sum(aa)
if h <= all :
print("Yes")
else :
print("No")
|
# Date [ 2020-08-23 14:02:31 ]
# Problem [ e.py ]
# Author Koki_tkg
# After Contest
import sys; from decimal import Decimal
import math; from numba import njit, i8, u1, b1
import bisect; from itertools import combinations, product
import numpy as np; from collections import Counter, deque, defaultdict
# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846
def read_str(): return sys.stdin.readline().strip()
def read_int(): return int(sys.stdin.readline().strip())
def read_ints(x=0): return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)
def solve(h,w,bomb):
row = np.zeros(h, dtype=np.int64)
column = np.zeros(w, dtype=np.int64)
for y, x in bomb:
row[y] += 1
column[x] += 1
max_row = np.max(row)
max_col = np.max(column)
val = max_row + max_col
max_row_lis = np.ravel(np.where(row == row.max()))
max_col_lis = np.ravel(np.where(column == column.max()))
for r in max_row_lis:
for c in max_col_lis:
if (r, c) not in bomb:
return val
return val - 1
def Main():
h, w, m = read_ints()
bomb = set([tuple(read_ints(x=1)) for _ in range(m)])
print(solve(h, w, bomb))
if __name__ == '__main__':
Main()
| 0 | null | 41,416,511,581,962 | 226 | 89 |
n, m = map(int, input().split())
if m >= n:
print("Yes")
else:
print('No')
|
N, M = map(int, input().split())
if M - N >= 0:
print("Yes")
else:
print("No")
| 1 | 82,983,662,416,468 | null | 231 | 231 |
ans = []
while True:
arr = map(str, raw_input().split())
if arr[1] is '?':
break
val = eval(arr[0] + arr[1] + arr[2])
ans.append(val)
print("\n".join(map(str, ans)))
|
#coding:utf-8
def cal(a, b, op):
if op=="+":
return a + b
elif op == "-":
return a - b
elif op == "*":
return a * b
elif op == "/":
return a//b
else:
return -1
while True:
buff = input().split()
a = int(buff[0])
b = int(buff[2])
op = buff[1]
if op == "?":
break
print(cal(a, b, op))
| 1 | 698,571,125,190 | null | 47 | 47 |
r = int(input())
print( 2 * 3.14159268 * r )
|
import itertools
n = int(input())
S = list(input())
L = [k for k, v in itertools.groupby(S)]
print(len(L))
| 0 | null | 100,496,440,742,210 | 167 | 293 |
import sys
input = sys.stdin.readline
s = list(input())
a = 0
#print(len(s))
for i in range((len(s) - 1) // 2):
#print(s[i], s[-2-i])
if s[i] != s[-2-i]:
a += 1
print(a)
|
import math
from decimal import *
import random
mod = int(1e9)+7
s = str(input())
n =len(s)
if(n%2==0):
s1, s2 = s[:n//2], s[n//2:][::-1]
ans =0
for i in range(len(s1)):
if(s1[i]!= s2[i]):
ans+=1
print(ans)
else:
s1, s2 = s[:(n-1)//2], s[(n-1)//2:][::-1]
ans = 0
for i in range(len(s1)):
if(s1[i]!= s2[i]):
ans+=1
print(ans)
| 1 | 120,408,483,529,642 | null | 261 | 261 |
n = int(input())
a = [int(e) for e in input().split()]
ans = [0] * n
for i in range(n):
s = a[i]
ans[s-1] = i+1
print(*ans)
|
import sys
sys.setrecursionlimit(4100000)
import math
INF = 10**9
import itertools
def main():
n = int(input())
D = list(map(int, input().split()))
C = itertools.combinations(D, 2)
ans = 0
for c in C:
ans += c[0]*c[1]
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 174,357,040,232,464 | 299 | 292 |
A = list(map(int, input().split()))
N=A[0]
R=A[1]
if N>=10:
print(R)
else:
print(100*(10-N)+R)
|
N,R = map(int,input().split())
print( R if N >= 10 else R + 100*(10-N) )
| 1 | 63,448,110,701,756 | null | 211 | 211 |
def bubbleSort(A,N):
flag = 1 #????????????????????£??????????????????????????????????????¨??????
i = 0
while flag:
flag = 0
for j in range(N-1,i,-1):
if int(A[j][1]) < int(A[j-1][1]):
tmp = A[j]
A[j] = A[j-1]
A[j-1] = tmp
flag = 1
i += 1
return A
def selectionSort(A,N):
for i in range(0,N-1):
minj = i
j=i+1
for j in range(j,N):
if int(A[j][1]) < int(A[minj][1]):
minj = j
tmp = A[minj]
A[minj] = A[i]
A[i] = tmp
return A
def isStable(ori,sorted):
for i in range(len(ori)-1):
for j in range(i+1, len(ori)-1):
for a in range(len(ori)):
for b in range(a+1, len(ori)):
if ori[i][1] == ori[j][1] and ori[i] == sorted[b] and ori[j] == sorted[a]:
return 'Not stable'
return 'Stable'
def output(A):
for i in range(len(A)): # output
if i == len(A) - 1:
print(A[i])
else:
print(A[i], end=' ')
if __name__ == '__main__':
N = int(input())
numbers = input().split(' ')
n1 = numbers.copy()
n2 = numbers.copy()
A = bubbleSort(n1,N)
output(A)
print(isStable(numbers,A))
B = selectionSort(n2,N)
output(B)
print(isStable(numbers, B))
|
def is_stable(output_str, same_num_str):
if len(same_num_str)>0:
for i in range(len(same_num_str)):
if output_str.find(same_num_str[i]) == -1:
return "Not stable"
return "Stable"
data_num = int(input())
cards = input().split(" ")
cards2 = cards[:]
cnt = 0
num_1 = 0
num_2 = 0
same_num_str = []
output_str = ""
judge_stbl = ""
for i in range(data_num):
for j in range(data_num-1, i,-1):
if int(cards[j][1:]) < int(cards[j-1][1:]):
cards[j], cards[j-1] = cards[j-1], cards[j]
elif int(cards[j][1:]) == int(cards[j-1][1:]):
same_num_str.append(cards[j-1]+" "+cards[j])
output_str = " ".join(cards)
judge_stbl = is_stable(output_str, same_num_str)
print(output_str + "\n" + judge_stbl)
same_num_str = []
output_str = ""
judge_stbl = ""
for i in range(data_num):
min_idx = i
for j in range(i, data_num):
if int(cards2[j][1:]) < int(cards2[min_idx][1:]):
min_idx = j
elif int(cards2[j][1:]) == int(cards2[min_idx][1:]) and j != min_idx:
same_num_str.append(cards2[min_idx]+" "+cards2[j])
if i != min_idx:
cards2[i], cards2[min_idx] = cards2[min_idx], cards2[i]
output_str = " ".join(cards2)
judge_stbl = is_stable(output_str, same_num_str)
print(output_str + "\n" + judge_stbl)
| 1 | 23,565,615,018 | null | 16 | 16 |
s = input()
ans = "No"
for i in range(3):
if s[i]=="7" :
ans = "Yes"
print(ans)
|
a, b = map(int, input().split())
c = str(a)*b
d = str(b)*a
my_list = [c, d]
my_list.sort()
print(my_list[0])
| 0 | null | 59,349,132,682,840 | 172 | 232 |
N=int(input())
print(sum(j*(N//j)*(N//j+1)//2 for j in range(1,N+1)))
|
def main():
S, W = map(int, input().split(' '))
if S > W:
print('safe')
else:
print('unsafe')
main()
| 0 | null | 19,961,113,000,260 | 118 | 163 |
n = int(input())
l = [input() for i in range(n)]
d = {k: 0 for k in ['AC', 'WA', 'TLE', 'RE']}
for s in l:
d[s] += 1
for s in ['AC', 'WA', 'TLE', 'RE']:
print('{} x {}'.format(s, d[s]))
|
n=int(input())
s=[input() for i in range(n)]
ac=s.count("AC")
wa=s.count("WA")
tle=s.count("TLE")
re=s.count("RE")
print("AC x "+str(ac))
print("WA x "+str(wa))
print("TLE x "+str(tle))
print("RE x "+str(re))
| 1 | 8,770,351,944,288 | null | 109 | 109 |
import sys
input = sys.stdin.readline
n = int(input())
num = list(map(int, input().split()))
mod = 10**9+7
num = tuple(num)
def gcd(a, b):
while b: a, b = b, a % b
return a
def isPrimeMR(n):
d = n - 1
d = d // (d & -d)
L = [2]
for a in L:
t = d
y = pow(a, t, n)
if y == 1: continue
while y != n - 1:
y = (y * y) % n
if y == 1 or t == n - 1: return 0
t <<= 1
return 1
def findFactorRho(n):
m = 1 << n.bit_length() // 8
for c in range(1, 99):
f = lambda x: (x * x + c) % n
y, r, q, g = 2, 1, 1, 1
while g == 1:
x = y
for i in range(r):
y = f(y)
k = 0
while k < r and g == 1:
ys = y
for i in range(min(m, r - k)):
y = f(y)
q = q * abs(x - y) % n
g = gcd(q, n)
k += m
r <<= 1
if g == n:
g = 1
while g == 1:
ys = f(ys)
g = gcd(abs(x - ys), n)
if g < n:
if isPrimeMR(g): return g
elif isPrimeMR(n // g): return n // g
return findFactorRho(g)
def primeFactor(n):
i = 2
ret = {}
rhoFlg = 0
while i * i <= n:
k = 0
while n % i == 0:
n //= i
k += 1
if k: ret[i] = k
i += i % 2 + (3 if i % 3 == 1 else 1)
if i == 101 and n >= 2 ** 20:
while n > 1:
if isPrimeMR(n):
ret[n], n = 1, 1
else:
rhoFlg = 1
j = findFactorRho(n)
k = 0
while n % j == 0:
n //= j
k += 1
ret[j] = k
if n > 1: ret[n] = 1
if rhoFlg: ret = {x: ret[x] for x in sorted(ret)}
return ret
lcd = dict()
for i in num:
j = primeFactor(i)
for x,y in j.items():
if not x in lcd.keys():
lcd[x] = y
else:
lcd[x] = max(lcd[x],y)
lc = 1
for i,j in lcd.items():
lc *= pow(i,j,mod)
ans =0
for i in range(n):
ans += lc*pow(num[i], mod-2, mod)
ans %= mod
print(ans)
|
import math
H, W = map(int, input().split())
A = math.ceil((H * W)/2)
if H == 1 or W == 1:
print(1)
else:
print(A)
| 0 | null | 69,177,602,173,284 | 235 | 196 |
l = map (int, raw_input().split())
if l[0]<l[1]:
if l[1]<l[2]:
print 'Yes'
else:
print 'No'
else:
print 'No'
|
if __name__ == "__main__":
a, b, c = map( int, input().split() )
if a < b < c:
print("Yes")
else:
print("No")
| 1 | 378,744,680,990 | null | 39 | 39 |
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)
|
from itertools import permutations as per
n=int(input())
ans=[]
l=list(per(list(range(1,n+1))))
xy=[[] for i in range(n+1)]
for i in range(n):
x,y=map(int,input().split())
xy[i+1].append(x)
xy[i+1].append(y)
def f(x):
m=1
while x>0:
m*=x
x=x-1
return m
for i in range(f(n)):
dis=0
for j in range(n-1):
dis+=((xy[l[i][j]][0]-xy[l[i][j+1]][0])**2+(xy[l[i][j]][1]-xy[l[i][j+1]][1])**2)**(0.5)
ans.append(dis)
print(sum(ans)/f(n))
| 1 | 149,119,973,838,650 | null | 280 | 280 |
num1, num2, num3 = (int(x) for x in input().split())
count = 0
for i in range(num1, num2+1):
if i % num3 == 0:
count = count + 1
print(count)
|
while True:
table = input().split()
a = int(table[0])
op = table[1]
b = int(table[2])
if op == "?":
break
if op == "+":
print("%d"%(a+b))
elif op == "-":
print("%d"%(a-b))
elif op == "*":
print("%d"%(a*b))
else:
print("%d"%(a/b))
| 0 | null | 4,091,208,024,608 | 104 | 47 |
n = int(input())
a = list(map(int,input().split()))
ans = [0 for i in range(n)]
for i in a:
ans[i-1] += 1
for i in ans:
print(i)
|
n=int(input())
a=list(map(int, input().split()))
joushi = [0]*(n+1)
for i in range(n-1):
joushi[a[i]] += 1
del joushi[0]
for i in range(len(joushi)):
print(joushi[i])
| 1 | 32,525,333,955,422 | null | 169 | 169 |
from collections import defaultdict
def main():
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
S = [0] * (N + 1) # 累積和
for i in range(1, N + 1):
S[i] = S[i - 1] + A[i - 1]
T = [(s - i) % K for i, s in enumerate(S)]
counter = defaultdict(int)
ans = 0
for j in range(N + 1):
if j >= K:
counter[T[j - K]] -= 1
ans += counter[T[j]]
counter[T[j]] += 1
print(ans)
if __name__ == '__main__':
main()
|
dice = list(map(int,input().split()))
kaisuu = int(input())
lastlist = []
frlist = []
for i in range(kaisuu):
up,front = list(map(int,input().split()))
if up == dice[0] :
frlist = [1,2,4,3,1]
if up == dice[1]:
frlist = [0,3,5,2,0]
if up == dice[2]:
frlist = [0,1,5,4,0]
if up == dice[3]:
frlist = [0,4,5,1,0]
if up == dice[4]:
frlist = [0,2,5,3,0]
if up == dice[5] :
frlist = [1,3,4,2,1]
k = dice.index(int(front))
for n in frlist[0:4] :
if n == k :
l = frlist.index(int(n))
m = l + 1
lastlist.append(dice[frlist[m]])
for g in lastlist :
print(g)
| 0 | null | 68,568,869,120,778 | 273 | 34 |
def ints():
return [int(x) for x in input().split()]
def ii():
return int(input())
N, T = ints()
dp = [[0]*T for j in range(N+1)]
F = sorted([ints() for i in range(N)])
for i in range(N):
a, b = F[i]
for t in range(T):
dp[i+1][t] = max(dp[i+1][t], dp[i][t])
nt = t+a
if nt<T:
dp[i+1][nt] = max(dp[i+1][nt], dp[i][t]+b)
w = max([F[i][1] + dp[i][T-1] for i in range(1, N)])
print(w)
|
N, T = map(int, input().split())
dishes = sorted([tuple(map(int,input().split())) for i in range(N)])
dp = [[0] * 3100 for _ in range(3100)]
ans = 0
for i in range(1, N + 1):
for j in range(1, T):
#dpをもとにした最大値の更新
dp[i][j] = dp[i-1][j]
ans = max(ans, dp[i][j] + dishes[i-1][1]) #最後に、最も時間のかかるものを食べて、大きい場合は最大値(ans)を更新
# dpの更新
if j - dishes[i-1][0] >= 0: #最後に食べる品が持ち時間より長いばあいはNG
dp[i][j] = max(dp[i][j], dp[i-1][j-dishes[i-1][0]] + dishes[i-1][1]) #ラストオーダーまでにj-dishes[i-1][0]の時間があるので
print(ans)
| 1 | 152,277,548,841,600 | null | 282 | 282 |
n = int(input())
L = list(map(int, input().split()))
L.sort()
#print(L)
import bisect
ans = 0
for i in range(n-2):
for j in range(i+1, n-1):
k = bisect.bisect_left(L, L[i]+L[j])
#print(i, j, k)
ans += max(0, k-j-1)
print(ans)
|
s = input()
ret = s[:3]
print("{}".format(ret))
| 0 | null | 93,567,567,650,408 | 294 | 130 |
n=str(input())
a=list(n)
b=0
for i in a:
b=b+int(i)
if b%9==0:
print("Yes")
else:
print("No")
|
if int(input())%9==0:
print('Yes')
else:
print('No')
| 1 | 4,344,003,516,868 | null | 87 | 87 |
import math
p = [2]
def isprime(x):
if x == 2:
return True
if x == 3:
return True
if max(p) < x**0.5:
for j in range(max(p), math.floor(x**0.5) + 1):
if all([j % k != 0 for k in p]):
p.append(j)
i = 0
while i <= len(p)-1:
if x%p[i] == 0:
return False
if p[i] > x**0.5:
break;
i += 1
return True
n = int(input())
res = 0
for i in range(n):
k = int(input())
if isprime(k):
res += 1
print(res)
|
import math
n = int(input())
a = []
for i in range(n):
a.append(int(input()))
def isprime(x):
if x == 2:
return True
elif x % 2 == 0:
return False
i = 3
while i <= math.sqrt(x):
if x % i == 0:
return False
i += 2
else:
return True
counter = 0
for i in a:
if isprime(i) == True:
counter += 1
print(counter)
| 1 | 9,944,770,500 | null | 12 | 12 |
from itertools import combinations
n=int(input())
D=list(map(int,input().split()))
ans=0
for a,b in combinations(range(n),2):
ans+=D[a]*D[b]
print(ans)
|
def main():
import sys
readline = sys.stdin.buffer.readline
n = int(readline())
d = list(map(int, readline().split()))
e = sum(d)
ans = 0
for i in d:
e -= i
ans += i*e
print(ans)
if __name__ == '__main__':
main()
| 1 | 168,130,792,614,500 | null | 292 | 292 |
A,B,K = map(int,input().split())
if K>A+B:
A,B = 0,0
elif K>A:
A,B = 0,B-K+A
else:
A -= K
print(f'{A} {B}')
|
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
a, b, c = map(int, input().split())
if a + b + c > 21:
print('bust')
else:
print('win')
| 0 | null | 112,070,191,546,250 | 249 | 260 |
from collections import deque
N,M = map(int, input().split())
E = [[] for _ in range(N)]
for _ in range(M):
a,b = map(int, input().split())
E[a-1].append(b-1)
E[b-1].append(a-1)
used = [-1]*N
for i in range(N):
if used[i] >= 0:
continue
q = deque()
q.append(i)
used[i] = i
while q:
temp = q.popleft()
for e in E[temp]:
if used[e] == i:
continue
used[e] = i
q.append(e)
L = [0]*N
for c in used:
L[c] += 1
ans = max(L)
print(ans)
|
n = int(raw_input())
A = map(int, raw_input().strip().split(' '))
q = int(raw_input())
M = map(int, raw_input().strip().split(' '))
S = [0]*len(A)
flags = [False]*len(M)
def brute_force(n):
if n == len(S):
ans = 0
for i in range(len(S)):
if S[i] == 1: ans += A[i]
for i in range(len(M)):
if ans == M[i]: flags[i] = True
else:
S[n] = 0
brute_force(n+1)
S[n] = 1
brute_force(n+1)
brute_force(0)
for flag in flags:
if flag: print "yes"
else: print "no"
| 0 | null | 2,040,717,736,640 | 84 | 25 |
N, M = map(int, input().split())
S = input()
ans = []
now_pos = N
while now_pos > 0:
next_pos = max(0, now_pos - M)
for i in range(M):
if S[next_pos + i] == "0":
ans.append(now_pos - (next_pos + i))
now_pos = next_pos + i
break
else:
print(-1)
exit()
print(" ".join(map(str, ans[::-1])))
|
n = int(input())
h = [[] for _ in range(n)]
u = [[] for _ in range(n)]
for i in range(n):
a = int(input())
for _ in range(a):
x, y = map(int, input().split())
if y == 1:
h[i].append(x)
elif y == 0:
u[i].append(x)
ans = 0
for i in range(2**n):
hs, us = set(), set()
honest, unkind = set(), set()
for j in range(n):
if i & (1 << j):
hs |= set(h[j])
us |= set(u[j])
honest.add(j+1)
else:
unkind.add(j+1)
if not hs <= honest:
continue
if not us <= unkind:
continue
ans = max(ans, len(honest))
print(ans)
| 0 | null | 130,052,638,325,248 | 274 | 262 |
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
from bisect import bisect_left
def isok(n):
sum=0
for i in range(N):
sum+=N-bisect_left(A,n-A[i])
return sum>=M
start,end=0,2*10**5+1
while end-start>1:
mid=(start+end)//2
if isok(mid):
start=mid
else:
end=mid
b=[A[0]]
for i in range(1,N):
b.append(b[-1]+A[i])
l=0
ans=0
bn=b[N-1]
for i in range(N):
a=bisect_left(A,start-A[i])
l+=N-a
if a>0:
ans+=A[i]*(N-a)+bn-b[a-1]
else:
ans+=A[i]*(N-a)+bn
ans-=start*(l-M)
print(ans)
|
import bisect
import itertools
import numpy as np
def search_cut(N, M, A):
high_cut = A[-1] * 2
low_cut = A[0] * 2
while high_cut > low_cut + 1:
mid = (high_cut + low_cut) // 2
count = (N - np.searchsorted(A, mid - A, side = 'left')).sum()
if count > M:
low_cut = mid
else:
high_cut = mid
return low_cut, high_cut
def happiness(N, M, A, low_cut, high_cut):
A_sum = np.zeros(N + 1)
A_sum[1:] = np.cumsum(A)
X = np.searchsorted(A, high_cut - A, side = 'left')
happiness = (A_sum[-1] - A_sum[X]).sum()
happiness += ((N - X) * A).sum()
happiness += (M - (N - X).sum()) * low_cut
return int(happiness)
N, M = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.sort(A)
low_cut, high_cut = search_cut(N, M, A)
ans = happiness(N, M, A, low_cut, high_cut)
print(ans)
| 1 | 107,994,598,109,058 | null | 252 | 252 |
n,m=map(int,input().split())
li=[]
if n==1:
score="0"
else:
score="1"+"0"*(n-1)
for i in range(m):
s,c=map(int,input().split())
if n!=1 and (s,c)==(1,0):
print("-1")
quit()
li.append([s,c])
for j in range(len(li)):
if li[j][0]==s and li[j][1]!=c:
print("-1")
quit()
score=score[:s-1]+str(c)+score[s:]
print(int(score))
|
def lstToInt(l,x=0):
if len(l) == 0:
return x
else:
return lstToInt(l[1:], x*10 + l[0])
n,m = map(int,input().split())
c = [-1]*n
for i in range(m):
s, num = map(int,input().split())
if c[s-1] != -1 and c[s-1] != num:
print(-1)
exit()
elif s == 1 and num == 0 and n != 1:
print(-1)
exit()
else:
c[s-1] = num
if c[0] == -1 and n != 1:
c[0] = 1
ans = lstToInt(list(0 if c[i] == -1 else c[i] for i in range(n)))
print(ans)
| 1 | 60,532,654,238,616 | null | 208 | 208 |
def fibonacci(i, num_list):
if i == 0 or i == 1:
num_list[i] = 1
else:
num_list[i] = num_list[i-2] + num_list[i-1]
n = int(input()) + 1
num_list = [0 for i in range(n)]
for i in range(n):
fibonacci(i, num_list)
print(num_list[-1])
|
n = int(raw_input())
F = [0] * (n+1)
def fibonacci(n):
global F
if n == 0 or n == 1:
F[n] = 1
return F[n]
if F[n] != 0:
return F[n]
F[n] = fibonacci(n-2) + fibonacci(n-1)
return F[n]
def makeFibonacci(n):
global F
F[0] = 1
F[1] = 1
for i in xrange(2,n+1):
F[i] = makeFibonacci(i-2) + makeFibonacci(i-1)
return F[n]
def main():
ans = fibonacci(n)
print ans
return 0
main()
| 1 | 1,907,893,608 | null | 7 | 7 |
N = int(input())
A = list(map(int,input().split()))
s = sum(A)
MOD = 10 ** 9 + 7
ans = 0
for i in A:
s -= i
ans += i * s
print(ans % MOD)
|
number = int(input())
word = input()
answer = word.count('ABC')
print(answer)
| 0 | null | 51,687,062,159,726 | 83 | 245 |
N=int(input())
if N==1:
print('0')
exit()
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
def divcount(n):
cnt=0
while n>=0:
n-=cnt+1
cnt+=1
return cnt-1
s=factorization(N)
t=[i[1] for i in s]
ans=0
for i in t:
ans+=divcount(i)
print(ans)
|
s = input()
ss = ''
for i in s:
if i.islower():
l = i.upper()
elif i.isupper():
l =i.lower()
else: l = i
ss += l
print(ss)
| 0 | null | 9,116,538,712,470 | 136 | 61 |
# (+1,+2)をn回,(+2,+1)をm回とすると
# n + 2m = X
# 2n + m = Y でn,mが求まる.
# このとき答えは(n+m)Cn通り
def combination(n,r):
"""
高速な組み合わせの計算.
nCrは必ず整数になるため分母を全部約分で1にしてから残った分子の積を求める.
[参考]https://kadzus.hatenadiary.org/entry/20081211/1229023326
"""
if n-r < r: r = n-r
if r == 0: return 1
if r == 1: return int(n)
numerator = [i+1 for i in range(n-r, n)]
denominator = [i+1 for i in range(r)]
for p in range(2,r+1):
pivot = denominator[p-1]
if pivot > 1:
offset = (n-r)%p
for k in range(p-1, r, p):
numerator[k-offset] /= pivot
denominator[k] /= pivot
ans = 1
for i in range(r):
if numerator[i] > 1:
ans *= numerator[i]
if ans >= 1e9+7: ans %= 1e9+7
return int(ans)
X,Y = map(int, input().split())
if (X+Y)%3 != 0:
ans = 0
else:
n = int((2*Y-X)/3)
m = int(Y-2*n)
if n<0 or m<0:
ans = 0
else:
ans = combination(n+m, n)
print(ans)
|
n = int(input())
a = list(map(int, input().split()))
s = 0
for i in range(0, n - 1):
h = a[i] - a[i+1]
if h > 0:
s += h
a[i+1] += h
# a[i+1] = a[i]
print(str(s))
| 0 | null | 77,299,032,131,396 | 281 | 88 |
#ITP1_10-C Standard Deviation
while True:
n= int(input())
if n==0:
break
s = input().split(" ")
mean = 0.0
for i in range(n):
mean += float(s[i])
mean /= n
cov = 0.0
for i in range(n):
cov+=((float(s[i])-mean)**2)/n
print(cov**0.5)
|
n, k, s = map(int, input().split())
ans = []
for i in range(n):
if i < k:
ans.append(s)
else:
if s != 10**9:
ans.append(s+1)
else:
ans.append(1)
print(*ans)
| 0 | null | 45,574,903,184,356 | 31 | 238 |
import sys
stdin = sys.stdin
def main():
N = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().split()))
mod = 10**9+7
ans = 0
for i in range(61):
bits = 0
for x in A:
if (x>>i)&1:
bits += 1
ans += ((bits*(N-bits))* 2**i) %mod
ans %= mod
print(ans)
if __name__ == "__main__":
main()
|
d = int(input())
if d % 2 == 0:
print(d//2)
else:
print((d//2)+1)
| 0 | null | 91,282,272,122,080 | 263 | 206 |
import sys
input = sys.stdin.readline
N = int(input())
musics = []
for _ in range(N):
s, t = input().split()
musics.append((s.strip(), int(t)))
X = input().strip()
ans = 0
flag = False
for s, t in musics:
if flag:
ans += t
if s == X:
flag = True
print(ans)
|
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
xc = Counter()
yc = Counter()
for i, v in enumerate(a):
xc[i + v] += 1
yc[i - v] += 1
ans = 0
for v in xc:
ans += xc[v] * yc[v]
print(ans)
| 0 | null | 61,347,665,055,920 | 243 | 157 |
def f(s):
a=[-c]
for i,s in enumerate(s,1):
if s<'x'and a[-1]+c<i:a+=i,
return a[1:k+1]
n,s=open(0)
n,k,c=map(int,n.split())
for a,b in zip(f(s[:n]),f(s[-2::-1])[::-1]):
if a==n+1-b:print(a)
|
n, k, c = map(int, input().split())
s = input()
l = [0] * k
r = [0] * k
l_idx = 0
r_idx = k - 1
l_cnt = r_cnt = 0
for i in range(n):
if s[i] == "o" and (l_cnt >= c or l_idx == 0) and l_idx < k:
l[l_idx] = i
l_cnt = -1
l_idx += 1
if s[n-i-1] == "o" and (r_cnt >= c or r_idx == k - 1) and r_idx >= 0:
r[r_idx] = n - i - 1
r_cnt = -1
r_idx -= 1
l_cnt += 1
r_cnt += 1
for i in range(k):
if l[i] == r[i]:
print(l[i] + 1)
| 1 | 40,780,098,603,738 | null | 182 | 182 |
n, m = map(int, input().split())
a = list(map(int, input().split()))
dp = [float('inf') for _ in range(n+1)]
dp[0] = 0
for i in range(n):
for money in a:
next_money = i + money
if next_money > n:
continue
dp[next_money] = min(dp[i] + 1, dp[next_money])
print(dp[n])
|
a,b = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
ans = [10**9 for _ in range(a+1)]
ans[0] = 0
for i in range(a+1):
for j in l:
if i + j < a+1:
ans[i+j] = min(ans[i] + 1,ans[i+j])
print(ans[-1])
| 1 | 141,040,354,140 | null | 28 | 28 |
N = int(input()) % 10
if N in {2,4,5,7,9}:
print("hon")
elif N in {0,1,6,8}:
print("pon")
else:
print("bon")
|
N = input()
lastWord = int(N[-1])
if lastWord == 3:
print('bon')
elif lastWord == 0 or lastWord == 1 or lastWord == 6 or lastWord == 8:
print('pon')
else:
print('hon')
| 1 | 19,266,310,868,520 | null | 142 | 142 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.