Harryxun/stf_run huggingface dataset:
N = int(input())
A = list(map(int,input().split()))
k = sum(A)　###syuutenの個数
c = 1  ###考えている列の〇の個数(次の列につながる個数)
aa = 1
ans = 1
if A[0] != 0:
    print(-1)
else:
    for i in range(1,N + 1):
        c *= 2 ##次の列につながる個数の最大値
        if A[i] > c:
            ###A[i]はここが終点になるということ。終点の方が多いことは認められない
            aa = 0
            break
        elif A[i] == c:
            ###終点と同数だとこれより下には繋がらない。もし最下段でなければアウト
            if i != N:
                aa = 0
                break
            else:
                ans += c
        else:
            c -= A[i]
            k -= A[i]
            ans += A[i]
            ###終点の個数を引く。ここで終点の個数だけansを足す
            if c >= k:
                ###次の列につながる個数はこれよりしたにある終点の個数を超えれない
                c = k
            ans += c
            ###この段で下につながる個数を足す

if aa == 0:
    print(-1)
else:
    print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
k = sum(A)
###syuutenの個数
c = 1  
###考えている列の〇の個数(次の列につながる個数)
aa = 1
ans = 1
if A[0] != 0:
    aa = 2
    if N != 0:
        print(-1)
    else:
        if A[0] == 1:
            print(1)
        else:
            print(-1)
else:
    for i in range(1,N + 1):
        c *= 2
        ##次の列につながる個数の最大値
        if A[i] > c:
            ###A[i]はここが終点になるということ。終点の方が多いことは認められない
            aa = 0
            break
        elif A[i] == c:
            ###終点と同数だとこれより下には繋がらない。もし最下段でなければアウト
            if i != N:
                aa = 0
                break
            else:
                ans += c
        else:
            c -= A[i]
            k -= A[i]
            ans += A[i]
            ###終点の個数を引く。ここで終点の個数だけansを足す
            if c >= k:
                ###次の列につながる個数はこれよりしたにある終点の個数を超えれない
                c = k
            ans += c
            ###この段で下につながる個数を足す

if aa == 0:
    print(-1)
elif aa == 1:
    print(ans)
for i in range(5):
  a=int(input())
  b=int(input())
  c=int(input())
  d=int(input())
  e=int(input())
  if a<0 and a<b:
    x=abs(a)*c+d+b*e
    print(x)
  elif a>0 and a<b:
    y=(b-a)*e
    print(y)
  else:
    pass


### FIXED CODE ###

a=int(input())
b=int(input())
c=int(input())
d=int(input())
e=int(input())
if a<0:
  x=-a*c+d+b*e
  print(x)
else :
  y=(b-a)*e
  print(y)
print(math.ceil(input()))

### FIXED CODE ###

import math
print(math.ceil(int(input())/2))
A, B, K = map(int, input().split())

if A >= K:
  A = A - K
  print(A, B)

elif A + B >= K:
  B = A + B - K
  print(0, B)
    
else:
  print(0,0)

### FIXED CODE ###

S, T = input().split()
print(T + S)
LenS = len(input())
LenT = len(input())
if(LenS+1==LenT):
	print("Yes")
else():
	print("No")

### FIXED CODE ###

S = input()
T = input()
if(S==T[:-1]):
  print("Yes")
else:
  print("No")
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
new=[]
cards.sort()
mod=0
for i in range(M):
    time,val=list(map(int,input().split()))
    mod+=time
    tmp=[val for _ in range(time)]
    new.append([val,time])
new.sort(key=lambda x:x[0],reverse=True)
print(sum(new))
"""
total=0
vali=0
if mod<N:
    total+=sum(cards[mod-N:])
for i in new:
    for j in range(i[1]):
        if vali==N:
            break
        total+=max(i[0],cards[vali])
        vali+=1
        
print(total)
"""

### FIXED CODE ###

from collections import Counter
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
all_c=Counter(cards)

for i in range(M):
    time,val=list(map(int,input().split()))
    try:
        all_c[val]+=time
    except:
        all_c[val]=time

