Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import numpy as np
N,M=map(int,input().split(" "))
A=list(sorted(map(int,input().split(" "))))
L=np.array([A[s]-A[s+1] for s in range(M-1)])

if M<=N:
    print(0)
else:
    for s in range(N-1):
        L[np.argmin(L)]=0
    print(abs(sum(L)))

### FIXED CODE ###

N,M=map(int,input().split(" "))
A=list(sorted(map(int,input().split(" "))))
L=[A[s]-A[s+1] for s in range(M-1)]
if M>N:
    print(abs(sum(sorted(L)[N-1:])))
else:
    print(0)
### BUGGY CODE ###

n = int(input())
ll = [[0]*i for i in range(n)]
for i in range(n):
  x, l =map(int, input().split())
  ll[i] = [x-l, x+l]
ll = sorted(ll)

l = ll[0]
ans = n

for i in range(n-1):
  li = ll[i+1]
  if l[1] > li[0]:
    ans -= 1
  else:
    l = li

print(ans)

### FIXED CODE ###

n = int(input())
ll = []
for i in range(n):
  x, l = map(int, input().split())
  ll.append([x-l, x+l])
ll = sorted(ll)

ans = n
r = ll.pop()[0]

while ll:
  while ll and ll[-1][1] > r:
    ans -= 1
    ll.pop()
  if ll:
    r = ll.pop()[0]

print(ans)
### BUGGY CODE ###

if __name__ == '__main__':
    ks = input()
    k, s = [int(i) for i in ks.split()]

    c = 0
    for x in range(k+1):
        for y in range(k+1):
            for z in range(k+1):
                if x + y + z == s:
                    c += 1

    print(c)


### FIXED CODE ###

if __name__ == '__main__':
    ks = input()
    k, s = [int(i) for i in ks.split()]

    c = 0
    for x in range(k+1):
        for y in range(k+1):
            if 0 <= s - x - y <= k:
                c += 1

    print(c)
### BUGGY CODE ###

import sys
#大きすぎるとダメ
sys.setrecursionlimit(2550)
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
inf=1000000000000000
d=[[inf]*w for i in range(h)]


def dfs(i,j):
    global h,w,s
    nex=[(0,-1),(0,1),(-1,0),(1,0)]
    for k in range(4):
        l,m=i+nex[k][0],j+nex[k][1]
        if 0<=i+nex[k][0]<h and 0<=j+nex[k][1]<w:
            if s[l][m]=="." and d[l][m]>d[i][j]+1:
                d[l][m]=d[i][j]+1
                dfs(l,m)
d[0][0]=0
dfs(0,0)
ans=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            ans+=1
if d[h-1][w-1]==inf:
    print(-1)
else:
    print(ans-d[h-1][w-1]-1)




### FIXED CODE ###

#一個だけRE、わからねえ
#解答見る
import sys
sys.setrecursionlimit(1005)
h,w=map(int,input().split())

def go_next(I,J,n):
    n+=1
    global dp,w,h
    if n>1000:
        return
    if 0<=I<h-1:
        if x[I+1][J]=="." and dp[I+1][J]>dp[I][J]+1:
            dp[I+1][J]=dp[I][J]+1
            go_next(I+1,J,n)
    if 0<I<=h-1:
        if x[I-1][J]=="." and dp[I-1][J]>dp[I][J]+1:
            dp[I-1][J]=dp[I][J]+1
            go_next(I-1,J,n)
    if 0<=J<w-1:
        if x[I][J+1]=="." and dp[I][J+1]>dp[I][J]+1:
            dp[I][J+1]=dp[I][J]+1
            go_next(I,J+1,n)
    if 0<J<=w-1:
        if x[I][J-1]=="." and dp[I][J-1]>dp[I][J]+1:
            dp[I][J-1]=dp[I][J]+1
            go_next(I,J-1,n)

x=[]
for i in range(h):
    x.append(list(input()))
#print(x)
c=0
for i in range(h):
    for j in range(w):
        if x[i][j]==".":
            c+=1
