code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 2
178,025B
⌀ | question_pair_id
float64 27.1M
177,113B
⌀ | code1_group
int64 1
297
| code2_group
int64 1
297
|
---|---|---|---|---|---|---|
N=int(input())
L=list(map(int,input().split()))
count=0
for a in range(N):
for b in range(N):
for c in range(N):
if L[a]<(L[b]+L[c]) and L[b]<(L[a]+L[c]) and L[c]<(L[b]+L[a]) and L[a]!=L[b] and L[a]!=L[c] and L[c]!=L[b] and a<b<c:
count=count+1
else:
continue
print(count)
|
from itertools import combinations
n = int(input())
L = list(map(int, input().split()))
cnt = 0
for edges in list(combinations(L, 3)):
if len(set(edges)) != 3:
continue
e1 = edges[0] < edges[1] + edges[2]
e2 = edges[1] < edges[0] + edges[2]
e3 = edges[2] < edges[0] + edges[1]
if e1 and e2 and e3:
cnt += 1
print(cnt)
| 1 | 5,076,000,307,732 | null | 91 | 91 |
def shu(a,b):
for i in range(a):
b[i]=list(map(int,input().split()))
n,m,l = map(int,input().split())
A=[[]for i in range(n)]
B=[[]for i in range(m)]
C=[[0 for i in range(l)]for i in range(n)]
shu(n,A)
shu(m,B)
for k in range(m):
for i in range(n):
for j in range(l):
C[i][j] += (A[i][k]*B[k][j])
for i in range(n):
print(' '.join(map(str,C[i])))
|
n,m,l = map(int,input().split())
A = []
for i in range(n):
a = [int(j) for j in input().split()]
A.append(a)
B = []
for j in range(m):
b = [int(k) for k in input().split()]
B.append(b)
for i in range(n):
c = []
for k in range(l):
x = 0
for j in range(m):
x += A[i][j]*B[j][k]
c.append(str(x))
print(" ".join(c))
| 1 | 1,439,778,513,172 | null | 60 | 60 |
a, b = map(int, input().split())
print(a // b, a % b, "{:.10f}".format(a / b))
|
K,X = map(int,input().split())
ans = 500 * K
if ans >= X:
print("Yes")
exit(0)
print("No")
| 0 | null | 49,512,137,610,142 | 45 | 244 |
not_ans = []
not_ans.append(int(input()))
not_ans.append(int(input()))
for i in range(1, 4):
if i not in not_ans:
print(i)
|
T=input()
list=[]
for j in range(len(T)):
if T[j]=="?":
list.append(j)
tt=[]
for TT in range(len(T)):
tt.append(T[TT])
for i in list:
tt[i]="D"
answer=""
for p in range(len(T)):
answer+=tt[p]
print(answer)
| 0 | null | 64,455,820,673,660 | 254 | 140 |
# ng, ok = 0, 10**9+1
n,k = map(int, input().split())
al = list(map(int, input().split()))
ng, ok = 0, 10**9+1
while abs(ok-ng) > 1:
mid = (ok+ng)//2
ok_flag = True
# ...
cnt = 0
for a in al:
cnt += (a-1)//mid
if cnt <= k:
ok = mid
else:
ng = mid
print(ok)
|
n, k = map(int, input().split())
A = list(map(int, input().split()))
l = 1
r = 10**9+1
while l < r:
mid = (l+r)//2
count = 0
for i in range(n):
if A[i] > mid:
count += A[i]//mid
if count <= k:
r = mid
else:
l = mid+1
print(l)
| 1 | 6,494,879,858,390 | null | 99 | 99 |
s = list(input())
days = 0
if s[1] == "R":
days += 1
if s[0] == "R":
days += 1
if s[2] == "R":
days += 1
else:
if s[0] == "R" or s[2] == "R":
days = 1
print(days)
|
x, n = map(int, input().split())
lis = []
if n == 0:
print(x)
else:
lis = list(map(int, input().split()))
if x not in lis:
print(x)
else:
y = x + 1
z = x - 1
while True:
if y in lis and z in lis:
y += 1
z -= 1
elif z not in lis:
print(z)
break
elif y not in lis:
print(y)
break
| 0 | null | 9,461,030,984,372 | 90 | 128 |
import math
a, b, x = map(int, input().split(" "))
cap = a * a * b
h = (x / cap) * b
if h / b >= 0.5:
gap = b - h
print(math.degrees(math.atan((gap / (a / 2)))))
else:
vol = h * a
gap = (vol / b) * 2
print(math.degrees(math.atan(b / gap)))
|
# D - Water Bottle
from math import atan, degrees
a, b, x = map(int, input().split())
volume = a * a * b
if x * 2 >= volume:
h = 2 * b - 2 * x / (a * a)
print(degrees(atan(h / a)))
else:
h = 2 * x / (a * b)
print(degrees(atan(b / h)))
| 1 | 163,617,492,344,398 | null | 289 | 289 |
import math
a, b, C = map(int, input().split())
C = C / 180 * math.pi
S = a * b * math.sin(C) / 2
print(S)
print(a + b + math.sqrt(a**2 + b**2 - 2*a*b*math.cos(C)))
print(2 * S / a)
|
import math
a,b,c=map(float,input().split())
C=math.radians(c)
D=math.sin(C)*a*b
S=D/2
E=a**2+b**2-2*a*b*math.cos(C)
F=math.sqrt(E)
L=a+b+F
if C<90:
h=b*math.sin(C)
elif C==90:
h=b
else:
h=b*math.sin(180-C)
print(S,L,h)
| 1 | 171,307,548,592 | null | 30 | 30 |
N = int(input())
A = list(map(int,input().split()))
cnt = {}
for i in A:
if i in cnt:
cnt[i] += 1
else:
cnt[i] = 1
total = 0
for i in cnt:
if cnt[i]>=2:
total += cnt[i] * (cnt[i]-1) // 2
for i in A:
if cnt[i] == 1:
print(total)
else:
#print(total - cnt[i]* (cnt[i]-1) // 2 + (cnt[i]-1) * (cnt[i]-2) // 2)
print(total - (cnt[i]-1))
|
n,m = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort(reverse = True)
s = sum(a)
res = "Yes"
for i in range(m):
if a[i] * 4 * m < s:
res = "No"
print(res)
| 0 | null | 43,380,900,262,056 | 192 | 179 |
def cin():
in_ = list(map(int,input().split()))
if len(in_) == 1: return in_[0]
else: return in_
N = cin()
A = cin()
INF = 10 ** 9 + 7
res = [0 for _ in range(65)]
for i in range(65):
c0, c1 = 0, 0
for j in range(N):
if bool(A[j] & (1 << i)): c1 += 1
else: c0 += 1
res[i] = c0 * c1
ans = 0
for i in range(65): ans = (ans + (1 << i) * res[i]) % INF
print(ans)
|
N = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
ans = 0
for i in range(60):
ones = 0
for a in A:
# 数列Aの全要素の(二進法表記の)i桁目の1の数を集計する
if a & (1 << i):
ones += 1
# Aの要素から任意の2つを選んで排他的論理和をとってできた数列をBとすると、
# 数列Bの全要素のi桁目の合計は、(Aのi桁目の1の数)*(0の数)となる
# 選んだ2つのAの要素が(0,1)の組み合わせになっている場合(XOR)だけに、Bの桁を構成する要素になるため
ans = (ans + ones * (N - ones) * (1 << i)) % mod
print(ans)
| 1 | 123,166,751,126,980 | null | 263 | 263 |
R, C, K = map(int, input().split())
xy = [[0] * (C + 1) for _ in range(R + 1)]
for _ in range(K) :
r, c, v = map(int, input().split())
xy[r - 1][c - 1] = v
dp = [[[0] * (C + 1) for _ in range(R + 1)] for _ in range(4)]
for cy in range(R + 1) :
for cx in range(C + 1) :
if cx < C :
dp[0][cy][cx + 1] = max(dp[0][cy][cx + 1], dp[0][cy][cx])
dp[1][cy][cx + 1] = max(dp[1][cy][cx + 1], dp[0][cy][cx] + xy[cy][cx], dp[1][cy][cx])
dp[2][cy][cx + 1] = max(dp[2][cy][cx + 1], dp[1][cy][cx] + xy[cy][cx], dp[2][cy][cx])
dp[3][cy][cx + 1] = max(dp[3][cy][cx + 1], dp[2][cy][cx] + xy[cy][cx], dp[3][cy][cx])
if cy < R :
dp[0][cy + 1][cx] = max(dp[i][cy][cx] for i in range(3)) + xy[cy][cx]
dp[0][cy + 1][cx] = max(dp[0][cy + 1][cx], dp[3][cy][cx])
print(max(dp[i][-1][-1] for i in range(4)))
|
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
if __file__=="test.py":
f = open("./in_out/input.txt", "r", encoding="utf-8")
read = f.read
readline = f.readline
def main():
R, C, K = map(int, readline().split())
L_INF = int(1e17)
dp = [[[-L_INF for _ in range(C+1)] for _ in range(R+1)] for _ in range(4)]
cell = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
x, y, c = map(int, readline().split())
cell[x-1][y-1] = c
dp[0][0][1] = dp[0][1][0] = 0
for i in range(1, R + 1):
for j in range(1, C + 1):
for k in range(4):
if k > 0:
dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j])
dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
if k > 0:
dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + cell[i-1][j-1])
if k == 1:
dp[1][i][j] = max(dp[1][i][j], dp[3][i-1][j] + cell[i-1][j-1])
print(dp[3][R][C])
if __name__ == "__main__":
main()
| 1 | 5,549,471,261,088 | null | 94 | 94 |
a = int(input())
result = a + pow(a, 2) + pow(a,3)
print(result)
|
import math
def is_prime(n):
global primes
if n == 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
s = int(math.sqrt(n))
for x in range(3, s + 1, 2):
if n % x == 0:
return False
else:
return True
N = int(input())
d = [int(input()) for _ in range(N)]
d.sort()
print([is_prime(x) for x in d].count(True))
| 0 | null | 5,135,783,730,530 | 115 | 12 |
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
C_A = Counter(A)
S = 0
for ca in C_A:
S += ca * C_A[ca]
Q = int(input())
for q in range(Q):
b, c = map(int,input().split())
S += (c - b) * C_A[b]
C_A[c] += C_A[b]
C_A[b] = 0
print(S)
|
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
NumList=[0]*(10**5+1)
AllSum=sum(A)
for a in A:
NumList[a]+=1
for i in range(Q):
B,C=map(int,input().split())
AllSum=AllSum + (C-B)*NumList[B]
NumList[C]+=NumList[B]
NumList[B]=0
print(AllSum)
| 1 | 12,239,207,145,438 | null | 122 | 122 |
N = int(input())
X = list(map(int,input().split()))
minimum = 10000000
for n in range(1,max(X)+1):
kyori = 0
for i in range(N):
kyori += (X[i]-n)**2
if kyori < minimum:
minimum = kyori
print(minimum)
|
def mk_tree(A,N):
B = [None for _ in range(N+1)]
B[0] = 1
#B[i]:深さiの頂点の個数
A_sum = [A[i] for i in range(N+1)]
for i in reversed(range(N)):
A_sum[i] += A_sum[i+1]
for i in range(1,N+1):
B[i] = min(2*(B[i-1]-A[i-1]),A_sum[i])
if B[N] == A[N]:
return sum(B)
else:
return -1
def main():
N = int(input())
A = list(map(int,input().split()))
print(mk_tree(A,N))
if __name__ == "__main__":
main()
| 0 | null | 42,196,210,324,620 | 213 | 141 |
X,K,D=(int(x) for x in input().split())
x=abs(X)
a=x//D
b=x-a*D
B=abs(x-(a+1)*D)
if b>B:
a=a+1
if a>=K:
print(abs(x-K*D))
else:
c=K-a
if c%2 == 0:
print(abs(x-a*D))
else:
d=abs(x-(a-1)*D)
e=abs(x-(a+1)*D)
print(min(d,e))
|
def main():
import numpy as np
n,t=map(int,input().split())
F=[tuple(map(int,input().split())) for _ in range(n)]
F.sort(key=lambda x:x[0])
dp=np.zeros([n+1,t],dtype=np.int64)
for i in range(n):
a1=F[i][0]
b1=F[i][1]
dp[i+1][:a1]=dp[i][:a1]
dp[i+1][a1:]=np.maximum(dp[i][a1:],dp[i][:-a1]+b1)
ans=0
for i in range(n):
ans=max(ans,dp[i][-1]+F[i][1])
print(ans)
if __name__=='__main__':
main()
| 0 | null | 78,599,816,159,840 | 92 | 282 |
K = input(str())
a = 'ACL'
if K == '1':
print(a)
if K == '2':
print(a * 2)
if K == '3':
print(a * 3)
if K == '4':
print(a * 4)
if K == '5':
print(a * 5)
|
n = int(input())
buf = list(map(int,input().split()))
a = []
for i in range(n):
a.append([buf[i],i])
a = sorted(a,reverse=True)
dp = [[0]*(n+1) for i in range(n+1)]
for i in range(n):
for j in range(n-i):
cur = i+j
temp1 = dp[i][j]+a[cur][0]*abs(n-1-a[cur][1]-j)
temp2 = dp[i][j]+a[cur][0]*abs(a[cur][1]-i)
dp[i+1][j] = max(dp[i+1][j],temp2)
dp[i][j+1] = max(dp[i][j+1],temp1)
print(max([max(i) for i in dp]))
| 0 | null | 17,880,253,271,130 | 69 | 171 |
n, r = [int(i) for i in input().split()]
ans = r if n >= 10 else r + 100 *(10 - n)
print(ans)
|
import numpy as np
from numba import jit
@jit(cache=True)
def solve(n,k,A):
l,r=0,0
for ki in range(k):
B=np.zeros(n+1,dtype=np.int64)
for i,j in enumerate(A):
l=max(0,i-j)
r=min(n,i+j+1)
B[l]+=1
B[r]-=1
A=np.cumsum(B[:-1])
if A[A==n].size==n:
break
return A
n,k=map(int,input().split())
A=list(map(int,input().split()))
A=np.array(A)
x=solve(n,k,A)
print(*x)
| 0 | null | 39,602,707,859,712 | 211 | 132 |
n, k = map(int, input().split())
p = list(map(lambda x: int(x) + 1, input().split()))
s = 0
e = k
t = sum(p[0:k])
ans = 0
while True:
ans = max(ans, t)
if e == n:
break
t -= p[s]
t += p[e]
s += 1
e += 1
print(ans / 2)
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a[:k])
mx = s
for i in range(k, n):
s += a[i] - a[i - k]
mx = max(mx, s)
print((mx + k) / 2)
| 1 | 75,180,825,181,658 | null | 223 | 223 |
import numpy as np
N = int(input())
A = []
B = []
for i in range(N):
a,b=map(int, input().split())
A.append(a)
B.append(b)
Aarray = np.array(A)
Barray = np.array(B)
medA = np.median(Aarray)
medB = np.median(Barray)
if N%2 == 1:
ans = medB-medA+1
else:
ans = 2*(medB-medA)+1
print(str(int(ans)))
|
from statistics import median
N = int(input())
A = [0] * N
B = [0] * N
max=0
min =0
for c in range(N):
A[c], B[c] = map(int, input().split())
med_A = median(A)
med_B = median(B)
if N%2==0:
print(int((med_B-med_A)*2+1))
else:
print(med_B-med_A+1)
| 1 | 17,178,673,170,960 | null | 137 | 137 |
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
import numpy as np
# sympy as syp(素因数分解とか)
Mod = 1000000007
def sieve_of_eratosthenes(n):
if not isinstance(n,int):
raise TypeError("n is not int")
if n<2:
raise ValueError("n is not effective")
prime = [1]*(n+1)
for i in range(2,int(math.sqrt(n))+1):
if prime[i] == 1:
for j in range(2*i,n+1):
if j%i == 0:
prime[j] = 0
res = []
for i in range(2,n+1):
if prime[i] == 1:
res.append(i)
return res
class UnionFind:
def __init__(self,n):
self.parent = [i for i in range(n+1)]
self.rank = [0 for i in range(n+1)]
def findroot(self,x):
if x == self.parent[x]:
return x
else:
y = self.parent[x]
y = self.findroot(self.parent[x])
return y
def union(self,x,y):
px = self.findroot(x)
py = self.findroot(y)
if px < py:
self.parent[y] = px
else:
self.parent[px] = py
def same_group_or_no(self,x,y):
return self.findroot(x) == self.findroot(y)
def main(): #startline-------------------------------------------
n, x, y = map(int, input().split())
ans = [0]*(n-1)
for i in range(n-1):
for j in range(i + 1, n):
if j < x - 1 or y - 1 < i:
distance = j - i
else:
distance = min(j - i, abs(x - 1 - i) + abs(y - 1 - j) + 1)
ans[distance - 1] += 1
for i in range(n - 1):
print(ans[i])
if __name__ == "__main__":
main() #endline===============================================
|
def allocate(n,w,k,p):
t=0
c=1
for i in range(n):
if t + w[i] > p:
t=0
c+=1
if c > k:
return -1
t+=w[i]
return 0
n,k=map(int,input().split())
w=[0]*n
m=(1<<30)*-1
s=0
for i in range(n):
w[i]=int(input())
m=max(m,w[i])
s+=w[i]
i=m
j=s
while (i<=j):
guess = int(i+(j-i)/2)
if allocate(n,w,k,guess) < 0:
i = guess + 1
else:
j = guess - 1
print(i)
| 0 | null | 21,993,217,539,512 | 187 | 24 |
S = input()
l = [0] * (len(S)+1)
r = [0] * (len(S)+1)
for i in range(len(S)):
if S[i] == '<':
l[i+1] = l[i] + 1
for i in range(len(S)-1,-1,-1):
if S[i] == '>':
r[i] = r[i+1] + 1
print(sum([max(l[i],r[i]) for i in range(len(S)+1)]))
|
#coding:utf-8
#1_6_A 2015.4.1
n = int(input())
numbers = list(map(int,input().split()))
for i in range(n):
if i == n - 1:
print(numbers[-i-1])
else:
print(numbers[-i-1], end = ' ')
| 0 | null | 78,514,074,269,618 | 285 | 53 |
#coding:utf-8
#3????????°???????????????
n = input()
print "",
for i in xrange(1, n+1):
if i % 3 == 0:
print i,
elif "3" in str(i):
print i,
|
x = list(map(int, input().split()))
top_count = 0
ans = 0
for i in x:
if i == 1:
ans += 300000
top_count += 1
elif i == 2:
ans += 200000
elif i == 3:
ans += 100000
if top_count == 2:
print(ans + 400000)
else:
print(ans)
| 0 | null | 70,820,000,269,470 | 52 | 275 |
import sys
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)
l, r = -1, 10 ** 12 + 1
while r - l > 1:
m = (l + r) // 2
cnt = 0
for i in range(n):
cnt += max(0, a[i] - (m // f[i]))
if cnt <= k:
r = m
else:
l = m
print(r)
|
import sys
read = sys.stdin.buffer.read
def main():
N, K, *AF = map(int, read().split())
A = AF[:N]
F = AF[N:]
A.sort()
F.sort(reverse=True)
ok = pow(10, 12)
ng = -1
while ok - ng > 1:
mid = (ok + ng) // 2
k = 0
for i in range(N):
if A[i] * F[i] > mid:
k += A[i] - mid // F[i]
if k <= K:
ok = mid
else:
ng = mid
print(ok)
return
if __name__ == '__main__':
main()
| 1 | 164,251,369,004,862 | null | 290 | 290 |
while True:
a=int(input())
if a==0 :
break
b=list(map(float,input().split()))
m=sum(b)/a
ver=0.0
for i in b :
ver+=(i-m)**2/a
s=(ver)**0.5
print(s)
|
a,b,c=input().split()
a=int(a)
b=int(b)
c=int(c)
ls=[a,b,c]
ls.sort()
if ls[0]==ls[1]:
if ls[1]==ls[2]:
print("No")
else:
print("Yes")
else:
if ls[1]==ls[2]:
print("Yes")
else:
print("No")
| 0 | null | 34,169,304,548,268 | 31 | 216 |
import sys
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
def lcm(a, b):
return a * b // gcd(a, b)
def main():
for i in sys.stdin.readlines():
a, b = [int(x) for x in i.split()]
print(gcd(a, b), lcm(a, b))
if __name__ == '__main__':
main()
|
a,b,k = map(int,input().split())
if a < k:
print(max(0,a-k),max(0,b+a-k))
else:
print(max(0,a-k),b)
| 0 | null | 52,150,503,709,300 | 5 | 249 |
import fractions
def c2(a):
ans = 0
while True:
q,r = divmod(a,2)
if r==0:
ans+=1
a = q
else:break
return ans
gcd = fractions.gcd
n,m= map(int,input().split())
A = list(map(lambda x:int(x)//2,input().split()))
A.sort()
ma = 0
cp = c2(A[0])
for i in range(n):
if cp!=c2(A[i]):
print(0)
exit()
lcm = A[0]
for i in range(1,n):
lcm = A[i]*lcm//gcd(A[i],lcm)
print((m//lcm +1)//2)
|
# 最小公倍数(mathは3.5以降) fractions
from functools import reduce
import fractions #(2020-0405 fractions→math)
def lcm_base(x, y):
return (x * y) // fractions.gcd(x, y) # 「//」はフロートにせずにintにする
def lcm(*numbers):
return reduce(lcm_base, numbers, 1)
def lcm_list(numbers):
return reduce(lcm_base, numbers, 1)
N,M = (int(x) for x in input().split())
A = list(map(int, input().split()))
lcm =lcm_list(A)
han_lcm =lcm//2
han_lcm_num =M//han_lcm
han_lcm_num =(han_lcm_num +1)//2 #偶数を除く
#半公倍数がA自身の中にある時は✖
if han_lcm in A:
han_lcm_num =0
#半公倍数がaiで割り切れるときは✖
for a in A:
if han_lcm % a ==0:
han_lcm_num =0
print(han_lcm_num)
| 1 | 101,693,322,443,382 | null | 247 | 247 |
a, _ = map(int, input().split())
c, d = map(int, input().split())
if a != c and d == 1:
print("1")
else:
print("0")
|
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 1000000007
plus = []
minus = []
ans = 1
lp = 0
lm = 0
lz = 0
for k in range(N):
if A[k] > 0:
plus.append(A[k])
lp += 1
elif A[k] < 0:
minus.append(A[k])
lm += 1
else:
lz += 1
plus.sort(reverse = True)
minus.sort()
def main(mp, mm, plus, minus):
mod = 1000000007
ans = 1
for k in range(mp):
ans = ans * plus[k] % mod
for k in range(mm):
ans = ans * minus[k] % mod
print(ans)
exit()
if lz + K > N:
print(0)
exit()
elif lp == 0:
if K % 2 == 1:
if lz > 0:
print(0)
exit()
else:
for k in range(K):
ans = ans * minus[- k - 1] % mod
print(ans)
exit()
else:
main(0, K, plus, minus)
elif lm == 0:
main(K, 0, plus, minus)
p = 0
m = 0
for _ in range(K):
if plus[p] >= -1 * minus[m]:
p += 1
if p == lp:
m = K - p
break
else:
m += 1
if m == lm:
p = K - m
break
if m % 2 == 0:
main(p, m, plus, minus)
else:
if p == lp:
if m != lm:
main(p - 1, m + 1, plus, minus)
else:
if lz > 0:
print(0)
exit()
else:
main(p, m, plus, minus)
elif m == lm:
main(p + 1, m - 1, plus, minus)
elif p == 0:
main(p + 1, m - 1, plus, minus)
else:
if plus[p] * plus[p - 1] > minus[m - 1] * minus[m]:
main(p + 1, m - 1, plus, minus)
else:
main(p - 1, m + 1, plus, minus)
| 0 | null | 66,972,341,453,692 | 264 | 112 |
import math
N = int(input())
if N % 2 == 1:
print(0)
else:
i = 10
ans = 0
while(True):
a = math.floor(N // i)
if a < 1:
break
ans += a
i *= 5
print(ans)
|
def resolve():
'''
code here
'''
N = input()
res = 0
if int(str(N)[-1]) % 2 == 0:
i = 0
N=int(N)
while int(N) >= 2*5**i:
i += 1
res += N//(2*5**i)
print(res)
if __name__ == "__main__":
resolve()
| 1 | 115,580,091,141,580 | null | 258 | 258 |
import math
while 1:
n=int(input())
if n==0:
break
ave=0.0
aru=0.0
m=[float(i) for i in input().split()]
for i in range(n):
ave=ave+m[i]
ave=ave/n
for i in range(n):
aru=aru+(m[i]-ave)*(m[i]-ave)
aru=math.sqrt(aru/n)
print("%.5f"% aru)
|
if __name__ == '__main__':
from statistics import pstdev
while True:
# ??????????????\???
data_count = int(input())
if data_count == 0:
break
scores = [int(x) for x in input().split(' ')]
# ?¨??????????????¨????
result = pstdev(scores)
# ???????????????
print('{0:.8f}'.format(result))
| 1 | 190,766,030,250 | null | 31 | 31 |
h,w,k = list(map(int,input().split())); arr = [[int(i) for i in input()] for _ in range(h)]
from itertools import product
ans = 2000
for cond in product([True,False],repeat=(h-1)):
cut = sum(cond)
splits = [arr[0][:]]
for i in range(1,h):
if cond[i-1]:
splits.append(arr[i][:])
else:
splits[-1] = [splits[-1][j]+arr[i][j] for j in range(w)]
check = [max(i) for i in splits]
if max(check) > k:
break
count = [i[0] for i in splits]
div = cut
for j in range(1,w):
addarr = [count[i]+splits[i][j] for i in range(cut+1)]
if max(addarr) > k:
div += 1
count = [splits[i][j] for i in range(cut+1)]
else:
count = addarr[:]
ans = min(ans,div)
print(ans)
|
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()
H, W, K = map(int, input().split())
S = [input() for _ in range(H)]
mi = None
def _max(iterable):
ma = 0
for i in iterable:
ma = max(ma, i)
return ma
for i in range(2 ** (H - 1)):
# div[j] is true if row j and row j + 1 is divided
div = [(i & (1 << j)) > 0 for j in range(H - 1)]
group = [0] * H
for j in range(H - 1):
group[j + 1] = group[j] + 1 if div[j] else group[j]
count = Counter()
ans = div.count(True)
ok = True
for j in range(W):
tmp = Counter()
for k in range(H):
if S[k][j] == '1':
tmp[group[k]] += 1
if _max(tmp.values()) > K:
ok = False
elif _max((tmp + count).values()) <= K:
count += tmp
else:
count = tmp
ans += 1
if ok and (mi is None or ans < mi):
mi = ans
print(mi)
| 1 | 48,519,480,228,030 | null | 193 | 193 |
n = int(input())
print(int(n / 2) - 1 if n % 2 == 0 else int(n / 2))
|
#python 3.7.1
N=int(input())
if (N%2)==0:
ans=N//2-1
else:
ans=N//2
print(ans)
| 1 | 153,130,654,636,102 | null | 283 | 283 |
import math
def main():
a, b, n = map(int, input().split())
x = n if (b-1) > n else b-1
print(math.floor(a*x/b)-a*math.floor(x/b))
if __name__ == '__main__':
main()
|
# 具体例を試す
import math
import sys
import os
from operator import mul
sys.setrecursionlimit(10**7)
def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))
if os.getenv("LOCAL"):
inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
sys.stdin = open(inputFile, "r")
INF = float("inf")
A,B,N = LI()
# ans = 0
# a = [0]*N
# for x in range(1,N+1):
# # ans = max(ans,math.floor(A*x/B) - A * math.floor(x/B))
# a[x-1]=math.floor(A*x/B) - A * math.floor(x/B)
x = min(N,B-1)
ans = math.floor(A*x/B) - A * math.floor(x/B)
print(ans)
| 1 | 28,057,938,089,728 | null | 161 | 161 |
X,Y,A,B,C=map(int,input().split())
ls_a=sorted(list(map(int,input().split())))
ls_b=sorted(list(map(int,input().split())))
ls_c=sorted(list(map(int,input().split())))
ls_x=ls_a[A-X:A]
ls_y=ls_b[B-Y:B]
ls_c.reverse()
ans=sum(ls_x)+sum(ls_y)
a=b=c=0
for _ in range(min([X+Y,C])):
if a==len(ls_x):
m=min([ls_y[b],ls_c[c]])
if m==ls_y[b]:
ans+=(-ls_y[b]+ls_c[c])
b+=1
c+=1
else:
break
elif b==len(ls_y):
m=min([ls_x[a],ls_c[c]])
if m==ls_x[a]:
ans+=(-ls_x[a]+ls_c[c])
a+=1
c+=1
else:
break
else:
m=min([ls_x[a],ls_y[b],ls_c[c]])
if m==ls_x[a]:
ans+=(-ls_x[a]+ls_c[c])
a+=1
c+=1
elif m==ls_y[b]:
ans+=(-ls_y[b]+ls_c[c])
b+=1
c+=1
else:
break
print(ans)
|
import sys
import math
from collections import deque
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]
#隣接リスト 1-order
def make_adjlist_d(n, edges):
res = [[] for _ in range(n + 1)]
for edge in edges:
res[edge[0]].append(edge[1])
res[edge[1]].append(edge[0])
return res
def make_adjlist_nond(n, edges):
res = [[] for _ in range(n + 1)]
for edge in edges:
res[edge[0]].append(edge[1])
return res
#nCr
def cmb(n, r):
return math.factorial(n) // math.factorial(r) // math.factorial(n - r)
def main():
X, Y, A, B, C = NMI()
P = NLI()
Q = NLI()
R = NLI()
reds = [[1, p] for p in P]
greens = [[2, q] for q in Q]
skels = [[0, r] for r in R]
apples = reds + greens + skels
apples.sort(key=lambda x: x[1], reverse=True)
colors = [0, 0, 0]
limits = [10**9, X, Y]
ans = 0
for color, a in apples:
if sum(colors) >= X + Y:
break
if colors[color] <= limits[color] - 1:
colors[color] += 1
ans += a
continue
print(ans)
if __name__ == "__main__":
main()
| 1 | 44,789,536,095,902 | null | 188 | 188 |
n,k = map(int,input().split())
A = list(map(int,input().split()))
mod = 10**9 + 7
A.sort(key = lambda x:abs(x),reverse = True)
ans = 1
last = -1
lastp = -1
cnt = 0
for a in A:
if a >= 0:break
else:
if k&1:
for i in range(k):
ans *= A[n-i-1]
ans %= mod
else:
for i in range(k):
ans *= A[i]
ans %= mod
print(ans)
exit()
for i in range(k):
ans *= A[i]
ans %= mod
if A[i] < 0:
last = i
cnt += 1
if A[i] > 0:
lastp = i
if n == k:
print(ans%mod)
exit()
if cnt&1:
first = 0
firstp = 0
for a in A[k:]:
if a > 0 and firstp == 0:
firstp = a
if a < 0 and first == 0:
first = a
if first == 0 and firstp == 0:
ans = 0
elif first == 0 or lastp == -1:
ans *= pow(A[last],mod-2,mod)*firstp%mod
ans %= mod
elif firstp == 0:
ans *= pow(A[lastp],mod-2,mod)*first%mod
ans %= mod
else:
if A[lastp]*firstp <= A[last]*first:
ans *= pow(A[lastp],mod-2,mod)*first%mod
ans %= mod
else:
ans *= pow(A[last],mod-2,mod)*firstp%mod
ans %= mod
print(ans%mod)
|
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]
def inside(y, x, H, W):
return 0 <= y < H and 0 <= x < W
def ceil(a, b):
return (a + b - 1) // b
def sum_of_arithmetic_progression(s, d, n):
return n * (2 * s + (n - 1) * d) // 2
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b):
g = gcd(a, b)
return a / g * b
def solve():
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7
num_zero = 0
neg_list = []
pos_list = []
for a in A:
if a == 0:
num_zero += 1
elif a < 0:
neg_list.append(a)
else:
pos_list.append(a)
# 0にしかできない
if len(pos_list) + len(neg_list) < K:
print(0)
else:
min_num_neg = K - len(pos_list)
max_num_neg = min(K, len(neg_list))
# 負にしかできない
if min_num_neg == max_num_neg and min_num_neg % 2 == 1:
if num_zero > 0:
print(0)
return
ans = 1
neg_list.sort()
pos_list.sort(reverse=True)
for i in range(K):
use_pos = False
if len(neg_list) == 0 or len(pos_list) == 0:
if len(neg_list) == 0:
use_pos = True
else:
use_pos = False
elif abs(neg_list[-1]) < pos_list[-1]:
use_pos = False
else:
use_pos = True
if use_pos:
ans *= pos_list[-1]
pos_list.pop()
else:
ans *= neg_list[-1]
neg_list.pop()
ans %= MOD
print(ans)
else:
# 正にできる
ans = 1
neg_list.sort(reverse=True)
pos_list.sort()
if K % 2 == 1:
K -= 1
ans *= pos_list[-1]
pos_list.pop()
# posもnegも偶数個ずつ使う
for _ in range(0, K, 2):
use_pos = False
if len(neg_list) <= 1 or len(pos_list) <= 1:
if len(neg_list) <= 1:
use_pos = True
else:
use_pos = False
elif abs(neg_list[-1] * neg_list[-2]) > (pos_list[-1] * pos_list[-2]):
use_pos = False
else:
use_pos = True
if use_pos:
ans *= pos_list[-1] * pos_list[-2]
pos_list.pop()
pos_list.pop()
else:
ans *= neg_list[-1] * neg_list[-2]
neg_list.pop()
neg_list.pop()
ans %= MOD
print(ans)
def main():
solve()
if __name__ == '__main__':
main()
| 1 | 9,328,321,673,440 | null | 112 | 112 |
import sys
import math
n, m, l = map(int, raw_input().split())
A = [[0 for i in xrange(m)] for j in xrange(n)]
B = [[0 for i in xrange(l)] for j in xrange(m)]
for i in xrange(n):
A[i] = map(int, raw_input().split())
for i in xrange(m):
B[i] = map(int, raw_input().split())
for i in xrange(n):
for j in xrange(l):
sm = 0
for k in xrange(m):
sm += A[i][k] * B[k][j]
sys.stdout.write(str(sm))
if j < l-1:
sys.stdout.write(" ")
print
|
n,m,l = [int(i) for i in input().split()]
a = [[int(i) for i in input().split()] for j in range(n)]
b = [[int(i) for i in input().split()] for j in range(m)]
c = [[0 for i in range(l)] for j in range(n)]
for i in range(n):
for j in range(l):
for k in range(m):
c[i][j] += a[i][k] * b[k][j]
for i in range(n):
print(' '.join(map(str,c[i])))
| 1 | 1,441,817,324,590 | null | 60 | 60 |
n=int(input())
if n%1000!=0:
print(1000-n%1000)
else:
print(0)
|
a = list(map(int,input().split()))
b = [0, 0, 0, 0]
b[0] = a[0]*a[2]
b[1] = a[0]*a[3]
b[2] = a[1]*a[2]
b[3] = a[1]*a[3]
b.sort()
print(b[3])
| 0 | null | 5,744,153,151,164 | 108 | 77 |
k,s=open(0);k=int(k);print(s[:k]+'...'*(k<~-len(s)))
|
def main():
k = int(input())
s = str(input())
output = ''
if len(s) > k:
for i in range(k):
output += s[i]
output += '...'
else:
output = s
print(output)
if __name__ == '__main__':
main()
| 1 | 19,588,345,780,990 | null | 143 | 143 |
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
memo = [[None]*2000 for i in range(2000)]
def exhaustivesearch(i, target):
if memo[i][target] is not None:
return memo[i][target]
if target == 0:
memo[i][target] = True
return True
if i >= n:
memo[i][target] = False
return False
else:
memo[i][target] = exhaustivesearch(i + 1, target) or exhaustivesearch(i + 1, target-A[i])
return memo[i][target]
for j in m:
if exhaustivesearch(0, j):
print("yes")
else:
print("no")
|
from itertools import combinations
n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = map(int, input().split())
s = set()
for i in range(1, n):
for j in combinations(a, i):
s.add(sum(j))
for ans in m:
print('yes' if ans in s else 'no')
| 1 | 99,689,382,080 | null | 25 | 25 |
n = input()
k = input()
a = k.count("R")
print(k[:a].count("W"))
|
def dijkstra(v, G):
import heapq
ret = [10 ** 10] * len(G)
ret[v] = 0
q = [(ret[i], i) for i in range(len(G))]
heapq.heapify(q)
while len(q):
tmpr, u = heapq.heappop(q)
if tmpr == ret[u]:
for w in G[u]:
if ret[w] > ret[u] + 1:
ret[w] = ret[u] + 1
heapq.heappush(q, (ret[w], w))
return ret
N, M = map(int, input().split())
G, ans = {i: [] for i in range(N)}, [-1] * N
for _ in range(M):
A, B = map(int, input().split())
G[A - 1].append(B - 1)
G[B - 1].append(A - 1)
d = dijkstra(0, G)
for a in range(N):
for b in G[a]:
if d[a] - d[b] == 1:
ans[a] = b + 1
print("Yes")
for a in ans[1:]:
print(a)
| 0 | null | 13,367,290,259,840 | 98 | 145 |
a = int(input())
b = int(input())
ans_list = [1, 2, 3]
ans_list.remove(a)
ans_list.remove(b)
print(ans_list[-1])
|
import sys
input = sys.stdin.readline
h,w,m = map(int,input().split())
h_array = [ 0 for i in range(h) ]
w_array = [ 0 for i in range(w) ]
ps = set()
for i in range(m):
hi,wi = map( lambda x : int(x) - 1 , input().split() )
h_array[hi] += 1
w_array[wi] += 1
ps.add( (hi,wi) )
h_great = max(h_array)
w_great = max(w_array)
h_greats = list()
w_greats = list()
for i , hi in enumerate(h_array):
if hi == h_great:
h_greats.append(i)
for i , wi in enumerate(w_array):
if wi == w_great:
w_greats.append(i)
ans = h_great + w_great
for _h in h_greats:
for _w in w_greats:
if (_h,_w) in ps:
continue
print(ans)
exit()
print(ans-1)
| 0 | null | 57,536,406,174,880 | 254 | 89 |
from collections import defaultdict,Counter
h,w,m = map(int,input().split())
w_list = []
h_list = []
se = set()
for _ in range(m):
nh,nw = map(int,input().split())
se.add((nh,nw))
w_list.append(nw)
h_list.append(nh)
w_count = Counter(w_list)
h_count = Counter(h_list)
max_w = 0
max_h = 0
ch = []
cw = []
for i in w_count.values():
max_w = max(max_w,i)
cw.append(i)
for i in h_count.values():
max_h = max(max_h,i)
ch.append(i)
ma = ch.count(max_h)*cw.count(max_w)
result = max_h+max_w
point = 0
for i,j in se:
if w_count[j] == max_w and h_count[i] == max_h:
point += 1
if point == ma:
print(result-1)
else:
print(result)
|
n, m = [int(el) for el in input().split(' ')]
c = [int(el) for el in input().split(' ')]
t = [0] + [float('inf') for _ in range(n)]
for i in range(m):
for j in range(c[i], n+1):
t[j] = min(t[j], t[j-c[i]] + 1)
print(t[n])
| 0 | null | 2,464,259,670,182 | 89 | 28 |
m = []
a = 0
for i in range(input()+1):
if i == 0:
continue
if i % 3 == 0:
m.append(str(i))
else:
a = i
while True:
if a % 10 == 3:
m.append(str(i))
break
a /= 10
if a == 0:
break
print " " + " ".join(m)
|
n = int(input())
L = []
for x in range(3,n+1):
if x % 3 == 0:
L.append(x)
else:
i = x
while i != 0:
if i % 10 == 3:
L.append(x)
break
else : i /= 10
print "",
while L != []:
print L.pop(0),
| 1 | 923,011,639,330 | null | 52 | 52 |
n = int(input())
title = []
length = []
for i in range(n):
a, b = input().split()
title.append(a)
length.append(int(b))
i = title.index(input())
print(sum((length[i+1:])))
|
def mi():
return map(int, input().split())
def main():
N, K = mi()
R, S, P = mi()
T = input()
pt = 0
my_choices = ['']*N
for i in range(K):
tmp = T[i::K]
for j in range(len(tmp)):
if j == 0:
if tmp[j] == 'r':
my_choices[i] = 'p'
pt += P
if tmp[j] == 's':
my_choices[i] = 'r'
pt += R
if tmp[j] == 'p':
my_choices[i] = 's'
pt += S
else:
if tmp[j] == 'r' and my_choices[i+(j-1)*K] != 'p':
my_choices[i+j*K] = 'p'
pt += P
if tmp[j] == 's' and my_choices[i+(j-1)*K] != 'r':
my_choices[i+j*K] = 'r'
pt += R
if tmp[j] == 'p' and my_choices[i+(j-1)*K] != 's':
my_choices[i+j*K] = 's'
pt += S
print(pt)
if __name__ == '__main__':
main()
| 0 | null | 102,219,485,195,932 | 243 | 251 |
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7
def gcd(n, m):
if m == 0:
return n
return gcd(m, n % m)
def lcm(a, b):
return a * b // gcd(a, b)
L = 1
for a in A:
L = lcm(L, a)
L %= MOD
coef = 0
for a in A:
coef += pow(a, MOD - 2, MOD)
print((L * coef) % MOD)
|
i = list(map(int, input().split()))
a = i[0]
b = i[1]
print('{0} {1} {2:.5f}'.format(a // b, a % b, float(a / b)))
| 0 | null | 44,218,067,666,388 | 235 | 45 |
line = input()
for ch in line:
n = ord(ch)
if ord('a') <= n <= ord('z'):
n -= 32
elif ord('A') <= n <= ord('Z'):
n += 32
print(chr(n), end='')
print()
|
n=raw_input()
a=''
for i in n:
if i.islower():a+=i.upper()
else:a+=i.lower()
print a
| 1 | 1,504,483,636,392 | null | 61 | 61 |
N,M=map(int,input().split())
H=list(map(int,input().split()))
H.insert(0,0)
s=set()
for i in range(M):
a,b=map(int,input().split())
if H[a]<=H[b]:s.add(a)
if H[a]>=H[b]:s.add(b)
print(N-len(s))
|
from collections import deque
N, M, K = map(int, input().split())
flst = []
blst = []
glooplst = [i for i in range(N+1)]
colorlst = [0]*(N+1)
countlst = [0]*(N+1)
anslst = [0]*(N+1)
d = deque()
for i in range(N+1):
flst.append([])
blst.append([])
for i in range(M):
a, b = map(int, input().split())
flst[a].append(b)
flst[b].append(a)
for i in range(K):
a, b = map(int, input().split())
blst[a].append(b)
blst[b].append(a)
for i in range(1, N+1):
if colorlst[i] == 0:
d.append(i)
colorlst[i] = 1
countlst[i] += 1
while d:
now = d.popleft()
for j in flst[now]:
if colorlst[j] == 0:
d.append(j)
colorlst[j] = 1
glooplst[j] = i
countlst[i] += 1
for i in range(1, N+1):
cnt = countlst[glooplst[i]] - len(flst[i]) -1
for j in blst[i]:
if glooplst[i] == glooplst[j]:
cnt -= 1
print(cnt, end=' ')
print()
| 0 | null | 43,128,700,191,740 | 155 | 209 |
mod=998244353
class Combination:
def __init__(self, N):
self.fac = [1] * (N + 1)
for i in range(1, N + 1):
self.fac[i] = (self.fac[i - 1] * i) % mod
self.invmod = [1] * (N + 1)
self.invmod[N] = pow(self.fac[N], mod - 2, mod)
for i in range(N, 0, -1):
self.invmod[i - 1] = (self.invmod[i] * i) % mod
def calc(self, n, k): # nCk
return self.fac[n] * self.invmod[k] % mod * self.invmod[n - k] % mod
N,M,K=map(int,input().split())
C=Combination(N)
ans=0
c=[1]*(N)
for i in range(1,N):
c[i]=c[i-1]*(M-1)%mod
for i in range(K+1):
tmp=M*c[N-i-1]*C.calc(N-1,N-i-1)%mod
ans=(ans+tmp)%mod
print(ans)
|
N,M,K=map(int,input().split())
MOD=998244353
ans=0
x,y=1,1
for k in range(K+1):
ans=ans+M*x*pow(y,MOD-2,MOD)*pow(M-1,N-1-k,MOD)
ans=ans%MOD
x=x*(N-k-1)
y=y*(k+1)
x=x%MOD
y=y%MOD
print(ans%MOD)
| 1 | 23,172,315,091,808 | null | 151 | 151 |
import sys
import time
import math
import itertools as it
def inpl():
return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------
N, M = inpl()
ac = [False] * (N+10)
wa = [0] * (N+10)
a = 0
w = 0
for _ in range(M):
p, S = input().split()
p = int(p)
if S == 'WA':
if not ac[p]:
wa[p] += 1
else:
if not ac[p]:
ac[p] = True
a += 1
w += wa[p]
print(a, w)
# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)
|
def resolve():
K = int(input())
ans = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(ans[K-1])
resolve()
| 0 | null | 71,586,074,511,718 | 240 | 195 |
X,Y = map(int,input().split())
M = max(X,Y)
m = min(X,Y)
mod = 10 ** 9 + 7
con = (X + Y) // 3
dif = M - m
n = (con - dif) // 2
if (X + Y) % 3 != 0 or n < 0:
print(0)
else:
def comb(n, r):
n += 1
over = 1
under = 1
for i in range(1,r + 1):
over = over * (n - i) % mod
under = under * i % mod
#powでunder ** (mod - 2) % modを実現、逆元を求めている
return over * pow(under,mod - 2,mod) % mod
ans = comb(con,n)
print(ans)
|
N = int(input())
A = [int(x) for x in input().split()]
cnt = 0
for i in range(N):
mini = i
for j in range(i, N):
if A[j] < A[mini]:
mini = j
if i != mini:
A[i], A[mini] = A[mini], A[i]
cnt +=1
print(" ".join(map(str, A)))
print(cnt)
| 0 | null | 74,974,664,614,760 | 281 | 15 |
k = int(input())
def gcd1 (a, b):
while True:
if (a < b):
a, b = b, a
c = a%b
if (c == 0):
return (b)
else:
a = b
b = c
def gcd2 (a, b, c):
tmp = gcd1(a, b)
ans = gcd1(tmp, c)
return (ans)
count = 0
for i in range(k):
for j in range(i, k):
for l in range(j, k):
tmp = gcd2(i + 1, j + 1, l + 1)
if (i == j == l):
count = count + tmp
elif (i == j or j == l):
count = count + tmp*3
else:
count = count + tmp*6
print(count)
|
a=input()
b=input()
if (a=="1" and b=="2")or(a=="2" and b=="1"):
print("3")
if (a=="1" and b=="3")or(a=="3" and b=="1"):
print("2")
if (a=="3" and b=="2")or(a=="2" and b=="3"):
print("1")
| 0 | null | 73,351,151,503,592 | 174 | 254 |
H, W = map(int, input().split())
if H >= 2 and W >= 2:
if H % 2 == 0:
print((H * W) // 2)
else:
print(((H - 1) * W) // 2 + (W + 1) // 2)
else:
print(1)
|
H,W = map(int,input().split())
if(H == 1 or W == 1):
print("1")
elif((H*W)%2 == 0):
print(str((H*W)//2))
else:
print(str((H*W)//2+1))
| 1 | 50,850,981,465,980 | null | 196 | 196 |
n = list(input())
for i in range(0, len(n)):
print("x", end = "")
|
from collections import deque
S = deque(input())
Q = int(input())
is_reversed = False
for _ in range(Q):
Query = input().split()
if Query[0] == "1":
is_reversed = not is_reversed
else:
F, C = int(Query[1]), Query[2]
if is_reversed:
F = 3 - F
if F == 1:
S.appendleft(C)
else:
S.append(C)
if is_reversed:
S.reverse()
print("".join(S))
| 0 | null | 64,945,613,409,208 | 221 | 204 |
i = str(input())
w =''
for let in i:
if(let == let.upper()):
w = w + let.lower()
elif(let == let.lower()):
w = w + let.upper()
else:
w = w + let
print(w)
|
import math
A,B,H,M=map(int,input().split())
b=30*H+30*(M/60)
c=6*M
d=math.fabs(b-c)
d=math.radians(d)
f=0
f=A**2+B**2-2*A*B*(math.cos(d))
print(math.fabs(math.sqrt(f)))
| 0 | null | 10,890,744,829,290 | 61 | 144 |
from sys import stdin
def I(): return int(stdin.readline().rstrip())
def LI(): return list(map(int,stdin.readline().rstrip().split()))
if __name__=='__main__':
mod = 10**9 + 7
n = I()
a = LI()
ans = 0
for i in range(60):
c1 = 0
for rep in a:
if (rep>>i)&1:
c1 += 1
ans += ((n-c1)*c1*(2**i)%mod)%mod
print(ans%mod)
|
def main():
r = int(input())
print(r**2)
return 0
if __name__ == '__main__':
main()
| 0 | null | 134,235,367,596,048 | 263 | 278 |
x, y = map(int, input().split())
for a in range(x+1):
for b in range(x+1-a):
if a + b == x and 2*a + 4*b == y:
print("Yes")
exit()
print("No")
|
x,y=map(int,input().split())
a=(4*x-y)/2
b=(2*x-y)/(-2)
if a>=0 and b>=0 and a.is_integer() and b.is_integer():
print("Yes")
else:
print("No")
| 1 | 13,757,026,811,428 | null | 127 | 127 |
a = input()
if 'a' <= a <= 'z':
print("a")
else:
print("A")
|
Nsum = 0
while True:
I = input()
if int(I) == 0:
break
for i in I:
Nsum += int(i)
print(Nsum)
Nsum = 0
| 0 | null | 6,395,522,390,960 | 119 | 62 |
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)
|
B=[]
for i in range(3):
A=list(map(int,input().split()))
B.append(A[0])
B.append(A[1])
B.append(A[2])
N=int(input())
for j in range(N):
b=int(input())
if b in B:
B[B.index(b)]=0
if B[0]==0 and B[1]==0 and B[2]==0:
print('Yes')
elif B[3]==0 and B[4]==0 and B[5]==0:
print('Yes')
elif B[6]==0 and B[7]==0 and B[8]==0:
print('Yes')
elif B[0]==0 and B[3]==0 and B[6]==0:
print('Yes')
elif B[1]==0 and B[4]==0 and B[7]==0:
print('Yes')
elif B[2]==0 and B[5]==0 and B[8]==0:
print('Yes')
elif B[0]==0 and B[4]==0 and B[8]==0:
print('Yes')
elif B[2]==0 and B[4]==0 and B[6]==0:
print('Yes')
else:
print('No')
| 0 | null | 29,976,046,942,820 | 19 | 207 |
A,B=map(int,input().split())
ma,mi=0,0
ma=max(A,B)
mi=min(A,B)
if A%B==0 or B%A==0:
print(ma)
else :
for i in range(2,ma+1):
if (ma*i)%mi==0:
print(ma*i)
break
i+=(mi-1)
|
import math
a,b = map(int,input().split())
print((a*b)//math.gcd(a,b))
| 1 | 113,702,632,942,166 | null | 256 | 256 |
n = input()
A = list(map(int, input().split()))
ans = 1
for a in A:
ans *= a
ans = min(ans, 10**18+1)
if ans == 10**18+1:
ans = -1
print(ans)
|
n = int(input())
l = [1]*(n+1)
l[0] = 0
ans = l[1]
for i in range(2,n+1):
for j in range(i,n+1,i):
l[j] += 1
ans += (l[i]*i)
print(ans)
| 0 | null | 13,508,644,449,810 | 134 | 118 |
import sys
def gcd(m, n):
if n != 0:
return gcd(n, m % n)
else:
return m
for line in sys.stdin.readlines():
m, n = map(int, line.split())
g = gcd(m, n)
l = m * n // g # LCM
print(g, l)
|
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000
d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
N = int(input())
A = [0 for i in range(N)]
B = [0 for i in range(N)]
for i in range(N):
a,b = LI()
A[i] = a
B[i] = b
A.sort()
B.sort()
if N % 2 == 1:
min_mid = A[(N - 1) // 2]
else:
min_mid = (A[N //2 - 1] + A[N//2]) / 2
if N % 2 == 1:
max_mid = B[(N - 1) // 2]
else:
max_mid = (B[N //2 - 1] + B[N//2]) / 2
if N % 2 == 0:
print(int((max_mid - min_mid) / 0.5 + 1))
else:
print(math.ceil(max_mid) - math.floor(min_mid) + 1)
| 0 | null | 8,744,581,684,640 | 5 | 137 |
from collections import Counter
N=int(input())
A=list(map(int, input().split()))
count = Counter(A)
vals = count.values()
sum_ = 0
for v in vals:
if v >= 2: sum_ += v*(v-1)
sum_ //= 2
for k in range(N):
if count[A[k]] < 2: print(sum_)
else:print(sum_ + 1 - count[A[k]])
|
n = int(input())
lst = [int(i) for i in input().split()]
dic = {}
for i in range(n):
if lst[i] not in dic:
dic[lst[i]] = [1, 0, 0]
else:
dic[lst[i]][0] += 1
dic[lst[i]][1] = dic[lst[i]][0] * (dic[lst[i]][0] - 1) // 2
dic[lst[i]][2] = (dic[lst[i]][0] - 1) * (dic[lst[i]][0] - 2) // 2
#print(dic)
count = 0
for value in dic.values():
count += value[1]
for i in range(n):
m = lst[i]
count -= dic[m][1]
count += dic[m][2]
print(count)
count += dic[m][1]
count -= dic[m][2]
| 1 | 48,104,959,919,424 | null | 192 | 192 |
def main():
S = input()
Q = int(input())
order = [list(input().split()) for _ in range(Q)]
left_flag = 0
right_flag = 0
cnt = 0
for i in range(Q):
if order[i][0] == '1':
cnt += 1
else:
if cnt%2 == 0:
if order[i][1] == '1':
if left_flag == 1:
left = order[i][2] + left
else:
left = order[i][2]
left_flag = 1
else:
if right_flag == 1:
right = right + order[i][2]
else:
right = order[i][2]
right_flag = 1
else:
if order[i][1] == '2':
if left_flag == 1:
left = order[i][2] + left
else:
left = order[i][2]
left_flag = 1
else:
if right_flag == 1:
right = right + order[i][2]
else:
right = order[i][2]
right_flag = 1
if left_flag == 1 and right_flag == 1:
S = left + S + right
elif left_flag == 1 and right_flag == 0:
S = left + S
elif left_flag == 0 and right_flag == 1:
S = S + right
else:
S = S
if cnt%2 == 0:
return(S)
else:
S2 = S[-1]
for i in range(len(S)-2,-1,-1):
S2 = S2 + S[i]
return S2
print(main())
|
N = int(input())
ans = 0 if N else 1
print(ans)
| 0 | null | 30,202,128,568,550 | 204 | 76 |
NN=raw_input()
N=raw_input()
A = [int(i) for i in N.split()]
swap=0
for i in range(int(NN)):
for j in reversed(range(i+1,int(NN))):
if A[j] < A[j-1] :
t = A[j]
A[j] = A[j-1]
A[j-1] = t
swap+=1
print ' '.join([str(k) for k in A])
print swap
|
import random
name = input()
name_lenght = len(name)
start = random.randint(0, name_lenght - 3)
end = start + 3
print(name[start:end])
| 0 | null | 7,344,051,959,452 | 14 | 130 |
# -*- coding: utf-8 -*-
"""
A - 9x9
https://atcoder.jp/contests/abc144/tasks/abc144_a
"""
import sys
def solve(A, B):
if 1 <= A <= 9 and 1 <= B <= 9:
return A * B
return -1
def main(args):
A, B = map(int, input().split())
ans = solve(A, B)
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
|
# coding: utf-8
a, b = map(int, input().split())
ans = -1
if a < 10 and b < 10:
ans = a * b
print(ans)
| 1 | 158,151,392,869,888 | null | 286 | 286 |
import math
def getDistance( x, y, n, p ):
dxy = 0
for i in range( n ):
dxy += abs( x[i] - y[i] )**p
return dxy**(1/float( p ) )
n = int( raw_input() )
x = [ float( i ) for i in raw_input( ).split( " " ) ]
y = [ float( i ) for i in raw_input( ).split( " " ) ]
print( getDistance( x, y, n, 1 ) )
print( getDistance( x, y, n, 2 ) )
print( getDistance( x, y, n, 3 ) )
dxy = [ 0 ]*(n+1)
for i in range( n ):
dxy[i] = abs( x[i] - y[i] )
print( max( dxy ) )
|
from functools import lru_cache
import math
def main():
n=int(input())
a=list(map(int,input().split()))
x=set()
y=a[0]
pair=True
for i in a:
if pair:
p=set(prime_factorize(i))
if len(x&p)>0:
pair=False
x|=p
y=math.gcd(y, i)
if pair:
print("pairwise coprime")
elif y==1:
print("setwise coprime")
else:
print("not coprime")
# 素数リスト(エラトステネスの篩)
@lru_cache(maxsize=None)
def primes(n:int) -> list:
'''n以下の全素数をlistで返す'''
is_prime = [True] * (n + 1)
is_prime[0] = False
is_prime[1] = False
for i in range(2, int(n**0.5) + 1):
if not is_prime[i]:
continue
for j in range(i * 2, n + 1, i):
is_prime[j] = False
return [i for i in range(n + 1) if is_prime[i]]
# 素数判定(単純な素数判定なら十分早い。大量にやる場合はX in primesがよさそう)
@lru_cache(maxsize=None)
def is_prime(n: int) -> bool:
'''引数nが素数であればTrue、そうでなければFalseを返す'''
if n == 1:
return False
elif n == 2:
return True
elif n % 2 == 0:
return False
for i in range(3, int(n**0.5)+1, 2):
if n % i == 0:
return False
return True
# 素因数分解
def prime_factorize(n: int) -> list:
'''引数nの素因数分解結果のlistを返す。'''
arr = []
# 2で割り続け奇数まで還元する
while n % 2 == 0:
arr.append(2)
n //= 2
# sqrt(n)までの素数で試し割
for f in primes(int(n**0.5)):
while n % f == 0:
arr.append(f)
n //= f
if n != 1:
arr.append(n)
return arr
if __name__ == "__main__":
main()
| 0 | null | 2,133,509,686,198 | 32 | 85 |
import math
A,B,H,M=map(int,input().split())
s=float(max(6*M,30*H+1/2*M)-min(6*M,30*H+1/2*M))/180*math.pi
print(float(math.sqrt(A**2+B**2-2*A*B*math.cos(s))))
|
from collections import Counter
import sys
import math
sys.setrecursionlimit(10 ** 6)
mod = 1000000007
inf = int(1e18)
def main():
a, b, h, m = map(int, input().split())
x1 = a * math.cos(2 * math.pi * (h % 12 / 12 + m / 60 / 12))
y1 = a * math.sin(2 * math.pi * (h % 12 / 12 + m / 60 / 12))
x2 = b * math.cos(2 * math.pi * (m / 60))
y2 = b * math.sin(2 * math.pi * (m / 60))
print(((x1 - x2)**2 + (y1 - y2)**2)**(1/2))
main()
| 1 | 20,121,927,315,168 | null | 144 | 144 |
N=int(input())
P=[int(x) for x in input().split()]
minP=P[0]
cnt=0
for p in P:
if p<=minP:
cnt+=1
minP=min(minP, p)
print(cnt)
|
n=int(input())
p=list(map(int,input().split()))
res=p[0]
cnt=0
for i in p:
if res>=i:
cnt+=1
res=min(res,i)
print(cnt)
| 1 | 85,335,510,866,950 | null | 233 | 233 |
N, K = map(int, input().split())
H = list(map(int, input().split()))
H.sort()
count = 0
for i in range(0, N-K):
count += H[i]
print(count)
|
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort(reverse=True)
s=sum(l[k:])
print(s)
| 1 | 79,219,951,047,872 | null | 227 | 227 |
n=int(input())
st=[]
for i in range(n):
s,t=input().split()
st.append((s,t))
x=input()
flg=False
ans=0
for i in range(n):
if flg:
ans+=int(st[i][1])
if st[i][0]==x:
flg=True
print(ans)
|
r = int(input())
R = int(r*r)
print(R)
| 0 | null | 121,058,521,833,010 | 243 | 278 |
a,b,c,d = map(int, input().split())
m = a*c
n = a*d
o = b*c
p = b*d
if(m>n and m>o and m>p):
print(m)
elif (n>m and n>o and n>p):
print(n)
elif (o> m and o>n and o>p):
print(o)
else:
print(p)
|
# This code is generated by [Atcoder_base64](https://github.com/kyomukyomupurin/AtCoder_base64)
# Original source code :
"""
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using int64 = long long;
#define all($) begin($), end($)
#define rall($) rbegin($), rend($)
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int64 a, b, c, d; cin >> a >> b >> c >> d;
cout << max({a * c, a * d, b * c, b * d}) << endl;
return 0;
}
"""
import base64
import subprocess
import zlib
exe_bin = "c$~die{5UD9Y5P~;<Sn5h89B0R<ER5QWO)nX>iwd#7^8kZ%*2#4(k~0^~HATM;$w|pTX(a<|zfu3t<srsA$@Rs5F7JA0Qz#ZAeQS1lz>4^p9;!FsV#{5(Fbt`O&d8-uLd^+3%g-8w^dm$oby)bKmdx?!Nc?`@R{D?zh`)h$jd70;1eHjUQn6!j`HCU;)$&|38IpK#jmR=;TCt#b%_gYCW1>z^?Hky#X1iO-G%`I$dcoQiQx!J<`@xtA?gpy$1R?tGB~x)Uz|a64NU&c}@}2<COQWf&F3(7wHf((gsEky{;xm{-O06FH$SL!=TrQK7R5rI?C#^^2=G)UQYQq+?-a%`}=NACwkIKrcmrD2KsyY`+WJVZzpY&hshoo8Kd%Kyt%7L`9_A3@|8y}w|?-M*tzoUC$>f3yYR#l6TjR^WZkGXZeY=F15G!xvGHunVE*AIZn7>t1;2I^{I?ePC{(n~1Rt}I58KS~J`4OU3p*|g`K^f0Kx2|E<UJPhdoA!sEZXA*`x{UP8t`aZ9yZ|fNW)KT(C`jE5nvwV)kOTJo5<MrBaBZ@W-~ai$~hHdgd?$GoJi)9CzZUK%*BQW)7eZiCXc6+TA6vt?MKwUWG0c0#E+<ce>^4Uu$q$<H6MwG!=r%h8kh4*9B1=tE-6n&VmQ`|!$<ltRoxX=GLRWLq6U;~9)oDIFOpHzA%FNTif8xWVXS2G$($M~5`)<o78Lxu^V69)zFSdK(3ymi4Uba^#or6M$s#l-&XkqWI&E6rpjI7pC7vy)#8GkXBXCdr6pp7(;R#tu0}wCDSXProMP<x{l9AKOY48Omip-&tGW6^~G!h!bJAHlC+)m$aEr0YV-o<e(@7qHL$qs9+f&QE{&-V=n{UgZ+_zs&kk9y&^QBhjSPMc>~VYAXqw7$;9oyglzZ68s)A9N9WWhP(#X1ATx`#mPl_Y=N9BDSAR3HVa8MvDTzz|JF=$B1)&sZFDe2(cb%=_MWBtivzq@O3(TS%<IJ;j22lMTcL};jKEHuOD)pxOF&RKLl^n;b)nhCRE#FNqSU=>o>c{b$Ejge^Q6*&&i+baQ!*4pu?MV_^)-iONUD{Z@Hvd=TDlvNV<1HwO5v;nde-K)$+>jolsQS-Ut8Nwhlm!@F`NXa;XAo`wqgBF0L$5ejDLQ*H#uNznSo42v?q@d^_Pu7grvo{5ryuuC2^d-cESZrIixpKWy}(iFsXnkNPVKX|`c|7h%goosxfHxd4*V+`i*3=tlVs=>=z?S?lle_i2gR>5|GXEI+^)4`IH=m=40IgqdyVlx9ikY!uMhdx+8nsqJTfgx`7ieIS)%onBIB7jVCq_FRzWD4=%sN^>EQj&=s_aY*GQX)ge$JyP4j7o?dB9niFM&<$y?3y=%aZ13{ZYfvB5`yx>P0;y0+NV{5SrwUFOgQJVfOKY@KkC9HnFfCsq{OrCB#M0cR&6MmuLrb4?tvm>&6Z8JJBjt<7rSe~;nb%ei#iDc0J7H|3xz@|XTXXxK1*yuWXPjR2Zrj!&3jG<FF=@_u5va81id24Gy&k5-cbwXkXgyZBG~sL8dWQP**dmz)H6HXIango7NX>MwfxdKp4V<tlonO9HvOO<dTvNfD&cn=L3o&SAue9$e7=Ki_S(@2bX@J}qX-^Audk3g5zkRh*S(cy+&pXQ?XCvw*qIY?5g7$}qA6uCK35^eaIe1&7{HNep5PY$HznylqX9VbxVEJmK{C>3ib-D)5Uv(_M^Dl7ly>F{t|I55R(ej7U^7}*OcY>9TuSheCHfhga3U3mJfAv`Kj^MH2@gQEDueJa3B3n~z714cli(~HqTYJw!754-EJapt~Vjt*>gokli2AT$X2he+<2MsWCZvl<kwEGoFw$r1?R`l4mv^2TqZA~76lRLEw^ls9vYjb&oI+=VIw1Ya%?b+|{_+s1oyIm!8>*l>Xc6Dy!`s7Yp0y+JD(E&fwz9QhO1Q>Fku)nd+?Fz1^WkuE}A1V8i+jHElX%kxy!EXxMnIu5S?fI@f<nDOD5q5j$8ba=_`<;@z_r6BS9hhkvb`Rv-fuOrL=<W)+y&wnjA+SZxqF3N|7VKDF7XKfK9VYyI<L8<FU8AQvScac9z09fnj5)*XUC(%azVc@pKbJQ%%Fk2&p5kNl-dZI~D8JwHGh=`qD*UWiVCDR*JZCd>i;g8pn~*W_v$(|06FvryGs@>3KYOlY^xI8FJk@Ayx0lt=&vah@r;$<apB1ftHU_OR-o@-y7|+|a%J|oqBmTj7{TcheOx1U!kS16M?qc*lMjvMMM~pth==1tT_X!feM;#a({G7LIY`l<B3*K9NyM4Vqy9$)^f5RW}_4ajZ=*O|?0Ivs37M%mU9W%upNHxV9s`Jx`JFD~3h&NW}uMuymj;j%ORp)~dZ?4WeBfhRWKaKeM>N+*zE$EOb-inq?aW}eTinmqQy%G1Ic~g8tHEv+U+tEv=_%+pi#E5UK?jJ^clj;0*plhq^-H5~L_L$+d_oflQz78YHz5)HcBI4H|vWDH&{nf|t&r+QZG~Mp4>XX!OVJFD!^sY1FgAAV%@Y@)^AmC|MZ&9fCYcyZ)&&QY@B#g^XseJvoShfFmRK6X(+ai{j%fC$j*Uyu`Q9JeH`vLubO<nt4HVqfsxy458)XxV$eJ5UrJi@r&X(#cD`gszdaf#YI=ga$mMLZ;Hf&b7V&O@+v-MU+4_%AH%*rA;p?AKNIO`e(@M81BVw%N_?Wb9hK{9xsGvTh6cmnmMq&xBcfM1LN(o5yp=i_2r;X9pdZ4peG4;suNLJZjhE#d?2iA%Bte*TejiWcaIg^ElO2CSPLme7kRTn8%}TbePv0a+upcY=NJ$u!BPQP%fJo^j7k%-mDTEJ14~BwKjPr$Y9MNJlB<vFnV60@7jH04=PR*-@i3wjE)q-JIoy2hx^vWZQ6IpyRogJ6cIH6{9c`O%-D4w01N+;E1!govmo`AU!2^1+<&gJB3oXn`XX*7|OCzCi)n4Fvj6&;63Qw*XQ?;i~ghjDmhh{WpffsrvBmKZ1vjUqhsm670ZWDv>>$_XPtgHu&M9NfSENH~UL!B8{|R+9-?l@X2{q;a$iF65I5%P8EyfH8LUkwH0wf)G1nWh_AU$%Bew9f^Dvr)02{G(_%%>VzO)t(2IE4G(g!^8jI8E#NlpS?j=Kox|{WKCe?ViAjnT(fDFKU!WuUpCh(4Su*Xa!B$g?wTPwYnED8@MHJXHf@`b{`SR0~syq%<&1p2nb4mtGP9a|=t0sNHP^3qdPcrUgrr;YdDCtCxl0cM8$@vuWC8jfAPorv1E4eF~%PZN8fx%FgOQvNa$Z}I@l~f5XBw63dEM#C_z<=s1AM6@rA^4IhwkT7HT#eIIwe_TF@I2HYPbzW9P<0qy<b$1I5|*&p{`6la@?IocC*N1CzQgfk6ZjjEF*YfF=kf~j;<el_==1wOr=1_6PvQjc!yAq3i{God7|yT#;<b7NX;&_;&)*ZAmRN_$Gr*?P=kHB&|I>fl;_nVl&j}6_{dXK`Z&0q!-zS`Uh4u@d8__AGy=ghm-!Yt?K(+SUQGNeUBh9B=pTBoF<?n3Kf4u)^n0|=y{9VK;f9DhZYfbbYK>BZdwKxZ-JRZQ4IR0fppTDy>EeY}-fj%VY^Y<91qQ6`M{ehs*?~j~5CcIBQMi!kD^!dG!(`G?l?Ee!?pWjvZ{aCywi~5{Cg^cUp>U*<Q{l60Q`MZ)+uQ1_7fBpt(-$!_ysP_F?<3)do{{I8gzN2t`{w^uDn%5`lzlx0CjB4Na8NfQ$DCqHaokd#z#W+`qecUO1ME!3H`qlfm$5@Z3&(kWyxPJAX&lCrQ`8OcwzsKqq13Fy4TfoiYm1c3(0j9so97oQX9#p^nn{{p1r(U-3=Y>LX|J-1qzrX^YYXUCn{~Kc+v<U"
open("./kyomu", 'wb').write(zlib.decompress(base64.b85decode(exe_bin)))
subprocess.run(["chmod +x ./kyomu"], shell=True)
subprocess.run(["./kyomu"], shell=True)
| 1 | 3,096,429,337,400 | null | 77 | 77 |
num_list = input().split()
i = 0
while True:
if int(num_list[i]) == 0:
print(i + 1)
break
i += 1
|
def solve(n):
if n % 2 == 1:
return 0
div_2 = 0
cur = 2
while cur <= n:
div_2 += (n // cur)
cur = cur * 2
div_5 = 0
cur = 5
while cur <= n:
div_5 += (n // cur) // 2
cur = cur * 5
return min(div_2, div_5)
n = int(input())
print(solve(n))
| 0 | null | 64,465,226,561,790 | 126 | 258 |
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
# 値がiであるようなA_iの個数を格納する
val_count = [0] * (10 ** 5 + 1)
total_A = 0
for a in A:
total_A += a
val_count[a] += 1
S = []
for i in range(Q):
B, C = map(int, input().split())
B_count = val_count[B]
total_A -= B * B_count
total_A += C * B_count
S.append(total_A)
val_count[B] = 0
val_count[C] += B_count
for s in S:
print(s)
|
a,b,n=map(int,input().split())
if n<b:
c=(a*n)//b
print(c)
else:
c=(a*(b-1))//b
print(c)
| 0 | null | 20,306,751,992,828 | 122 | 161 |
import math
a,b,c = [float(s) for s in input().split()]
r = c * math.pi / 180
h = math.sin(r) * b
s = a * h / 2
x1 = a
y1 = 0
if c == 90:
x2 = 0
else:
x2 = math.cos(r) * b
y2 = h
d = math.sqrt((math.fabs(x1 - x2) ** 2) + (math.fabs(y1 - y2) ** 2))
l = a + b + d
print(s)
print(l)
print(h)
|
def abc156c_rally():
n = int(input())
x = list(map(int, input().split()))
min_x, max_x = min(x), max(x)
best = float('inf')
for i in range(min_x, max_x + 1):
total = 0
for v in x:
total += (v - i) * (v - i)
if best > total:
best = total
print(best)
abc156c_rally()
| 0 | null | 32,797,550,394,104 | 30 | 213 |
S, T = map(str,input().split())
A, B = map(int,input().split())
U = str(input())
if S == U:
A -= 1
l = [A,B]
print(' '.join(map(str,l)))
elif T == U:
B -= 1
l = [A, B]
print(' '.join(map(str, l)))
|
num_l = set(map(int, input().split()))
print('Yes' if len(num_l) == 2 else 'No')
| 0 | null | 70,159,117,573,692 | 220 | 216 |
s = len(input())
print('x'*s)
|
n = int(input())
d = [[0]*9 for _ in range(9)]
ans = 0
for num in range(1, n+1):
s = str(num)
x, y = int(s[0]), int(s[-1])
if x != 0 and y != 0:
d[x-1][y-1] += 1
for i in range(9):
k = -1
for j in range(9):
if i == j:
ans += pow(d[i][j], 2)
k = 1
elif k == 1:
ans += 2*d[i][j]*d[j][i]
print(ans)
| 0 | null | 79,455,765,133,794 | 221 | 234 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)
# mod = 10 ** 9 + 7
mod = 998244353
def read_values():
return map(int, input().split())
def read_index():
return map(lambda x: int(x) - 1, input().split())
def read_list():
return list(read_values())
def read_lists(N):
return [read_list() for n in range(N)]
class V:
def __init__(self, f, v=None):
self.f = f
self.v = v
def __str__(self):
return str(self.v)
def ud(self, n):
if n is None:
return
if self.v is None:
self.v = n
return
self.v = self.f(self.v, n)
def main():
N, P = read_values()
S = input().strip()
if P in (2, 5):
res = 0
for i, s in enumerate(S):
if int(s) % P == 0:
res += i + 1
print(res)
else:
r = 0
D = {0: 1}
res = 0
d = 1
for i, s in enumerate(S[::-1]):
r += int(s) * d
r %= P
d *= 10
d %= P
c = D.setdefault(r, 0)
res += c
D[r] += 1
print(res)
if __name__ == "__main__":
main()
|
from collections import Counter
N, P = map(int, input().split())
S = input()
sum = 0
if P == 2 or P == 5:
for i in range(N):
if int(S[i]) % P == 0:
sum += i+1
print(sum)
exit()
m = []
t = 1
q = 0
for i in range(N,0,-1):
q = ((int(S[i-1]) % P) * t + q) % P
m.append(q)
t = (10 * t) % P
mc = Counter(m)
for v in mc.values():
if v > 1:
sum += v*(v-1)//2
sum += mc[0]
print(sum)
| 1 | 57,854,827,266,360 | null | 205 | 205 |
s=raw_input()
target=raw_input()
ring=s+s[0:len(target)]
f=0
for i in range(len(s)):
if ring[i:i+len(target)]==target:
f=1
break
if f==1:
print "Yes"
else:
print "No"
|
s=input()*3
if s.find(input()) == -1 :
print('No')
else:
print('Yes')
| 1 | 1,721,291,900,480 | null | 64 | 64 |
from collections import deque
import sys
input = sys.stdin.readline
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N):
ls = list(map(int, input().split()))
u = ls[0] - 1
for v in ls[2:]:
G[u].append(v - 1)
time = 1
d = [None for _ in range(N)]
f = [None for _ in range(N)]
def dfs(v):
global time
d[v] = time
time += 1
for u in range(N):
if u in G[v]:
if d[u] is None:
dfs(u)
f[v] = time
time += 1
for v in range(N):
if d[v] is None:
dfs(v)
for v in range(N):
print("{} {} {}".format(v + 1, d[v], f[v]))
|
N, M = map(int, input().split())
S = input()
if N <= M:
print(N)
exit()
if '1'*M in S:
print(-1)
exit()
from collections import deque
count = deque([])
for k in range(M+1):
if S[k] == '1':
count.append(-1)
else:
count.append(k)
sgn = deque([])
k = 0
while k < N:
k += 1
if S[k] == '0':
sgn.append(M)
else:
d = 0
while k < N:
if S[k] == '1':
k += 1
d += 1
continue
else:
break
while d > 0:
sgn.append(M-d)
d -= 1
sgn.append(M)
now = M
while now < N:
now += 1
a = sgn.popleft()
if S[now] == '1':
count.append(-1)
else:
count.append(a)
count = list(count)
c = 0
ans = ''
while c < N:
ans = str(count[-1-c]) + ' ' + ans
c += count[-1-c]
print(ans)
| 0 | null | 69,245,620,050,346 | 8 | 274 |
n, k = map(int, input().split())
a = set()
for i in range(k):
d = input()
a |= set(map(int, input().split()))
print(n - len(a))
|
n,k = map(int,input().split())
t = [1]*n
for i in range(k):
input()
for j in map(int,input().split()):
t[j-1] = 0
print(sum(t))
| 1 | 24,625,625,367,200 | null | 154 | 154 |
d = {}
m = [[False]*3 for _ in range(3)]
for i in range(3):
for j, val in enumerate(list(map(int,input().split()))):
d[val] = (i,j)
N = int(input())
for _ in range(N):
key = int(input())
if key in d:
i,j = d[key]
m[i][j] = True
def main():
if m[0][0] and m[1][1] and m[2][2]:
return True
if m[0][2] and m[1][1] and m[2][0]:
return True
for i in range(3):
if m[i][0] and m[i][1] and m[i][2]:
return True
if m[0][i] and m[1][i] and m[2][i]:
return True
return False
ans = main()
if ans:
print("Yes")
else:
print("No")
|
A = [list(map(int, input().split())) for i in range(3)]
N = int(input())
for k in range(N):
B = int(input())
for l in range(3):
for m in range(3):
if A[l][m] == B:
A[l][m] = 0
if (A[0][0] == A[0][1] == A[0][2] == 0) or (A[1][0] == A[1][1] == A[1][2] == 0) or (A[2][0] == A[2][1] == A[2][2] == 0):
print ("Yes")
elif (A[0][0] == A[1][0] == A[2][0] == 0) or (A[0][1] == A[1][1] == A[2][1] == 0) or (A[0][2] == A[1][2] == A[2][2] == 0):
print ("Yes")
elif (A[0][0] == A[1][1] == A[2][2] == 0) or (A[0][2] == A[1][1] == A[2][0] == 0):
print ("Yes")
else:
print ("No")
| 1 | 59,573,359,338,610 | null | 207 | 207 |
m1, d1 = [int(_) for _ in input().split()]
m2, d2 = [int(_) for _ in input().split()]
print(1 if m1 != m2 else 0)
|
M,D=map(int,input().split())
MM,DD=map(int,input().split())
if D>DD:
print(1)
else:
print(0)
| 1 | 123,949,201,241,108 | null | 264 | 264 |
s = "1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51"
t = s.split(", ")
k = int(input())
print(t[k-1])
|
import sys
def main():
input = sys.stdin.buffer.readline
k = int(input())
a = [
1,
1,
1,
2,
1,
2,
1,
5,
2,
2,
1,
5,
1,
2,
1,
14,
1,
5,
1,
5,
2,
2,
1,
15,
2,
2,
5,
4,
1,
4,
1,
51,
]
print(a[k - 1])
if __name__ == "__main__":
main()
| 1 | 50,081,493,734,544 | null | 195 | 195 |
n = int(raw_input())
numbers = map(int, raw_input().split())
print min(numbers), max(numbers), sum(numbers)
|
l,r,d = map(int, input().split())
var=l//d
var1=r//d
ans=var1-var
if l%d==0:
ans+=1
print(ans)
| 0 | null | 4,121,275,318,244 | 48 | 104 |
num = int(input())
print('ACL'*num)
|
K = int(input())
a = "ACL"
ans = ""
for i in range(K):
ans += a
print(ans)
| 1 | 2,190,443,196,760 | null | 69 | 69 |
while True:
s = raw_input()
if(s == "-"):
break
m = input()
for i in range(m):
h = input()
temp = ""
for j in range(h, len(s)):
temp += s[j]
for j in range(h):
temp += s[j]
s = temp
print(s)
|
def warshall_floyd(edge):
e=edge
for k in range(len(e)):
for i in range(len(e)):
for j in range(len(e)):
e[i][j]=min(e[i][j],e[i][k]+e[k][j])
return e
n,m,l=map(int,input().split())
edge=[n*[10**18]for _ in range(n)]
for i in range(n):
edge[i][i]=0
for i in range(m):
a,b,c=map(int,input().split())
a-=1
b-=1
edge[a][b]=edge[b][a]=c
edge=warshall_floyd(edge)
edge2=[n*[10**18]for _ in range(n)]
for i in range(n):
edge2[i][i]=0
for j in range(n):
if edge[i][j]<=l:
edge2[i][j]=1
edge2=warshall_floyd(edge2)
q=int(input())
for i in range(q):
s,t=map(int,input().split())
s-=1
t-=1
ans=edge2[s][t]
if ans==10**18:
print(-1)
else:
print(ans-1)
| 0 | null | 87,618,434,529,860 | 66 | 295 |
from collections import Counter
import math
n = int(input())
ab = []
mod = 10**9+7
num_ab0 = 0
num_a0 = 0
num_b0 = 0
for i in range(n):
A, B = map(int, input().split())
if A == 0 and B == 0:
num_ab0 += 1
elif A == 0:
num_a0 += 1
elif B == 0:
num_b0 += 1
else:
g = math.gcd(A, B)
if A < 0:
A = -A
B = -B
ab.append((A//g, B//g))
c = Counter(ab)
total = 2 ** num_a0 + 2 ** num_b0 - 1
for k, v in c.items():
if k[1] < 0:
num = c.get((-k[1], k[0]))
else:
num = c.get((k[1], -k[0]))
if num:
if k[1] > 0:
total *= (2 ** v + 2 ** num - 1)
else:
total *= 2 ** v
print((total - 1 + num_ab0) % mod)
|
n = input().split()
a = int(n[0])
b = int(n[1])
if a < b:
print("a < b")
elif a > b:
print("a > b")
elif a == b:
print("a == b")
| 0 | null | 10,615,280,381,850 | 146 | 38 |
import numpy as np
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
n, t = map(int, input().split())
dp = np.zeros(t,dtype=int)
food = []
for _ in range(n):
a, b = map(int, input().split())
food.append([a, b])
food.sort(key=lambda x: x[0]*-1)
for j in range(n):
a, b = food[j][0], food[j][1]
a=min(a,t)
dptmp=np.zeros(t,dtype=int)
dptmp[a:] = np.maximum(dp[a:],dp[:-a]+b)
dptmp[:a] = np.maximum(np.full(a,b,dtype=int), dp[:a])
dp=dptmp
print(dp[-1])
if __name__ == '__main__':
main()
|
INF = 10 ** 7
N,T = map(int,input().split())
foods = []
for _ in range(N):
foods.append(list(map(int,input().split())))
foods.sort()
dp = [[-INF for i in range(T)] for j in range(N+1)]
dp[0][0] = 0
ans = 0
for i in range(N):
food = foods[i]
for j in range(T):
ans = max(ans, dp[i][j] + food[1])
if dp[i][j] != INF:
dp[i+1][j] = max(dp[i+1][j], dp[i][j])
if j + food[0] < T:
dp[i+1][j + food[0]] = max(dp[i+1][j + food[0]], dp[i][j] + food[1])
print(ans)
| 1 | 152,186,036,888,762 | null | 282 | 282 |
a,b,k = map(int, input().split())
m = k if a > k else a
a -= m
b -= min(k-m, b)
print(f'{a} {b}')
|
A, B, K = map(int, input().split())
if A >= K:
A = A - K
else:
B = B - (K - A)
A = 0
if B < 0:
B = 0
print(str(A) + " " + str(B))
| 1 | 104,409,776,432,640 | null | 249 | 249 |
import sys
def I(): return int(sys.stdin.readline().rstrip())
N = I()
for i in range(50000):
if i*27//25 == N:
print(i)
break
else:
print(':(')
|
from collections import Counter
N=int(input())
Alist=list(map(int,input().split()))
count=Counter(Alist)
flag=True
for value in count.values():
if value>1:
flag=False
print('YES' if flag else 'NO')
| 0 | null | 99,688,513,297,650 | 265 | 222 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
from fractions import gcd
n, *a = map(int, read().split())
memo = 1
cnt = 0
mod = 10 ** 9 + 7
for aa in a:
q = memo * aa // gcd(memo, aa)
cnt *= q // memo
memo = q
cnt += q // aa
print(cnt % mod)
|
n,a,b = map(int,input().split())
if (a - b) % 2 == 0:
print(min(abs(a-b) // 2, max(a-1, b-1), max(n-a, n-b)))
else:
print(min(max(a-1, b-1), max(n-a, n-b), (a+b-1)//2, (2 * n - a - b + 1)//2))
| 0 | null | 98,241,673,178,662 | 235 | 253 |
h,n= map(int, input().split())
p = [list(map(int, input().split())) for _ in range(n)]
m = 10**4
dp = [0]*(h+m+1)
for i in range(m+1,h+m+1):
dp[i] = min(dp[i-a] + b for a,b in p)
print(dp[h+m])
|
import sys
input = sys.stdin.readline
H,N = map(int,input().split())
spells = [list(map(int,input().split())) for i in range(N)]
INF = 10**10
dp = [INF]*(H+1)
dp[0] = 0
for use in spells:
damage = use[0]
mp = use[1]
for i in range(1,H+1):
dp[i] = min(dp[max(0,i-damage)] + mp, dp[i])
print(dp[-1])
| 1 | 80,993,698,500,682 | null | 229 | 229 |
d,t,s=list(map(int,input().split()))
time=d/s
if(time<t or time==t):
print("Yes")
else:
print("No")
|
def main():
d, t, s = (int(i) for i in input().split())
if t*s >= d:
print("Yes")
else:
print("No")
if __name__ == '__main__':
main()
| 1 | 3,565,570,886,788 | null | 81 | 81 |
N,P=map(int,input().split());S,a,i,j,c=input(),0,0,1,[1]+[0]*P
if 10%P:
for v in S[::-1]:i,j=(i+int(v)*j)%P,j*10%P;a+=c[i];c[i]+=1
else:
for v in S:
i+=1
if int(v)%P<1:a+=i
print(a)
|
S = input()
S = str.swapcase(S)
print(S)
| 0 | null | 29,705,929,373,142 | 205 | 61 |
S = input()
print(S.replace('?', 'D'))
|
t = input()
s = t.replace("?", "D")
print(s)
| 1 | 18,397,919,311,210 | null | 140 | 140 |
# ========= /\ /| |====/|
# | / \ | | / |
# | /____\ | | / |
# | / \ | | / |
# ========= / \ ===== |/====|
# code
def main():
n , m = map(int , input().split())
if n % 2 == 0:
n -= 1
k = (n + 1) // 2
j = 1
while j < k and m > 0:
print(j , k)
j += 1
k -= 1
m -= 1
j = (n + 1) // 2 + 1
k = n
while j < k and m > 0:
if k <= n:
print(j , k)
m -= 1
j += 1
k -= 1
return
if __name__ == "__main__":
main()
|
N, M = map(int, input().split())
if M%2 != 0:
center = M + 1
else:
center = M
c = center//2
d = (center + 1 + 2*M + 1) // 2
i = 0
while i < c:
print(c-i, c+1+i)
i += 1
j = 0
while i < M:
print(d-(j+1), d+(j+1))
i += 1
j += 1
| 1 | 28,547,537,263,420 | null | 162 | 162 |
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()
|
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 | 104,118,291,002,660 | 166 | 297 |
# 逆元を利用した組み合わせの計算
#############################################################
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
NN = 10**6
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, NN + 1):
g1.append((g1[-1] * i) % mod)
inverse.append((-inverse[mod % i] * (mod // i)) % mod)
g2.append((g2[-1] * inverse[-1]) % mod)
#############################################################
N, K = map(int, input().split())
# M = 空き部屋数の最大値
M = min(N - 1, K)
"""
m = 0,1,2,...,M
N部屋から、空き部屋をm個選ぶ
-> N_C_m
残りの(N-m)部屋に、N人を配置する(空き部屋が出来てはいけない)
まず、(N-m)部屋に1人ずつ配置し、残ったm人を(N-m)部屋に配置する
これは、m個のボールと(N-m-1)本の仕切りの並び替えに相当する
-> (m+N-m-1)_C_m = N-1_C_m
"""
ans = 0
for m in range(0, M + 1):
ans += cmb(N, m, mod) * cmb(N - 1, m, mod)
ans %= mod
print(ans)
|
n, k = map(int, input().split())
mod = 10 ** 9 + 7
def comb(n, r):
if n < r:return 0
if n < 0 or k < 0:return 0
return fa[n] * fi[r] % mod * fi[n - r] % mod
fa = [1] * (n + 1)
fi = [1] * (n + 1)
for i in range(1, n + 1):
fa[i] = fa[i - 1] * i % mod
fi[i] = pow(fa[i], mod - 2, mod)
ans = 0
for i in range(min(k, n - 1) + 1):
ans += comb(n, i) * comb(n - 1, i) % mod
print(ans % mod)
| 1 | 67,509,272,381,408 | null | 215 | 215 |
n,k = map(int,input().split())
p = list(map(int,input().split()))
p.sort()
print(sum(p[:k]))
|
N, K = map(int, input().split())
p = input().split()
p = [int(s) for s in p]
p.sort()
ans = 0
for i in range(K):
ans += p[i]
print(ans)
| 1 | 11,600,235,081,060 | null | 120 | 120 |
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
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 make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
divisors.sort()
return divisors
#maincode-------------------------------------------------
n = ini()
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
ans = r*g*b
for j in range(n):
for i in range(j):
k = 2*j-i
if k < n:
if (s[i] == s[j]):
continue
if (s[i] == s[k]):
continue
if (s[j] == s[k]):
continue
ans -= 1
print(ans)
|
N = int(input())
S = input()
R = []
G = []
B = []
for i in range(N):
if S[i] == 'R':
R.append(i+1)
elif S[i] == 'G':
G.append(i+1)
elif S[i] == 'B':
B.append(i+1)
lenb = len(B)
cnt = 0
for r in R:
for g in G:
up = max(r, g)
down = min(r, g)
diff = up - down
chk = 0
if up + diff <= N:
if S[up+diff-1] == 'B':
chk += 1
if down-diff >= 1:
if S[down-diff-1] == 'B':
chk += 1
if diff%2 == 0:
if S[int(up-diff/2-1)] == 'B':
chk += 1
cnt += lenb - chk
print(cnt)
| 1 | 36,064,761,367,548 | null | 175 | 175 |
import bisect, collections, copy, heapq, itertools, math, 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())
def main():
N =I()
if N % 2 == 0:
print(N // 2)
else:
print(N // 2 + 1)
if __name__ == "__main__":
main()
|
import sys
class SWAG:
def __init__(self):
self.fold_l = []
self.r = []
self.fold_r = ~(1 << 60)
def push(self, a):
if self.fold_r < a:
self.fold_r = a
self.r.append(a)
def pop(self):
if not self.fold_l:
self.r.reverse()
self.fold_l = self.r
self.r = []
self.fold_r = ~(1 << 60)
fold_l = self.fold_l
for i in range(len(fold_l) - 1):
if fold_l[i + 1] < fold_l[i]:
fold_l[i + 1] = fold_l[i]
self.fold_l.pop()
def get(self):
if not self.fold_l:
return self.fold_r
elif not self.r:
return self.fold_l[-1]
else:
return max(self.fold_l[-1], self.fold_r)
n, k = map(int, input().split())
p = [*map(int, input().split())]
c = [*map(int, input().split())]
for i in range(n):
p[i] -= 1
if max(c) < 0:
print(max(c))
sys.exit()
ans = 0
used = [False] * n
for i in range(n):
if used[i]:
continue
# ループを取り出す
loop = []
at = i
while not used[at]:
loop.append(c[at])
used[at] = True
at = p[at]
siz = len(loop)
cusum = [0] + loop * 3
for i in range(siz * 3):
cusum[i + 1] += cusum[i]
sum = max(0, cusum[siz])
# k % siz 回以下での最大 -> 累積和にスライド最大値
d = k % siz
swag = SWAG()
for i in range(d):
swag.push(cusum[i])
for i in range(siz):
swag.push(cusum[d + i])
ans = max(ans, swag.get() - cusum[i] + sum * (k // siz))
swag.pop()
if k < siz:
continue
# k % siz + siz 回以下での最大
d = k % siz + siz
swag = SWAG()
for i in range(d):
swag.push(cusum[i])
for i in range(siz):
swag.push(cusum[d + i])
ans = max(ans, swag.get() - cusum[i] + sum * (k // siz - 1))
swag.pop()
print(ans)
| 0 | null | 32,031,046,386,208 | 206 | 93 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.