tmp=list(all_c.keys())
tmp.sort(reverse=True)
total=0
sum_time=0
for i in tmp:
    if sum_time>=N:
        break
    sum_time+=all_c[i]
    if sum_time<N:
        total+=i*all_c[i]
    else:
        total+=i*(all_c[i]+N-sum_time)
print(total)
from collections import Counter
n=int(input())
x=[]
y=[]
for i in range(n):
  a,b=map(int,input().split())
  x.append(a)
  y.append(b)
cost=n
list=[]
for i in range(n):
  for j in range(n):
    if i != j:
      list.append([x[i]-x[j],y[i]-y[j]])
c=Counter(map(tuple, list))
mode=c.most_common(1)
print(cost-mode[0][1])

### FIXED CODE ###

from collections import Counter
n=int(input())
x=[]
y=[]
for i in range(n):
  a,b=map(int,input().split())
  x.append(a)
  y.append(b)
cost=n
list=[]
for i in range(n):
  for j in range(n):
    if i != j:
      list.append([x[i]-x[j],y[i]-y[j]])

if n ==1:
  print(cost)
else:
  c=Counter(map(tuple, list))
  mode=c.most_common(1)
  print(cost-mode[0][1])
from collections import defaultdict

import sys
input = sys.stdin.readline

parent_node = []

def get_parent(ind):
    global parent_node
    if ind != parent_node[ind]:
        parent_node[ind] = get_parent(parent_node[ind])
    return parent_node[ind]



def main():
    num = int(input())

    data = defaultdict(tuple)

    for i in range(num):
        x, y = map(int, input().split())
        data[i] = (x, y)

    data_sort = sorted(data.items(), key=lambda x: x[1][0])
    data_x = [ele for ele, ele2 in data_sort]
    data_sort = sorted(data.items(), key=lambda x: x[1][1])
    data_y = [ele for ele, ele2 in data_sort]

    cost_data = [0 for i in range((num - 1) * 2)]
    for i in range(len(data_x) - 1):
        a, b = data_x[i], data_x[i + 1]
        cost = abs(data[a][0] - data[b][0])
        cost_data[i] = a, b, cost
    for i in range(len(data_y) - 1):
        a, b = data_y[i], data_y[i + 1]
        cost = abs(data[a][1] - data[b][1])
        cost_data[i + len(data_x) - 1] = a, b, cost

    cost_data.sort(key=lambda x: x[2])
    global parent_node
    parent_node = [i for i in range(num)]

    ans = 0
    for i in range(len(cost_data)):
        a, b, cost = cost_data[i]
        a_parent = get_parent(a)
        b_parent = get_parent(b)
        if a_parent != b_parent:
            ans += cost
            parent_node[min(a_parent, b_parent)] = max(a_parent, b_parent)

    print(ans)





if __name__ == '__main__':
    main()


### FIXED CODE ###

from collections import defaultdict

import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline

parent_node = []

def get_parent(ind):
    global parent_node
    if ind != parent_node[ind]:
        parent_node[ind] = get_parent(parent_node[ind])
    return parent_node[ind]



def main():
    num = int(input())

    data = defaultdict(tuple)

    for i in range(num):
        x, y = map(int, input().split())
        data[i] = (x, y)

    data_sort = sorted(data.items(), key=lambda x: x[1][0])
    data_x = [ele for ele, ele2 in data_sort]
    data_sort = sorted(data.items(), key=lambda x: x[1][1])
    data_y = [ele for ele, ele2 in data_sort]

    cost_data = [0 for i in range((num - 1) * 2)]
    for i in range(len(data_x) - 1):
        a, b = data_x[i], data_x[i + 1]
        cost = abs(data[a][0] - data[b][0])
        cost_data[i] = a, b, cost
    for i in range(len(data_y) - 1):
        a, b = data_y[i], data_y[i + 1]
        cost = abs(data[a][1] - data[b][1])
        cost_data[i + len(data_x) - 1] = a, b, cost

    cost_data.sort(key=lambda x: x[2])
    global parent_node
    parent_node = [i for i in range(num)]

    ans = 0
    for i in range(len(cost_data)):
        a, b, cost = cost_data[i]
        a_parent = get_parent(a)
        b_parent = get_parent(b)
        if a_parent != b_parent:
            ans += cost
            parent_node[min(a_parent, b_parent)] = max(a_parent, b_parent)

    print(ans)