dp=[[10000000000 for i in range(w)]for j in range(h)]
dp[0][0]=1
go_next(0,0,0)
if dp[h-1][w-1]==10000000000:
    print(-1)
else:
    print(c-dp[h-1][w-1])
### BUGGY CODE ###

def main():
  h,*s=open(0)
  h,w,k=map(int,h.split())
  s=[list(map(int,t[:w]))for t in s]
  m=9e9
  for i in range(1<<h-1):
    t=[]
    b=v=l=0
    for j in range(h):
      if i>>j&1:
        t+=s[b:j+1],
        b=j+1
        l+=1
    t+=s[b:],
    l+=1
    c=[0]*l
    for i in range(w):
      f=g=0
      u=[]
      for j,d in enumerate(t):
        d=sum(d[i]for d in d)
        u+=d,
        if d>k:
          g=1
          break
        c[j]+=d
        f|=c[j]>k
      else:
        if f:
          c=u
          v+=1
      if g:break
    else:
      if v+l-1<m:m=v+l-1
  print(m)
main()

### FIXED CODE ###

h,*s=open(0)
h,w,k=map(int,h.split())
s=[list(map(int,t[:w]))for t in s]
m=9e9
for i in range(1<<h-1):
  t=[]
  b=v=l=0
  for j in range(h):
    if i>>j&1:
      t+=s[b:j+1],
      b=j+1
      l+=1
  t+=s[b:],
  l+=1
  c=[0]*l
  for i in range(w):
    f=g=0
    u=[]
    for j in range(l):
      d=sum(t[i]for t in t[j])
      u+=d,
      if d>k:
        g=1
        break
      c[j]+=d
      f|=c[j]>k
    else:
      v+=f
      if f:c=u
    if g:break
  else:m=min(m,v+l-1)
print(m)
### BUGGY CODE ###

from bisect import bisect_left

def main():
    N = int(input())
    S = input()
    
    R_index = []
    G_index = []
    B_index = []
    
    for i, s in enumerate(S):
        if s == 'R':
            R_index.append(i)
        elif s == 'G':
            G_index.append(i)
        elif s == 'B':
            B_index.append(i)
    
    ans = 0
    for i in R_index:
        sj = bisect_left(G_index, i)
        for ij in range(sj, len(G_index)):
            j = G_index[ij]
            sk = bisect_left(B_index, j)
            for ik in range(sk,len(B_index)):
                k = B_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(B_index, i)
        for ij in range(sj,len(B_index)):
            j = B_index[ij]
            sj = bisect_left(G_index, j)
            for ik in range(sj,len(G_index)):
                k = G_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    for i in G_index:
        sj = bisect_left(B_index, i)
        for ij in range(sj, len(B_index)):
            j = B_index[ij]
            sk = bisect_left(R_index, j)
            for ik in range(sk,len(R_index)):
                k = R_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(R_index, i)
        for ij in range(sj,len(R_index)):
            j = R_index[ij]
            sj = bisect_left(B_index, j)
            for ik in range(sj,len(B_index)):
                k = B_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    for i in B_index:
        sj = bisect_left(R_index, i)
        for ij in range(sj, len(R_index)):
            j = R_index[ij]
            sk = bisect_left(G_index, j)
            for ik in range(sk,len(G_index)):
                k = G_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
        sj = bisect_left(G_index, i)
        for ij in range(sj,len(G_index)):
            j = G_index[ij]
            sj = bisect_left(R_index, j)
            for ik in range(sj,len(R_index)):
                k = R_index[ik]
                if i < j < k and j - i != k - j:
                    ans += 1
    
    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import Counter

N = int(input())
S = input()

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1

print(ans)
### BUGGY CODE ###

N = int(input())
a = (int(x) for x in input().split())
number =1

for i in a:
    if number==0:
        print(0)
        exit()
    number *= i
    

if number>10**18:
    print(-1)
    exit()

print(number)

### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()]
number =1

if 0 in a:
    print(0)
    exit()

