Harryxun/stf_run huggingface dataset:
num_shops, target_drinks = map(int, input().split())
sell = {}

for _ in range(num_shops):
    a,b = map(int, input().split())

    try:
        sell[a] += b
    except:
        sell[a] = b

ans, bought = 0, 0

for price in sorted(sell):

    selling = sell[price]

    if bought + selling < target_drinks:
        ans += price * selling    
        bought += selling
    else:
        ans += price * (target_drinks - bought)
        break

print(ans)


### FIXED CODE ###

H, W = map(int, input().split())

h, w = map(int, input().split())

tot = H * W

rows = h * W
cols = H * w
common = h * w

print(tot - rows - cols + common)
N = int(input())

height = list(map(int,input().split()))

ans = 0
interval = 0

for i in range(N):
  #区間が足りない時
  if height[i] > interval:
　　#今の区間と必要な区間の差分を取って足し、新たな区間の数を定義する
　　##順番に注意 先にintervalに定義すると、
   ##ansがそれに影響されるためansが差分の分だけ余分にに大きくなる
    ans += height[i] - interval
    interval += height[i] - interval
  #区間が多い時、必要な数に減らす
  else:
    interval = height[i]

print(ans)

### FIXED CODE ###

N = int(input())

H = list(map(int,input().split()))

flowers = [0] * N

start = 0
end = 0

con = 0

while flowers != H:
	for i in range(0,N):
		if H[i] > flowers[i]:
			start = i
			for j in range(i,N):
				if H[j] == flowers[j]:
					end = j
					break
				else:
					end = N
			break


	for k in range(start,end):
		flowers[k] += 1
	con += 1
	start = 0
	end = 0
	
	


print(con)
import math
A,B = map(int,input().split())
ans = math.floor(A*B)

### FIXED CODE ###

import decimal
A,B = map(decimal.Decimal,input().split())
c = decimal.Decimal(A * B)
d = c.quantize(decimal.Decimal('0'), rounding=decimal.ROUND_FLOOR)
print(d)
n = input()
#a = [39, 98, 44, 13, 88, 53, 10]
a = [input() for i in range(n)]
b = [0, 0, 0]
for i in a:
	j = 0
	while j < 3:
		if i > b[j]:
			b.insert(j, i)
			break
		j += 1

for i in b[:3]:
	print i

### FIXED CODE ###

import sys

a = []
for line in sys.stdin:
    a.append(int(line))
b = [0, 0, 0]
for i in a:
	j = 0
	while j < 3:
		if i > b[j]:
			b.insert(j, i)
			break
		j += 1

for i in b[:3]:
	print i
N, X, M = map(int, input().split())
ls = [False]*M
ls_mod = []
x = X
for m in range(M+1):
  if ls[x] == False:
    ls_mod.append(x)
    ls[x] = m
    x = (x**2)%M
  else:
    last = m
    fast = ls[x]
    diff = last - fast
    break
if fast >= N:
  print(sum(ls_mod[:N]))
else:
  shou = (N-fast) // diff
  amari = (N-fast) % diff
  print(sum(ls_mod[:fast])+sum(ls_mod[fast:])*shou+sum(ls_mod[fast:fast+amari]))



### FIXED CODE ###

N, X, M = map(int, input().split())
ls = [False]*M
ls_mod = []
x = X
for m in range(M+1):
  if ls[x] == False:
    ls_mod.append(x)
    ls[x] = m
    x = (x**2)%M
  else:
    last = m
    fast = ls[x]
    diff = last - fast
    break
if M == 1:
  print(0)
else:
  if last > N:
    print(sum(ls_mod[:N]))
  else:
    shou = (N-fast) // diff
    amari = (N-fast) % diff
    print(sum(ls_mod[:fast])+sum(ls_mod[fast:])*shou+sum(ls_mod[fast:fast+amari]))
a, b, c, x = [int(input()) for i in range(4)]

can_count = 0