if __name__ == '__main__':
    main()
while 1:
    r,w,c,a = map(int,raw_input().split())
    if r == w == c == a == 0:
        break
    b = 0
    sum = 0
    if r % c == 0:
        if r / c >= w:
            b = 1
    while b == 0:
        sum += 1
        r += a
        if r % c == 0:
            if r/c>=w:
                b = 1
    print sum

### FIXED CODE ###

while 1:
    r,w,c,a = map(int,raw_input().split())
    if r == w == c == a == 0:
        break
    b = 0
    sum = 0
    if r / c >= w:
            b = 1
    while b == 0:
        sum += 1
        r += a
        if r/c>=w:
            b = 1
    print sum
s,t=map(sting,input())
a,b=map(int,input())
u=map(sting,input())

if s==u:
    print(a-1,b)
else
    print(a,b-1)



### FIXED CODE ###

s,t=map(str,input().split())
a,b=map(int,input().split())
u=input()

if s==u:
  print(a-1,b)
elif t==u:
  print(a,b-1)
n = int(input())
lis = [int(i) for i in input().split()]
sum = 0
for i in range(n-1):
  for j in range(i+1,n):
    sum += i*J
print(sum)

### FIXED CODE ###

n = int(input())
lis = [int(i) for i in input().split()]
sum = 0
for i in range(n-1):
  for j in range(i+1,n):
    sum += lis[i]*lis[j]
print(sum)
n, m = map(int,input().split())
X = sorted(map(int,input().split()))
l = sorted([X[i+1] - X[i] for i in range(m-1)])
print(sum(l[:-n+1]))

### FIXED CODE ###

a, b = map(int, input().split())
print("{:.10f}".format(a/b))
def solve(h,w,count,c):
    if 0 <= h < H and 0 <= w < W and A[h][w] == c:
        A[h][w] = count
        for dh,dw in zip([1,0,-1,0],[0,1,0,-1]):
            solve(h+dh,w+dw,count,c)