for i in a:
    number *= i
    if number > 10**18:
            print(-1)
            exit()

print(number)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)

h,w = map(int, input().split())
board = [[int(j=="#") for j in input()] for i in range(h)]
dxdy = [[0,1],[1,0],[0,-1],[-1,0]]

def dfs(i, j, k, prev):
    if used[i][j] or board[i][j] == prev:
        return
    used[i][j] = 1
    k[board[i][j]] += 1
    for dx, dy in dxdy:
        nx, ny = i+dx, j+dy
        if 0 <= nx < h and 0 <= ny < w and not used[nx][ny] and board[nx][ny] != board[i][j]:
            dfs(nx,ny,k,board[i][j])

used = [[0]*w for i in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if used[i][j]:
            continue
        k = [0]*2
        dfs(i,j,k, -1)
        ans += k[0]*k[1]
print(ans)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)

h,w = map(int, input().split())
board = [[int(j=="#") for j in input()] for i in range(h)]
dxdy = [[0,1], [0,-1], [1,0], [-1,0]]

def dfs(i, j, k, prev):
    if used[i][j] or board[i][j] == prev:
        return
    used[i][j] = 1
    k[board[i][j]] += 1
    for dx, dy in dxdy:
        nx, ny = i+dx, j+dy
        if 0 <= nx < h and 0 <= ny < w and not used[nx][ny] and board[nx][ny] != board[i][j]:
            dfs(nx,ny,k,board[i][j])

used = [[0]*w for i in range(h)]
ans = 0
for i in range(h):
    for j in range(w):
        if used[i][j]:
            continue
        k = [0]*2
        dfs(i,j,k, -1)
        ans += k[0]*k[1]
print(ans)
### BUGGY CODE ###

N, K = map(int, input().split())
h = list(map(int, input().split()))

dp = [1<<30]*(N)
dp[0] = 0

for i in range(0, N):
  for k in range(K+1):
    if i+k >= N:
      continue
    dp[i+k] = min(dp[i+k], dp[i]+abs(h[i+k]-h[i]))

print(dp[N-1])

### FIXED CODE ###

N, K = map(int, input().split())
h = list(map(int, input().split()))
 
dp = [1<<30]*(N)
dp[0] = 0
 
for i in range(1, N):
  dp[i] = min((dp[j]+abs(h[i]-h[j])) for j in range(max(0, i-K), i))
 
print(dp[N-1])
### BUGGY CODE ###

N = int(input())
S = input()

# 全探索
cnt = 0
for i in range(0, N-2):
  for j in range(i+1, N-1):
    for k in range(j+1, N):
      # 条件1(全要素が異なる文字判定)
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        # 条件2判定
        if j - i != k - j:
          cnt += 1

print(cnt)


### FIXED CODE ###

N = int(input())
S = input()

# RGBそれぞれの字数カウント
R = S.count('R')
G = S.count('G')
B = S.count('B')

# 条件２の対象数カウント
ad = R * G * B
ed = 0

for i in range(0, N-1):
  for j in range(i+1, N):
    # 判定
    P = j+(j-i)
    if P < N:
      if S[i] != S[j] and S[j] != S[P] and S[P] != S[i]:
        ed += 1

print(ad-ed)
### BUGGY CODE ###

import numpy as np

N, M = (list(map(int, input().split())))
a = np.array([0]*M)
b = np.array([0]*M)
for i in range(M):
  a[i], b[i] = (input().split())
ans = 0
for i in range(M):
  C = set()
  D = set()
  count = 0
  boo = True
  while(boo):
    for j in range(M):
      if i != j:
        if(count == 0):
          C.add(a[j])
          C.add(b[j])
          count += 1
        if C <= {a[j]} or C <= {b[j]}:
          C.add(a[j])
          C.add(b[j])
    if(C in D):
      boo = False
      break
    else:
      D = C
  if(len(C) == N):
    ans += 1
print(ans)

### FIXED CODE ###

import numpy as np
 