for i in range(a+1):
	coins_price_500 = i * 500
	for j in range(b+1):
		coins_price_100 = j * 100
		for k in range(c+1):
			coins_price_50 = k * 50
			if x == coins_price_500 * coins_price_100 * coins_price_50:
				can_count += 1

print(can_count)

### FIXED CODE ###

n = int(input())
a = [tuple(map(int, input().split(" "))) for i in range(2)]

up_line, down_line = a

max_num = 0
for i in range(n):
	num = sum(up_line[:i+1]) + sum(down_line[i:])
	if max_num < num:
		max_num = num
print(max_num)
class unionFind:
    parent = []
    nums = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.nums = [1]*N

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            self.nums[y] += self.nums[x]
            self.nums[x] = 0
            return


N, M = map(int, input().split())
G = unionFind(N)
ans = [0]*M
ab = [tuple(map(int, input().split())) for i in range(M)]

tmp = (N * (N-1)) // 2
for i, x in enumerate(reversed(ab)):
    a, b = x[0], x[1]
    ans[M-1-i] = tmp
    a = G.root(a-1)
    b = G.root(b-1)
    if a == b:
        continue
    else:
        tmp -= G.nums[a+1] * G.nums[b]
        G.parent[a+1] = b
        G.nums[b] += G.nums[a+1]
        G.nums[a+1] = 0


for i in range(M):
    print(ans[i])


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**5)


class unionFind:
    parent = []
    nums = []

    def __init__(self, n):
        self.parent = [i for i in range(n)]
        self.nums = [1]*n

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            self.nums[y] += self.nums[x]
            self.nums[x] = 0
            return


N, M = map(int, input().split())
G = unionFind(N)
ans = [0]*M
ab = [tuple(map(int, input().split())) for i in range(M)]

tmp = (N * (N-1)) // 2
for i, x in enumerate(reversed(ab)):
    a, b = x[0], x[1]
    ans[M-1-i] = tmp
    a = G.root(a-1)
    b = G.root(b-1)
    if a == b:
        continue
    else:
        tmp -= G.nums[a] * G.nums[b]
        G.parent[a] = b
        G.nums[b] += G.nums[a]
        G.nums[a] = 0


for i in range(M):
    print(ans[i])
import sys
lines = sys.stdin.readlines()
 
N, X = lines.strip("\n")
X = map(int, X.strip(" "))
 
res = float('inf')
for i in range(1, 101):
  cur_res = 0
  for j in X:
    cur_res = cur_res + (j-i)^2
  if cur_res < res:
    res = cur_res
 
print(res)

### FIXED CODE ###

N=int(input())
Xs = list(map(int, input().split()))

res=0
for i in Xs:
    res = res + (i-round(sum(Xs)/N))**2

print(res)
K,T=map(int,input.split())
A=list(map(int,input().split()))
from heapq import heappush,heappop
from collections import deque

A.sort(reverse=True)
d=deque(A)
while d:
  if len(d)==1:
    print(d-1)
    exit()
  a=d.pop()
  b=d.popleft()
  if not a==1:
    d.append(a-1)
  d.appendleft(b-1)

    
  

### FIXED CODE ###

K,T=map(int,input().split())
A=list(map(int,input().split()))
from heapq import heappush,heappop
from collections import deque

A.sort(reverse=True)
d=deque(A)
d2=deque(A)
ans=A[0]
mod=1
while d:
  if len(d)==1:
    print(d[0]-1)
    exit()
  a=d.pop()
  b=d.popleft()
  if not a==1:
    d.append(a-1)
  if not b==1:
    d.appendleft(b-1)
print(0)
K,S = map(int,input().split())
count = 0
for x in range(K + 1):
  for y in range(K + 1):
    if 0 <= S - x - y <= k:
        count += 1
print(count)

### FIXED CODE ###

K,S = map(int,input().split())
count = 0
for x in range(K + 1):
  for y in range(K + 1):
    z = S - x - y
    if 0 <= z and z <= K:
      count += 1