while 1:
    H,W = map(int,raw_input().split())
    if H == W == 0: break
    A = [list(raw_input()) for _ in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if type(A[h][w]) is not int:
                solve(h,w,count,A[h][w])
                count += 1
    print count

### FIXED CODE ###

import sys
sys.setrecursionlimit(20000)
def solve(h,w,count,c):
    if 0 <= h < H and 0 <= w < W and A[h][w] == c:
        A[h][w] = count
        for dh,dw in zip([1,0,-1,0],[0,1,0,-1]):
            solve(h+dh,w+dw,count,c)
while 1:
    H,W = map(int,raw_input().split())
    if H == W == 0: break
    A = [list(raw_input()) for _ in range(H)]
    count = 0
    for h in range(H):
        for w in range(W):
            if type(A[h][w]) is not int:
                solve(h,w,count,A[h][w])
                count += 1
    print count
# 088 B
N = int(input())
a = list(map(int, input().split()))
A = 0
B = 0
aa = sorted(a)
for i in range(0, N, 2):
    A += aa[-i-1]
    B += aa[-i-2]
print(A-B)


### FIXED CODE ###

# 088 B
N = int(input())
a = list(map(int, input().split()))
A = 0
B = 0
aa = sorted(a)
for i in range(0, N, 2):
    A += aa[-i-1]
    if (i+2)<=N:
        B += aa[-i-2]
print(A-B)
h,w=map(int, input().split())
S=["."+input()+"." for _ in range(h)]
S = ["."*(w+2)]+S+["."*(w+2)]
# print(S)
flag=True
for i in range(h):
    for j in range(w):
        if S[i][j]=="#":
            if S[i-1][j]=="." and S[i+1][j]=="." and S[i][j-1]=="." and S[i][j+1]==".":
                flag=False
print("Yes" if flag else "No")a

### FIXED CODE ###

h,w=map(int, input().split())
S=["."+input()+"." for _ in range(h)]
S = ["."*(w+2)]+S+["."*(w+2)]
# print(S)
flag=True
for i in range(h):
    for j in range(w):
        if S[i][j]=="#":
            if S[i-1][j]=="." and S[i+1][j]=="." and S[i][j-1]=="." and S[i][j+1]==".":
                flag=False
print("Yes" if flag else "No")
p = 10 ** 9 + 7 # mod
N = (10 ** 6) + 10  # N は必要分だけ用意する
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

def cmb(r, n, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

a = list(map(int,input().split()))
ans = cmb(a[2]+1,a[2]+a[3]+2,p) - cmb(a[1],a[2]+a[1]+1,p) - cmb(a[0],a[3]+a[0]+1,p) + cmb(a[0],a[0]+a[1],p)
print(ans)

### FIXED CODE ###

p = 10 ** 9 + 7 # mod
N = (10 ** 6) * 2 + 10  # N は必要分だけ用意する
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

def cmb(r, n, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

a = list(map(int,input().split()))
ans = cmb(a[2]+1,a[2]+a[3]+2,p) - cmb(a[1],a[2]+a[1]+1,p) - cmb(a[0],a[3]+a[0]+1,p) + cmb(a[0],a[0]+a[1],p)
print(ans%p)
n,k=map(int,input().split())
a=list(map(int,input().split()))
c=1
t=0
path=[]
s=set()
cycle=[]
while(c<=k):
    c+=1
    if a[t] in s:
        #print(path.index(a[t]),a[t])
        cycle=path[path.index(a[t]):]
        k-=c-1
        break
    path.append(a[t])
    s.add(a[t])
    t=a[t]-1
    #print(path,c)
#print(cycle)
print(cycle[k%len(cycle)])
    

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
c=2
t=0
path=[]
s=set()
cycle=[]
while(c<=k):
    if a[t] in s:
        #print(path.index(a[t]),a[t])
        cycle=path[path.index(a[t]):]
        k-=c-1
        break
    path.append(a[t])
    s.add(a[t])
    t=a[t]-1
    c+=1
if cycle==[]:
    print(a[t])
else:
    print(cycle[k%len(cycle)])
import bisect
N = int(input())
A = [int(input()) for n in range(N)]

tree = [[A[0]]]
leaf = [A[0]]
for n in range(1,N):
    a = A[n]
    pos = min(n-1,bisect.bisect_left(leaf,a)-1)
    if pos==-1:
        tree = [a] + tree
        leaf = [a] + leaf
    else:
        tree[pos].append(a)
        leaf[pos] = a
print(len(tree))

### FIXED CODE ###

import bisect
from collections import deque
N = int(input())
A = [int(input()) for n in range(N)]

tree = deque([[A[0]]])
leaf = deque([A[0]])
for n in range(1,N):
    a = A[n]
    pos = min(n-1,bisect.bisect_left(leaf,a)-1)
    if pos==-1:
        tree.appendleft([a])
        leaf.appendleft(a)
    else:
        tree[pos].append(a)
        leaf[pos] = a
print(len(tree))
O, E = [input() for _ in range(2)]
E += " "

length = max(len(O), len(E))

for i in range(length):
    print(O[i], end='')
    print(E[i], end='')


### FIXED CODE ###

O, E = [input() for _ in range(2)]
E += " "

for i in range(len(O)):
    print(O[i], end='')
    print(E[i], end='')
H,W=map(int,input().split())
C=[0 for i in range(9)]
for i in range(9):
    C[i]=map(int,input().split())
A=[0 for i in range(H)]
for i in range(H):
    A[i]=map(int,input().split())

    
def mini(S,dic,ans):
    if(S=={0,1,2,3,4,5,6,7,8}):
        return ans
    min_val=min(dic.items())
    for i in dic.keys():
        if dic[i]==min_val:
            S.add(i)
            pos=i
            break
    ans[pos]=dic.pop(pos)
    for i in dic.keys():
        dic[i]=min([dic[i],dic[pos]+C[pos][0]])
    mini(S,dic,ans)

dic={}
for i in range(9):
    dic[i]=C[i][0]
M=mini({},dic,{})
M[-1]=0

ans=0
for i in range(H):
    for j in range(W):
        ans+=M[A[i][j]]
print(ans)


### FIXED CODE ###

H,W=map(int,input().split())
C=[0 for i in range(10)]
for i in range(10):
    C[i]=list(map(int,input().split()))
A=[0 for i in range(H)]
for i in range(H):
    A[i]=list(map(int,input().split()))

    
def mini(S,dic,ans):
    if(S=={0,1,2,3,4,5,6,7,8,9}):
        return ans
    min_val=min(dic.values())
    for i in dic.keys():
        if dic[i]==min_val:
            S.add(i)
            pos=i
            break
    ans[pos]=dic.pop(pos)
    for i in dic.keys():
        dic[i]=min([dic[i],ans[pos]+C[i][pos]])
    return mini(S,dic,ans)

dic={}
for i in range(10):
    dic[i]=C[i][1]
M=mini(set(),dic,{})
M[-1]=0

ans=0
for i in range(H):
    for j in range(W):
        ans+=M[A[i][j]]
print(ans)
N,A,B=map(int,input().split())

import numpy as np
blue=np.array([1]*A)
red=np.array([-1]*B)

ball=np.concatenate(([blue, red]*N))

print(np.count_nonzero(ball[0:N]==1))

### FIXED CODE ###

N,A,B=map(int,input().split())

r=N//(A+B)*A
s=N%(A+B)

x=r+min(A,s)
print(x)
import sys

n,x = map(int,input().split())

def f(n):
	if n == 0:
		return 1
	return 2 * f(n-1) + 3

def f2(n):
	if n == 0:
		return 'P'
	return 'B' + f2(n-1) + 'P' + f2(n-1) + 'B'

def p(n):
	if n == 0:
		return 1
	return 2 * p(n-1) + 1

num = f(n)

ans = 0
while True:
	if num < 10 ** 5:
		break
	tmp = num // 2 + 1

	if x > tmp:
		ans += p(n) // 2 + 1
		x -= tmp
		num -= tmp + 1
	elif x < tmp:
		num -= tmp + 1
		x -= 1
	else:
		print(p(n-1)+1)
		sys.exit()
	n -= 1

string = f2(n)

for i in range(x):
	if string[i] == 'P':
		ans += 1
print(ans)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)

n,x = map(int,input().split())

def f(n):
	if n == 0:
		return 1
	return 2 * f(n-1) + 3

def f2(n):
	if n == 0:
		return 'P'
	return 'B' + f2(n-1) + 'P' + f2(n-1) + 'B'

def p(n):
	if n == 0:
		return 1
	return 2 * p(n-1) + 1

num = f(n)

ans = 0
while True:
	if num < 10 ** 5:
		break
	tmp = num // 2 + 1

	if x > tmp:
		ans += p(n) // 2 + 1
		x -= tmp
		num -= tmp + 1
	elif x < tmp:
		num -= tmp + 1
		x -= 1
	else:
		print(p(n-1)+1)
		sys.exit()
	n -= 1

string = f2(n)
if len(string) <= x:
	print(p(n)+ans)
else:
	for i in range(x):
		if string[i] == 'P':
			ans += 1
	print(ans)
import math
sum = 0
K = int(input())
for k in range(K):
  for j in range(K):
    for i in range(K):
      sum = sum + gcd(math.gcd(k+1,j+1),i+1)
print(sum)

### FIXED CODE ###

from math import gcd
sum = 0
K = int(input())
for k in range(1,K+1):
  for j in range(1,K+1):
    t = gcd(k,j)
    for i in range(1,K+1):
      sum += gcd(t,i)
print(sum)
import bisect
import numpy as np
import scipy as sp
from sys import stdin

r, g, b, n = map(int, input().split())
count = 0

for i in range(n + 1):
    for j in range(n + 1):
        total = n - r * i - g * j
        if total < 0:
            break
        if total % b == 0:
            count += 1

print(count)


### FIXED CODE ###

import bisect
import numpy as np
import scipy as sp
from sys import stdin

n, k = map(int, input().split())
print(n - k + 1)
a = input() ,
b = input()

print a * b ,
print a * 2 + b * 2

### FIXED CODE ###


l = map(int, raw_input().split())
 
print "%d %d" % (l[0]*l[1], (l[0]+l[1])*2)
from collections import Counter
n = int(input())

sn = []
count = 0

for i in range(n):
  sn.append(sorted(Counter(list(input())).items()))


for i in range(n - 1):
  for j in range(i+1,n):
    if sn[i] == sn[j]:
      count += 1
print(count)

### FIXED CODE ###

a,b = map(int,input().split())

l = [a+b,a-b,a*b]

print(max(l))
import sys
for line in sys.stdin.readlines():
    num = map(float, line.strip().split())
    x = (num[2]*num[4] - num[1]*num[5]) / (num[0]*num[4] - num[1]*num[3])
    y = (num[5]*num[0]-num[2]*num[3]) / (num[0]*num[4]-num[3]*num[1])
    out.append( "%0.3f %0.3f"%(x,y))
for i in xrange(len(out)):
    print out[i]

### FIXED CODE ###

import sys
out=[]
for line in sys.stdin.readlines():
    num = map(float, line.strip().split())
    x = (num[2]*num[4] - num[1]*num[5]) / (num[0]*num[4] - num[1]*num[3])
    y = (num[5]*num[0] - num[2]*num[3]) / (num[0]*num[4] - num[3]*num[1])
    if x == 0:
        x=0.00
    if y == 0:
        y=0.00
    out.append( "%.3f %.3f"%(round(x,3),round(y,3)))
for i in xrange(len(out)):
    print out[i]
### 20.07.2017
x = []
a, b, c = map(int, input().split())
for i in range(a, b):
	if c % i ==0:
		x.append(i)
print(len(x))### 20.07.2017
x = []
a, b, c = map(int, input().split())
for i in range(a, b+1):
	if c % i ==0:
		x.append(i)
print(len(x))

### FIXED CODE ###

### 20.07.2017
x = []
a, b, c = map(int, input().split())
for i in range(a, b+1):
	if c % i ==0:
		x.append(i)
print(len(x))
A = int(input())
B = int(input())

print(A*B)

### FIXED CODE ###

A, B = map(int, input().split())
print(A*B)
input()
a,b=map(int,input().split())
print(int(max(0,a-2*b)))

### FIXED CODE ###

a,b = map(int,input().split())
print(max(0,a-2*b))
a, b, c = map(int, input().split())
if a[-1] == b[0] and b[-1] == c[0]:
  print('YES')
else:
  print('NO')


### FIXED CODE ###

a, b, c = input().split()
if a[-1] == b[0] and b[-1] == c[0]:
  print('YES')
else:
  print('NO')
n = int(input())
d = list(map(int,input().split()))
m = int(input())
t = list(map(int,input().split()))
if n < m:
    print('NO')
    exit(0)
dcnt = {}
for x in d:dcnt[x] = 0
ok = True
for x in d:dcnt[x]+=1
for i in range(m):
    if t[i] not in dcnt:ok = False
    if dcnt[t[i]] == 0:ok = False
    dcnt[t[i]] -= 1
if ok:print('YES')
else:print('NO')


### FIXED CODE ###

n = int(input())
d = list(map(int,input().split()))
m = int(input())
t = list(map(int,input().split()))
if n < m:
    print('NO')
    exit(0)
dcnt = {}
for x in d:dcnt[x] = 0
ok = True
for x in d:dcnt[x]+=1
for i in range(m):
    if t[i] not in dcnt:
        ok = False
        break
    if dcnt[t[i]] == 0:
        ok = False
        break
    dcnt[t[i]] -= 1
if ok:print('YES')
else:print('NO')
input=sys.stdin.readline
import math
pi=math.pi
def calcAngle(h,m): 
    if (h == 12): 
        h = 0
    if (m == 60): 
        m = 0
    hour_angle = 0.5 * (h * 60 + m) 
    minute_angle = 6 * m 
    angle = abs(hour_angle - minute_angle) 
    angle = min(360 - angle, angle) 
    return angle
A,B,H,M=map(int,input().split())
angle=calcAngle(H,M)
t= A*A + B*B - 2*A*B*math.cos(((math.pi)/180)*(angle))
print("{0:.15f}".format(t**0.5))

### FIXED CODE ###

import math
pi=math.pi
def calcAngle(h,m): 
    if (h == 12): 
        h = 0
    if (m == 60): 
        m = 0
    hour_angle = 0.5 * (h * 60 + m) 
    minute_angle = 6 * m 
    angle = abs(hour_angle - minute_angle) 
    angle = min(360 - angle, angle) 
    return angle
A,B,H,M=map(int,input().split())
angle=calcAngle(H,M)
t= A*A + B*B - 2*A*B*math.cos(((math.pi)/180)*(angle))
print("{0:.15f}".format(t**0.5))
def f(x):
  if (x == 0):
    return 1
  else if (x == 1):
    return 0

  
f(int(input())

### FIXED CODE ###

def f(x):
	if x == 0:
		return 1
	elif x == 1:
		return 0


n = float(input())
r = f(n)
if r != None:
	print(r)
isExist = [0]*2001 
n = int( input() )
d = int( input() )
for i in range( 0, 5 ):
	print( "yes" )

### FIXED CODE ###

def solve( p, t ):
	global isExist, n, A
	isExist[ t ] = True
	if p < n:
		solve( p+1, t-A[p] )
		solve( p+1, t )
	else:
		return

isExist = [False]*2001 
n = int( input() )
A = [ int( val ) for val in input().rstrip().split( ' ' ) ]
q = int( input() )
M = [ int( val ) for val in input().rstrip().split( ' ' ) ]
s = sum( A )
solve( 0, s )
for i in range( 0, q ):
	if isExist[ M[i] ]:
		print( "yes" )
	else:
		print( "no" )
from bisect import bisect_left
n=int(input())
A=sorted(list(map(int, input())))
amax=max(A)
m=bisect_left(A,amax/2)
a,b=A[m],A[m+1]
if a-amax/2<=amax-b-amax/2:
  print(amax, a)
else:
  print(amax,b)

### FIXED CODE ###


n=int(input())
A=sorted(list(map(int, input().split())))
amax=max(A)
half=amax/2

ans=0
now=amax
for a in A:
  if abs(a-half)<=now and a<amax:
    now=abs(a-half)
    ans=a
print(amax, ans)
N = int(input())
S = 0
for i in range(N):
    if i+1%3 and i+1%5 or i+1%3 or i+1%5:
        break
    else:
        global S = S+1+i
        
    


### FIXED CODE ###

N = int(input())
S = 0
for i in range(N):
    if (i+1)%3 == 0 and (i+1)%5==0 or (i+1)%3 ==0 or (1+i)%5 ==0 :
        continue
    else:
        S = S+1+i

print(S)
X,Y= map(int,input().split())
if y%2==0 and 2*X<=Y and Y<=4*X:
    print('yes')
else:
    print('No')

### FIXED CODE ###

x,y=map(int,input().split())
 
if(y%2==0 and y>=2*x and y<=4*x):
  print("Yes")
  
else:
  print("No")
def dfs(G2, s, dist):
    for v in G2[s]:
        if dist[v] > dist[s] + 1:
            dist[v] = dist[s] + 1
            dfs(G2, v, dist)

N, M = map(int,input().split())

G = [[] for _ in range(N)]
for _ in range(M):
    u, v = map(int,input().split())
    G[u-1].append(v-1)

G2 = [[] for _ in range(N)]

for u in range(N):
    for v1 in G[u]:
        for v2 in G[v1]:
            for v3 in G[v2]:
                if u != v3 and v3 not in G2[u]:
                    G2[u].append(v3)

s, t = map(int,input().split())

dist = [10**9] * len(G2)
dist[s-1] = 0

dfs(G2, s-1, dist)

if dist[t-1] == 10**9:
    print(-1)
else: 
    print(dist[t-1])

### FIXED CODE ###

from collections import deque
INF = 10**9

N, M = map(int,input().split())

G = [[] for _ in range(3*N)]
for _ in range(M):
    u, v = map(int,input().split())
    u, v = u-1, v-1

    G[u].append(v+N)
    G[u+N].append(v+2*N)
    G[u+2*N].append(v)

s, t = map(int,input().split())
s, t = s-1, t-1

dist = [INF] * (3*N)
dist[s] = 0

Q = deque([s])
while len(Q) > 0:
    u = Q.popleft()
    for v in G[u]:
        if dist[v] == INF:
            dist[v] = dist[u] + 1
            Q.append(v)

if dist[t] == INF:
    print(-1)
else: 
    print(dist[t] // 3)