N, M = (list(map(int, input().split())))
a = np.array([0]*M)
b = np.array([0]*M)
for i in range(M):
  a[i], b[i] = (input().split())
ans = M
for i in range(M):
  C = set()
  D = set()
  count = 0
  while(True):
    for j in range(M):
      if not i == j:
        if(count == 0):
          C.add(a[j]) 
          C.add(b[j])
          count += 1  
        if a[j] in C or b[j] in C:
          C.add(a[j])
          C.add(b[j])
    if(C == D):
      break
    else:
      D = C.union(D)
  if(len(C) == N):
    ans -= 1
print(ans)
### BUGGY CODE ###

import math
import sys
from functools import reduce
def getN():
    return int(input())

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def main():
    
    K = getN()

    result = 0

    for i in range(1,K+1):
        for j in range(1,K+1):
            for k in range(1,K+1):
                result += gcd(i,j,k)
    
    print(result)

    return

if __name__ == "__main__":
    main()

### FIXED CODE ###

def getN():
    return int(input())

# def gcd(p, q):
#     if p % q == 0: return q
#     return gcd(q, p % q)

def gcd(p, q):
    while q != 0:
        r = p % q
        p = q
        q = r
    return p

def main():
    
    K = getN()

    result = 0

    for i in range(1,K+1):
        for j in range(1,K+1):
            for k in range(1,K+1):
                result += gcd(gcd(i,j),k)
    
    print(result)

    return

if __name__ == "__main__":
    main()
### BUGGY CODE ###

n,m,r_=map(int,input().split())
r=list(map(int,input().split()))
g=[[]for _ in range(n)]
for _ in range(m):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  g[a].append([b,c])
  g[b].append([a,c])
from heapq import heapify,heappush,heappop
def dks(t0,t1):
  kyori=[-1]*n
  todo=[[0,t0]]
  heapify(todo)
  while todo:
    d,t=heappop(todo)
    kyori[t]=d
    if t==t1:break
    l=g[t]
    for li,c in l:
      if kyori[li]==-1:
        heappush(todo,[c+d,li])
  return kyori[t1]
ans=pow(10,9)
allr=[]
import sys
sys.setrecursionlimit(10**7)
def dfs(s,k): #s:list, k:set
  if len(k)==1:
    s.append(k.pop())
    return [s]
  ret=[]
  for ki in k:
    ret.extend(dfs(s+[ki],k-{ki}))
  return ret
allr=dfs([],set(r))
for j in range(len(allr)):
  r1=allr[j]
  ansi=0
  for i in range(r_-1):
    ansi+=dks(r1[i]-1,r1[i+1]-1)
  ans=min(ans,ansi)
print(ans)


### FIXED CODE ###

n,m,r_=map(int,input().split())
r=list(map(int,input().split()))
g=[[]for _ in range(n)]
for _ in range(m):
  a,b,c=map(int,input().split())
  a-=1
  b-=1
  g[a].append([b,c])
  g[b].append([a,c])
from heapq import heapify,heappush,heappop
def dks(t0,t1):
  kyori=[-1]*n
  todo=[[0,t0]]
  heapify(todo)
  while todo:
    d,t=heappop(todo)
    kyori[t]=d
    if t==t1:break
    l=g[t]
    for li,c in l:
      if kyori[li]==-1:
        heappush(todo,[c+d,li])
  return kyori[t1]
ans=pow(10,9)
allr=[]
import sys
sys.setrecursionlimit(10**7)
def dfs(s,k): #s:list, k:set
  if len(k)==1:
    s.append(k.pop())
    return [s]
  ret=[]
  for ki in k:
    ret.extend(dfs(s+[ki],k-{ki}))
  return ret
allr=dfs([],set(r))
d={}
for i in range(r_-1):
  for j in range(i+1,r_):
    k=dks(r[i]-1,r[j]-1)
    d[r[i]-1,r[j]-1]=k
    d[r[j]-1,r[i]-1]=k