print(count)
import functools
import fractions

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    A = list(map(int, input().split()))
    A = list(set(A))
    N = len(A)
  else:
    # N = 3
    # A = [7,6,8]
    # N = 3
    # A = [12,15,18]
    N = 2
    A = [1000000000,1000000000]

  x = 0
  for i in range(N):
    tmp = functools.reduce(fractions.gcd, A[0:i]+A[i+1:])
    if tmp > x:
      x = tmp
  
  print(x)
  
main()

### FIXED CODE ###

import functools
import fractions

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    A = list(map(int, input().split()))
  else:
    # N = 3
    # A = [7,6,8]
    N = 3
    A = [12,15,18]
    # N = 3
    # A = [1,1000000000,1000000000]

  AL = [0] * N    
  AR = [0] * N
  for i in range(N):
    if i != 0:
      AL[i] = fractions.gcd(AL[i-1],A[i])
    else:
      AL[i] = A[i]
    j = N - i - 1
    if j!= N - 1:
      AR[j] = fractions.gcd(AR[j+1],A[j])
    else:
      AR[j] = A[j]
      
  if 'get_ipython' in globals():   
    print(AL)
    print(AR)
  
  x = 0
  for i in range(N):
    if i == 0:
      tmp = AR[i+1]
    elif i == N - 1:
      tmp = AL[i-1]
    else:
      tmp = fractions.gcd(AL[i-1],AR[i+1])
    if tmp > x: x = tmp
  
  print(x)
    