for j in range(len(allr)):
  r1=allr[j]
  ansi=0
  for i in range(r_-1):
    ansi+=d[(r1[i]-1,r1[i+1]-1)]
  ans=min(ans,ansi)
print(ans)
### BUGGY CODE ###

s = input()
sl = len(s)
while True:
  so = s
  so=so.replace("10","")
  so=so.replace("01","")
  if len(s) == len(so):
    break
  s = so

print(sl - len(s))

### FIXED CODE ###

s = input()

t = s[0]
for i in s[1:]:
  if not t:
    t=i
    continue
  if i != t[-1]:
    t=t[:-1]
  else:
    t+=i

print(len(s) - len(t))
### BUGGY CODE ###

# D - Triangles
import numpy as np
N = int(input())
L = sorted(np.array(list(map(int, input().split()))))

ans = 0
for i, a in enumerate(L):
    if i == N - 2:
        break
        
    for j, b in enumerate(L[i + 1:]):
        j += i + 1
        if j == N - 1:
            break
            
        s = a + b
        ans += np.count_nonzero(L[j + 1: ] < s)
                
print(ans)

### FIXED CODE ###

from bisect import bisect_left
N = int(input())
L = sorted(list(map(int, input().split())))

ans = 0
for b in range(1, N - 1):
    for a in range(b):
        ans += bisect_left(L, L[a] + L[b]) - b - 1
        
print(ans)
### BUGGY CODE ###

import math

MOD = 10 ** 9 + 7


def cmb(n, r):
    total = 1
    for i in range(r):
        total *= (n - i) % MOD
    return total * pow(math.factorial(r), MOD - 2, MOD) % MOD


if __name__ == '__main__':
    n, a, b = list(map(int, input().split(" ")))
    # 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)

    total = pow(2, n, MOD)
    # print(total)
    a_total = cmb(n, a)
    # print(a_total)
    b_total = cmb(n, b)
    print((total - a_total - b_total - 1) % MOD)

### FIXED CODE ###

from math import factorial
N, A, B = map(int, input().split())
MOD = 10 ** 9 + 7

def calc(n):
    ret = 1
    for i in range(n):
        ret = ret * (N - i) % MOD
    return ret * pow(factorial(n), MOD - 2, MOD) % MOD

print(max(0, (pow(2, N, MOD) - ((calc(A) + calc(B)) % MOD) - 1) % MOD))
### BUGGY CODE ###

n, k, q = [int(i) for i in input().split()]
a = [int(input()) for i in range(q)]
out = [k] * n
for i in a:
    out = [i-1 for i in out]
    out[i-1] += 1
for i in out:
    print("Yes" if i > 0 else "No")

### FIXED CODE ###

n, k, q = [int(i) for i in input().split()]
a = [int(input()) for i in range(q)]
out = [k-q] * n
for i in a:
    out[i-1] += 1
for i in out:
    print("Yes" if i > 0 else "No")
### BUGGY CODE ###

N,Q=map(int,input().split())
S=input()
ans=[]
for _ in range(Q):
    l,r=map(int,input().split())
    ans.append(S[l-1:r].count('AC'))
for i in ans:
    print(i)

### FIXED CODE ###

N,Q=map(int,input().split())
S=input()
t=[0,0]
for i in range(2,N+1):
    t.append(t[-1]+(S[i-2:i]=='AC'))
for _ in range(Q):
    l,r=map(int,input().split())
    print(t[r]-t[l])
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
bit = [[n,0] for i in range(60)]
b = []
ans = 0
mod = 10 ** 9 + 7
for i in a:
  b.append(bin(i)[2:])
for i in b:
  for k,j in enumerate(i[::-1]):
    j = int(j)
    if j:
      bit[k][0] -= 1
      bit[k][1] += 1
for k,(z,o) in enumerate(bit):
  ans = (ans + (z * o) * ((2 ** k) % mod)) % mod
print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
bit = [[0,0] for i in range(60)]
b = []
ans = 0
mod = 10 ** 9 + 7
for i in a:
  for j in range(60):
    bit[j][i % 2] += 1
    i //= 2
for k,(z,o) in enumerate(bit):
  ans = (ans + (z * o) * (pow(2,k,mod))) % mod
print(ans)
### BUGGY CODE ###

n = int(input())+1
a = list(map(int, input().split()))
cnt = []

for i in range(100005):
    cnt.append(0)
sum = 0
for i in range(100005):
    cnt[i] = a.count(i)
    sum += i * cnt[i]

q = int(input())
ans = []

for i in range(q):
    b, c = map(int, input().split())
    sum += (c-b)*cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    ans.append(sum)

for i in ans:
    print(i)






### FIXED CODE ###

n = int(input())+1
a = list(map(int, input().split()))
cnt = []
for i in range(100005):
    cnt.append(0)
sum = 0

for i in a:
    cnt[i] += 1
for i in range(100005):
    sum += i * cnt[i]

q = int(input())

for i in range(q):
    b, c = map(int, input().split())
    sum += (c-b)*cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    print(sum)
### BUGGY CODE ###

def fac(x):
    a=1
    for i in range(2,x+1):
        a*=i
    return(a)
def npr(x,x1):
    return fac(x)//(fac(x-x1)*fac(x1))
n=int(input())
l=list(map(int,input().split()))
d={}
d1={}
ans=0
for i in l:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
for i in d.keys():
    x=npr(d[i],2)
    d1[i]=x
    ans+=x
for i in l:
    if d[i]>1:
        print(ans-d1[i]+npr(d[i]-1,2))
    else:
        print(ans)
    


### FIXED CODE ###

n=int(input())
l=list(map(int,input().split()))
d={}
ans=0
for i in l:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
for i in d.keys():
    x=d[i]*(d[i]-1)//2
    ans+=x