main()
def main():
  n=int(input())
  dp=[[0,0,0]]
  for i in range(1,n+1):
    a,b,c=map(int,input().split())
    dp.append([
      dp[i-1][0]+max(b,c),
      dp[i-1][1]+max(a,c),
      dp[i-1][2]+max(a,b)
    ]
  print(max(dp[n]))

### FIXED CODE ###

N=int(input())
h=list(map(int,input().split()))
DP=[0]*N
DP[0]=0
DP[1]=abs(h[1]-h[0])+DP[0]
i=2
while i<N:
  DP[i]=min(abs(h[i]-h[i-1])+DP[i-1],abs(h[i]-h[i-2])+DP[i-2])
  i+=1
print(DP[-1])
n=int(input())
a=list(map(int,input().split()))

l=[]
l.append(0)
l.append(0)
r=[]
r.append(a[n-1])
r.append(a[n-1])
g=[]

def gcd(x,y):
  if x%y==0:
    return y
  else:
    return gcd(y,x%y)


for i in range(1,n):
  l.append(gcd(l[i],a[i-1]))
  r.append(gcd(r[i],a[n-1-i]))
  
r[0]=l[n]

for j in range(0,n):
  g.append(gcd(l[j+1],r[n-j-1]))

print(max(g))    
  
  


### FIXED CODE ###

a,b,t=map(int,input().split())
print(b*(t//a))
from sympy import nextprime
n=int(input())
print(nextprime(n))

### FIXED CODE ###

n=int(input())
for i in range(n,10**6):
  for j in range(2,int(i**0.5)+1):
    if i%j==0:
      break
  else:
    print(i)
    break
x, y = map(int, input().split())
mn = 10**9

f = lambda x: mn if x < 0 else x

print(min(f(y-x), f(y+x)+1, f(-y-x)+1, f(-y+x2)))

### FIXED CODE ###

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

f = lambda x: 10**10 if x < 0 else x

print(min(f(y-x), f(y+x)+1, f(-y-x)+1, f(-y+x)+2))
def b_powers_of_two(N, A):
    # 参考: https://beta.atcoder.jp/contests/agc029/submissions/3804857
    from bisect import bisect_left
    a = sorted(A)
    ans = 0
    for j in range(N - 1, -1, -1):
        # b > a[j]となるような最小のb(これは2の冪乗である)を探す
        b = 1
        while b <= a[j]:
            b <<= 1

        k = bisect_left(a, b - a[j])
        if k < j and (b - a[j] == a[k]):
            # a[k] <= a[j] であり、かつ、a[j] + a[k] = b(2の冪乗)
            # となるペア(j, k)が見つかれば、貪欲にペアにしてよい。
            # (a[j] を放置すると、ペアにできる可能性がある整数が少なくなり、得にならない)
            ans += 1
            # 整数でなくしておいて、ペアに使えないようにする。
            # ペアのうち、要素番号が大きい方は足し、小さい方は引いておくと、
            # 同じ値が複数あったときも二部探索で以前にペアにした整数を
            # ピックアップすることがなくなる。
            a[j] += 0.1
            a[k] -= 0.1

    return ans

A = [int(i) for i in input().split()]
print(b_powers_of_two(N, A))

### FIXED CODE ###

def b_powers_of_two(N, A):
    # 参考: https://beta.atcoder.jp/contests/agc029/submissions/3804857
    from bisect import bisect_left
    a = sorted(A)
    ans = 0
    for j in range(N - 1, -1, -1):
        # b > a[j]となるような最小のb(これは2の冪乗である)を探す
        b = 1
        while b <= a[j]:
            b <<= 1

        k = bisect_left(a, b - a[j])
        if k < j and (b - a[j] == a[k]):
            # a[k] <= a[j] であり、かつ、a[j] + a[k] = b(2の冪乗)
            # となるペア(j, k)が見つかれば、貪欲にペアにしてよい。
            # (a[j] を放置すると、ペアにできる可能性がある整数が少なくなり、得にならない)
            ans += 1
            # 整数でなくしておいて、ペアに使えないようにする。
            # ペアのうち、要素番号が大きい方は足し、小さい方は引いておくと、
            # 同じ値が複数あったときも二部探索で以前にペアにした整数を
            # ピックアップすることがなくなる。
            a[j] += 0.1
            a[k] -= 0.1

    return ans

N = int(input())
A = [int(i) for i in input().split()]
print(b_powers_of_two(N, A))
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
numk={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
dp=[0]+[-float('inf')]*(N)
for i in range(N):
  for j in A:
    if i+numk[j]<=N:
      dp[i+numk[j]]=max(dp[i+numk[j]],dp[i]+1)
m=N
ans=[]
while m>0:
  for i in range(M):
    if dp[m-numk[A[i]]]+1==dp[m]:
      ans.append(A[i])
      m-=numk[A[i]]
      break
ans.sort(reverse=True)
for i in range(len(ans)):
  ans[i]=str(ans[i])
a=''.join(ans)
print(a)

### FIXED CODE ###

N,M=map(int,input().split())
num={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
A=list(map(int,input().split()))
A.sort(reverse = True)
dp = [0]+[-1]*(N+1)
for i in range(N):
    for j in A:
        if i+num[j]<=N:
            dp[i+num[j]] = max(dp[i+num[j]],dp[i]+1)
ans = []
m=N
while m>0:
    for i in A:
        if m-num[i]>=0 and dp[m-num[i]]==dp[m]-1:
          m-=num[i]
          ans.append(str(i))
          break
print("".join(ans))
n, x = map(int, input().split())

def func(a, b):
    if a < b:
        if b%a == 0:
            return (b//a*2 - 1)*a
        else:
            return 2*a + func(a, b-a)
    elif a > b:
        if a%b == 0:
            return (a//b*2 - 1)*b
        else:
            return 2*b + func(a-b, b)
    else:
        return a

if x == n/2:
    print(3*x)
else:
    print(x+(n-x)+func(x,n-x))

### FIXED CODE ###

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

def func(a, b):
    if a < b:
        if b%a == 0:
            return (b//a*2 - 1)*a
        else:
            q, mod = divmod(b,a)
            return 2*q*a + func(a, mod)
    elif a > b:
        if a%b == 0:
            return (a//b*2 - 1)*b
        else:
            q, mod = divmod(a,b)
            return 2*q*b + func(mod, b)
    else:
        return a

if x == n/2:
    print(3*x)
else:
    print(x+(n-x)+func(x,n-x))
A,B,C,D = map(int,input().split())
ans = min(A,B) + min(C, D)
print(ans)

### FIXED CODE ###

A = int(input())
B = int(input())
C = int(input())
D = int(input())
ans = min(A,B) + min(C,D)
print(ans)
import bisect

n = int(input())
l = sorted([int(li) for li in input().split()])

sum = 0
for i in range(n-1,-1,-1):
    for j in range(i-1,-1,-1):
        k = bisect.bisect_right(l[j],l[i]-l[j])
        sum += j-k if j>k else 0

print(sum)


### FIXED CODE ###

import bisect

n = int(input())
l = sorted([int(li) for li in input().split()])

sum = 0
for i in range(n-1,-1,-1):
    for j in range(i-1,-1,-1):
        k = bisect.bisect_right(l,l[i]-l[j])
        sum += j-k if j>k else 0

print(sum)
import math

K = int(input())

for a in range(K):
  for b in range(K):
    for c in range(K):
      gcd = math.gcd(a, b, c)
      
      sum_gcd += gcd

print(sum_gcd)

### FIXED CODE ###

from math import gcd
K = int(input())
sum_gcd = [0]*201
ans = 0
 
for i in range(1,K+1):
  for j in range(1,K+1):
    sum_gcd[gcd(i,j)] += 1
for i in range(1,K+1):
  for k in range(1,K+1):
    ans += sum_gcd[i]*gcd(i,k)

print(ans)
n,m=map(int,input().split())
if m%2+n%2<2:
  print(n*m//2)
else:
  print(max(n*(m//2)+(n//2+1))


### FIXED CODE ###

n,m=map(int,input().split())
if m==1 or n==1:
  print(1)
elif m%2+n%2<2:
  print(n*m//2)
else:
  print(n*(m//2)+(n//2+1))
from sys import stdin
import sympy as sp
K = int(stdin.readline().rstrip())
[A, B] = [int(x) for x in input().rstrip().split()]


flag=0
K_temp=K
while(True):
    if(K_temp>B):
        break
    if(K_temp>=A and K_temp<=B):
        flag=1
        break
    else:
        K_temp+=K

if flag==1:
    print("OK")
else:
    print("NG")

### FIXED CODE ###

from sys import stdin

K = int(stdin.readline().rstrip())
[A, B] = [int(x) for x in input().rstrip().split()]


flag=0
K_temp=K
while(True):
    if(K_temp>B):
        break
    if(K_temp>=A and K_temp<=B):
        flag=1
        break
    else:
        K_temp+=K

if flag==1:
    print("OK")
else:
    print("NG")
import sys
def input():
    return sys.stdin.readline()

def main():
    S = input()
    S = S[::-1]

    dp = [[0] * 15 for _ in range(len(S) + 5)]
    MOD = 10**9 + 7

    dp[0][0] = 1

    for i in range(len(S)):
        for j in range(13):
            if S[i] != '?':
                k = int(S[i])
                dp[i+1][(j+k*10**i)%13] += dp[i][j]
                dp[i+1][(j+k*10**i)%13] %= MOD
            else:
                for k in range(10):
                    dp[i+1][(j+k*10**i)%13] += dp[i][j]
                    dp[i+1][(j+k*10**i)%13] %= MOD


    print(dp[len(S)][5])



main()

### FIXED CODE ###

def main():
    S = list(input())

    dp = [0] * 13
    dp[0] = 1
    mod = 10**9 + 7
    digit = 1
    for i in range(len(S)-1,-1,-1):
        nextdp = [0] * 13
        if S[i] == '?':
            for j in range(10):
                for k in range(13):
                    nextdp[(j * digit + k) % 13] += dp[k]
                    # あらかじめ10*9 + 7で割って余りを出しておく
                    nextdp[(j * digit + k) % 13] %= mod
        else:
            j = int(S[i])
            for k in range(13):
                nextdp[(j * digit + k) % 13] += dp[k]
                # あらかじめ10*9 + 7で割って余りを出しておく
                nextdp[(j * digit + k) % 13] %= mod
        
        digit *= 10
        # A * 桁数の13で割ったあまりは (A % 13) * (桁数 % 13)のため、桁数を13で割っておく
        digit %= 13
        dp = nextdp
    
    print(dp[5])

main()
def solve(i, m):
  if dp[i][m] != -1:
    return dp[i][m]

  if m == 0:
    dp[i][m] = 1
  elif i >= n:
    dp[i][m] = 0
  elif solve(i+1, m):
    dp[i][m] = 1
  elif solve(i+1, m-A[i]):
    dp[i][m] = 1
  else:
    dp[i][m] = 0

  return dp[i][m]



n = input()
A = map(int, raw_input().split())
p = input()
m = map(int, raw_input().split())

dp = [[]*p]*n

for i in range(n):
  for m in range(p):
    dp[i][m] == -1

for i in range(p):
  if solve(0, m[i]):
    print 'yes'
  else:
    print 'no'

### FIXED CODE ###

def solve(i, m):
  if dp[i][m] != -1:
    return dp[i][m]

  if m == 0:
    dp[i][m] = 1
  elif i >= n:
    dp[i][m] = 0
  elif solve(i+1, m):
    dp[i][m] = 1
  elif solve(i+1, m-A[i]):
    dp[i][m] = 1
  else:
    dp[i][m] = 0

  return dp[i][m]



n = input()
A = map(int, raw_input().split())
p = input()
m = map(int, raw_input().split())

dp = [[-1 for col in range(2000)] for row in range(n+1)]

for i in range(p):
  if solve(0, m[i]):
    print 'yes'
  else:
    print 'no'
def is_kaibun(kaibun):
    for i in range(len(kaibun)//2):
        if kaibun[i] != kaibun[-i-1]:
            return False
    return True
  
s = input()

s_1 = s[:(len(s)-1)/2]
s_2 = s[(len(s)-3)/2:]

if is_kaibun(s) and is_kaibun(s_1) and is_kaibun(s_2):
  print('Yes')
else:
  print('No')

### FIXED CODE ###

def is_kaibun(kaibun):
    for i in range(len(kaibun)//2):
        if kaibun[i] != kaibun[-i-1]:
            return False
    return True
  
s = input()

s_1 = s[:(int)((len(s)-1)/2)]
s_2 = s[(int)((len(s)+3)/2)-1:len(s)]

if is_kaibun(s) and is_kaibun(s_1) and is_kaibun(s_2):
  print('Yes')
else:
  print('No')
a, b, k = map(int,input().split())
List = []

for i in range(1,101,1):
    if a % i == 0 and b % i == 0:
        List.append(i)

print(List[k-1])

### FIXED CODE ###

a, b, k = map(int,input().split())
List = []

for i in range(1,101,1):
    if a % i == 0 and b % i == 0:
        List.append(i)

print(List[-k])
a, b, k = map(int, input().split())
l = []
for i in range(1, max(a,b)):
    if a % i == 0 and b % i == 0:
        l.append(i)
print(l[-k])

### FIXED CODE ###

a, b, k = map(int, input().split())
l = []
for i in range(1, max(a,b)+1):
    if a % i == 0 and b % i == 0:
        l.append(i)
print(l[-k])
def main():
    n,p=map(int,input().split())
    s=input()
    cnt,ans=0,0
    m=[0]*p

    if p==2 or p==5:
        for i in s:
            if i%p==0:
                cnt+=1
        print(cnt*(cnt+1)//2)
        exit()

    for i in range(n):
        m[int(s[i:])%p]+=1
    
    for i in m:
        ans+=i*(i-1)//2
    ans+=m[0]

    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    n,p=map(int,input().split())
    s=list(map(int,list(input())))
    cnt,ans=0,0
    m=[0]*p
    m[0]=1

    if p in [2, 5]:
        for i, x in enumerate(s):
            if x % p == 0:
                ans += i + 1
        print(ans)
        exit()

    sm=0
    d=1
    for i in range(n):
        sm=(sm+s[n-1-i]*d)%p
        m[sm]+=1
        d=(d*10)%p
    
    for i in m:
        ans+=i*(i-1)//2

    print(ans)

if __name__ == '__main__':
    main()
from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

#### START
n = int(input())
a = list(map(int,input().split()))

ans = 10**100
for i in range(1, n):
    if ans>abs(sum(a[:i])-sum(a[i:])):
        ans = abs(sum(a[:i])-sum(a[i:]))

print(ans)

### FIXED CODE ###


from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

#### START
n = int(input())
a = list(map(int,input().split()))

ans = 10**100
res = np.cumsum(a)

print(min([abs(res[-1]-res[i]-res[i]) for i in range(len(res)-1)]))
#!/usr/bin/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]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def main():
    N = int(input())
    S = list(map(int, input().split()))

    if "Y" in S:
        print("Four")
    else:
        print("Three")

if __name__ == '__main__':
    main()


### FIXED CODE ###

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
INF = float("inf")
import sys
sys.setrecursionlimit(10000)


def solve(N, S):
    if "Y" in S:
        return "Four"
    else:
        return "Three"


def main():
    N = int(input())
    S = list(input().split())
    print(solve(N, S))


if __name__ == '__main__':
    main()
n, m = map(int, input().split())
ab = [[int(i) for i in input().split()] for i in range(m)]
count = 0
while len(ab) > 0:
    ab = sorted(ab, key = lambda x: x[1])
    end = ab[0]
    list.sort(ab, reverse=True)
    remove = ab.index(end)
    while ab[remove][0] == end[0]:
        remove += 1
    del ab[(remove - 1):]
    count += 1
print(int(count))


### FIXED CODE ###

n, m = map(int, input().split())
l = []
for i in range(m):
    a, b = map(int, input().split())
    l.append((b, a))
l.sort()
answer = 0
last = 0
for b, a in l:
    if a >= last:
        answer += 1
        last = b
print(answer)
# -*- coding=utf-8 -*-
price = [int(input()) for p in range(int(input()))]

# Nのコード　→ いけるぞ！
# まずは最小値を調査
min_ = price[0]
index_ = 0
for i in range(1, len(price)):
    if price[i] < min_:
        min_ = price[i]
        index_ = i

# 次は最大値を調査
print(max(price[index_ + 1:]) - min_)


### FIXED CODE ###

# -*- coding=utf-8 -*-
price = [int(input()) for p in range(int(input()))]

# N ^ 2 のコード → ダメダメ
# max_ = price[1] - price[0]
# for i in range(1, len(price)):
#     for j in range(0, i):
#         if price[i] - price[j] >= max_:
#             max_ = price[i] - price[j]

# Nのコード　→ いけるぞ！
# まずは最小値を調査
min_ = price[0]
max_ = -1000000000000
for i in range(1, len(price)):
    if price[i] - min_ > max_:
        max_ = price[i] - min_

    if price[i] < min_:
        min_ = price[i]

print(max_)
import numpy as np
N = int(input())
n = [x for x in range(1, N+1)]
m = n[1:]
m.append(n[0])
n = np.array(n)
m = np.array(m)
x = n % m
print(sum(x))

### FIXED CODE ###

N = int(input())
print((N+1) * N // 2 - N)
a, b = map(int, raw_input().split(" ")):

if  a > b :
print "a > b"
if  a < b :
print "a < b"
if  a == b :
print "a == b"

### FIXED CODE ###

a,b=map(int,raw_input().split())
if a > b:
    print 'a > b'
elif a < b:
    print 'a < b'
else:
    print 'a == b'
n = int(input())
a = [int(e) for e in input().split()]
for i in range(0,n):
    for j in range(0,n):
        if l[j] == i:
            print(j, end = " ")
            break


### FIXED CODE ###

n = int(input())
a = [int(e) for e in input().split()]
b = [0] * n
for i in range(0,n):
    b[a[i] - 1] = i + 1
for j in range(0,n):
    print(b[j], end = " ")