for i in l:
    if d[i]>2:
        print(ans-(d[i]*(d[i]-1)//2)+((d[i]-1)*(d[i]-2)//2))
    elif d[i]==2:
        print(ans-1)
    else:
        print(ans)
### BUGGY CODE ###

class SegTree:
    def __init__(self, a, func):
        self.n = 2**(len(a) - 1).bit_length()
        self.f = func
        self.d = [0] * (2 * self.n - 1)
        self.d[self.n - 1:self.n - 1 + len(a)] = a
        for i in reversed(range(self.n - 1)):
            self.d[i] = self.f(self.d[2 * i + 1], self.d[2 * i + 2])

    def update(self, i, v):
        i += self.n - 1
        self.d[i] = v
        while i:
            i = (i - 1) // 2
            self.d[i] = self.f(self.d[2 * i + 1], self.d[2 * i + 2])

    def query(self, l, r):
        assert l <= r
        l += self.n
        r += self.n
        s = self.d[l - 1]
        while l < r:
            if l & 1:
                s = self.f(s, self.d[l - 1])
                l += 1
            if r & 1:
                r -= 1
                s = self.f(s, self.d[r - 1])
            l //= 2
            r //= 2
        return s

def main():
    N = int(input())
    H = list(map(int, input().split()))
    A = list(map(int, input().split()))
    S = SegTree([0] * N, max)

    for h, a in zip(H, A):
        S.update(h - 1, S.query(0, h - 1) + a)
    return S.query(0, N)

print(main())


### FIXED CODE ###

class SegTree:
    def __init__(self, a, func):
        self.n = 2**(len(a) - 1).bit_length()
        self.f = func
        self.d = [0] * (2 * self.n - 1)
        self.d[self.n - 1:self.n - 1 + len(a)] = a
        for i in reversed(range(self.n - 1)):
            self.d[i] = self.f(self.d[2 * i + 1], self.d[2 * i + 2])

    def update(self, i, v):
        i += self.n - 1
        self.d[i] = v
        while i:
            i = (i - 1) // 2
            self.d[i] = self.f(self.d[2 * i + 1], self.d[2 * i + 2])

    def query(self, l, r):
        assert l <= r
        l += self.n
        r += self.n
        s = self.d[l - 1]
        while l < r:
            if l & 1:
                s = self.f(s, self.d[l - 1])
                l += 1
            if r & 1:
                r -= 1
                s = self.f(s, self.d[r - 1])
            l //= 2
            r //= 2
        return s

def main():
    N = int(input())
    H = list(map(int, input().split()))
    A = list(map(int, input().split()))
    S = SegTree([0] * N, max)

    for h, a in zip(H, A):
        S.update(h - 1, S.query(0, h - 1) + a)
    return S.query(0, N)

print(main())
### BUGGY CODE ###

import sys
input = sys.stdin.readline

INF = 10 ** 18

N, W = map(int, input().split())
lst = [INF] * (10 **5 + 10)
lst[0] = 0
for _ in range(N):
    w, v = map(int, input().split())
    for i in range(10 ** 5 + 9, v - 1, -1):
        lst[i] = min(lst[i], lst[i - v] + w)
    for i in range(v - 1, -1, -1):
        lst[i] = min(lst[i], lst[i + 1])

for i in range(10 ** 5 + 9, -1, -1):
    if lst[i] <= W:
        print (i)
        exit()


### FIXED CODE ###

import sys
input = sys.stdin.readline

INF = 10 ** 18

N, W = map(int, input().split())
lst = [INF] * (10 **5 + 10)
lst[0] = 0
for _ in range(N):
    w, v = map(int, input().split())
    for i in range(10 ** 5 + 9, v - 1, -1):
        lst[i] = min(lst[i], lst[i - v] + w)
    for i in range(v - 1, -1, -1):
        lst[i] = min(lst[i], lst[i + 1])

for i in range(10 ** 5 + 9, -1, -1):
    if lst[i] <= W:
        print (i)
        exit()
### BUGGY CODE ###

s=input()
x=list(s)
y=[int(i) for i in x]
ans=0
for i in range(len(s)-3):
  for j in range(i+4,len(s)+1):
    if sum(y[i:j])%3==0:
      t=int(s[i:j])
      if t%673==0:
        ans=ans+1
print(ans)

### FIXED CODE ###

s=input()
n=len(s)
t=[0]*2019
t[0]=1
d=1
tkk=0
for k in reversed(range(n)):
  tk=tkk+int(s[k])*d
  tk=tk%2019
  t[tk]=t[tk]+1
  tkk=tk
  d=d*10
  d=d%2019
ans=0
for i in t:
  ans=ans+i*(i-1)//2
print(ans)
### BUGGY CODE ###

def cal():
    x = input()
    i = 0
    while i <= 2*10**3:
        i += 1
        x = x.replace('ST', '', 10**5)
    print(len(x))

cal()


### FIXED CODE ###

x = input()
ans = []

for i in x:
    if i == 'S':
        ans.append(i)
    else:
        if len(ans) == 0 or ans[-1] == 'T':
            ans.append(i)
        else:
            ans.pop()
print(len(ans))
### BUGGY CODE ###

from fractions import gcd
K = int(input())
ans = 0

for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
            ans += gcd(gcd(a, b), c)
print(ans)

### FIXED CODE ###

from math import gcd
K = int(input())
ans = 0

#すべての要素が等しい場合
for i in range(1,K+1):
    ans += i

#2要素が等しい場合(3*2通り)
for i in range(1,K):
    for j in range(i+1,K+1):
        ans += 6*gcd(i,j)
        
#すべての要素が異なる場合(6通り)
for i in range(1,K-1):
    for j in range(i+1,K):
        for k in range(j+1,K+1):
            ans += 6*gcd(gcd(i,j),k)


print(ans)
### BUGGY CODE ###

_=int(input())
val=1
for i in map(int,input().split()):
  val*=i
if val>10**18:
  print(-1)
  exit()

print(val)

### FIXED CODE ###

_=input()
val=1
t=10**18

for i in map(int,input().split()):
  if i==0:
    val*=i
    break
  if val>=t: continue
  val*=i

if val>t:
  print(-1)
  exit()

print(val)