CodeEff/ECCO huggingface dataset:
# Original solution
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,K=map(int,input().split())
    N+=2
    H=[0]+list(map(int,input().split()))+[0]
    edge=[[] for _ in range(N*(K+1))]
    for i in range(N):
        for j in range(K+1):
            for k in range(K+1-j):
                if i+k+1<N and N-1-i>=K-(k+j):
                    edge[i*(K+1)+j].append((max(H[i+k+1]-H[i],0),(i+k+1)*(K+1)+j+k))
    from heapq import heappop,heappush
    def dijkstra(start,n,edges):
        d=[INF]*n
        used=[False]*n
        d[start]=0
        used[start]=True
        edgelist=[]
        for edge in edges[start]:
            heappush(edgelist,edge)
        while edgelist:
            minedge=heappop(edgelist)
            if used[minedge[1]]:
                continue
            v=minedge[1]
            d[v]=minedge[0]
            used[v]=True
            for edge in edges[v]:
                if not used[edge[1]]:
                    heappush(edgelist,(edge[0]+d[v],edge[1]))
        return d
    d=dijkstra(0,N*(K+1),edge)
    print(min(d[(N-1)*(K+1):]))

if __name__ == '__main__':
    main()


# Optimized solution
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,K=map(int,input().split())
    N+=1
    H=[0]+list(map(int,input().split()))+[0]
    dp=[[INF]*(K+1) for _ in range(N+1)]
    dp[0][0]=0
    for i in range(N):
        for j in range(K+1):
            for k in range(j,K+1):
                if i-(k-j)>=0:
                    dp[i+1][k]=min(dp[i+1][k],dp[i-(k-j)][j]+max(H[i+1]-H[i-(k-j)],0))
                    pass
    print(dp[-1][-1])

if __name__ == '__main__':
    main()
# Original solution
N, K = list(map(int, input().split()))
H = [0] + [int(a) for a in input().split()]
X = [[1<<100] * (N-K+1) for _ in range(N+1)]
X[0][0] = 0
for i in range(1, N+1):
    for j in range(1, min(i+1, N-K+1)):
        X[i][j] = min([X[ii][j-1] + max(H[i] - H[ii], 0) for ii in range(i)])

print((min([x[-1] for x in X])))


# Optimized solution
N, K = list(map(int, input().split()))
H = [0] + list(map(int, input().split()))
dp = [[10 ** 100] * (N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for x in range(1, N+1):
    for y in range(1, min(x+1, N-K+1)):
        dp[x][y] = min([dp[i][y-1] + max(H[x] - H[i], 0) for i in range(x)])
print((min([dp[i][N-K] for i in range(N+1)])))
# Original solution
from functools import lru_cache

n, k = list(map(int, input().split()))
h = [0] + list(map(int, input().split())) + [0]
INF = 10 ** 18


@lru_cache(maxsize=10**9)
def solve(i, nokori):
    if i == 0:
       return 0
    res = INF
    for j in range(nokori + 1):
        if i - 1 - j < 0:
            break
        diff = max(h[i] - h[i - 1 - j], 0)
        res = min(solve(i - 1 - j, nokori - j) + diff, res)
    return res


print((solve(n + 1, k)))

# Optimized solution
from functools import lru_cache

n, k = list(map(int, input().split()))
h = [0] + list(map(int, input().split())) + [0]
INF = 10 ** 18


@lru_cache(maxsize=None)
def solve(i, nokori):
    if i == 0:
       return 0
    res = INF
    for j in range(nokori + 1):
        if i - 1 - j < 0:
            break
        diff = max(h[i] - h[i - 1 - j], 0)
        res = min(solve(i - 1 - j, nokori - j) + diff, res)
    return res


print((solve(n + 1, k)))
# Original solution

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 




n,k = [int(i) for i in readline().split()]
a = [0]+[int(i) for i in readline().split()] + [0]

n += 2

INF  = 10**18

diff = [0]
for i in range(n-1):
    diff.append(max(a[i+1]-a[i],0))

from itertools import accumulate
acc = list(accumulate(diff))

ans = {}

def calc(p,q,k):
    if (p,q,k) in ans: return ans[(p,q,k)]
    if k == 0:
        res = acc[q] - acc[p]
    elif k == q-p-1:
        res = max(0,a[q] - a[p])
    else:
        res = INF
        for i in range(p+1,q):
            for l in range(min(k+1,i-p)):
                if k-l > q-p-1: break
                #if p ==1 and q == 4 and k == 1: print(i,l)
                res = min(res, calc(p,i,l)+calc(i,q,k-l))
    #print(p,q,k,res)    
    ans[(p,q,k)] = res    
    return res

#print(acc)
#print(a,n)
print((calc(0,n-1,k)))
#print(calc(0,n-1,k))



        





# Optimized solution

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 




n,k = [int(i) for i in readline().split()]
a = [0]+[int(i) for i in readline().split()]

n += 1

INF = 10**18

K = n-k-1

dp = [[INF]*n for _ in range((K+1))]
dp[0][0] = 0



for i in range(1,n):
    ndp = [c[:] for c in dp]
    
    for l in range(min(i,K)):
        for now in range(0,i):
            ndp[l+1][i] = min(ndp[l+1][i], ndp[l][now]+max(0,a[i]-a[now]))
    
    dp = ndp
    #print(dp)    

#print(dp[-1])
print((min(dp[-1])))
# Original solution
N, K = list(map(int, input().split()))
D = [Di for Di in input().split()]
for n in range(N, 1000000000):
    n = str(n)
    flag = True
    for i in range(len(str(n))):
        if n[i] in D:
            flag = False
            break
    if flag is True:
        print(n)
        exit()

# Optimized solution
N, K = list(map(int, input().split()))
D = [Di for Di in input().split()]

for n in range(N, 1000000000):
    n = str(n)
    flag = True
    for ni in n:
        if ni in D:
            flag = False
            break
    if flag:
        print(n)
        exit()
# Original solution
n, k = list(map(int, input().split()))
list_D = set(map(str, input().split()))
num = n
ans = -1

while True:
    S = set(list(str(num)))
    cnt = 0
    for s in S:
        if s  in list_D:
           cnt += 1
    if cnt == 0:
        ans = num
        break
    num += 1

print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
list_D = list(map(str, input().split()))
num = n
ans = -1

while True:
    S = list(str(num))
    cnt = 0
    for s in S:
        if s  in list_D:
           cnt += 1
    if cnt == 0:
        ans = num
        break
    num += 1

print(ans)
# Original solution
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, D: "List[int]"):    
    while True:
        for d in D:
            strn=str(N)
            if str(d) in strn:
                break
        else:
            print(N)
            return 
        N+=1

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = [int(next(tokens)) for _ in range(K)]  # type: "List[int]"
    solve(N, K, D)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, D: "List[int]"):    
    D = list(map(str,D))
    while True:
        for d in D:
            if d in str(N):
                N += 1
                break
        else:
            print(N)
            break

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    D = [int(next(tokens)) for _ in range(K)]  # type: "List[int]"
    solve(N, K, D)

if __name__ == '__main__':
    main()
# Original solution
total, k = list(map(int, input().split()))
ds = list(input().split())

ans = total
while True:
  flag = False
  for d in ds:
    if d in list(str(ans)):
      flag = True
  if flag:
    ans += 1
  else:
    print(ans)
    exit(0)

# Optimized solution
total, k = list(map(int, input().split()))
ds = list(input().split())

for i in range(total,total*10+2):
  for d in ds:
    if d in list(str(i)):
      break
  else:
    print(i)
    exit(0)
# Original solution
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

for n in range(N, 100000):
    s = str(n)
    ok = True
    for c in s:
        if c in D:
            ok = False
    if ok:
        print(n)
        break

    
    

# Optimized solution
N, K = list(map(int, input().split()))
D = set(input().split())

for n in range(N, N * 10 + 1):
    ok = True
    for c in str(n):
        if c in D:
            ok = False
    if ok:
        print(n)
        break
# Original solution
n, k = list(map(int, input().split()))

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

n_list = list(str(n))
n_list = [int(num) for num in n_list]

num = {1,2,3,4,5,6,7,8,9,0}

d_set = set(d)
empty = set()
#print(set(n_list))
#print(d_set)
#print(set(n_list) & d_set)
while(1):
  n_list = list(str(n))
  n_list = [int(num) for num in n_list]
  if(set(n_list) & d_set == empty):
    print(n)
    break
  n += 1
  


# Optimized solution
import sys
N, K = list(map(int, input().split()))


d = list(map(int, input().split()))
d_set = set(d)

num_set = set([1,2,3,4,5,6,7,8,9,0])
enable = num_set - d_set
#print(type(enable))
#print(enable)


def dfs(s):
  #print(s)
  global ans
  # すでにN以上の場合はこの先を探索しない
  if s != "":
    if int(s) >= N:
      #print(s)
      ans.append(int(s))
      return

  for num in enable:
    if len(s) == 0 and num == 0:
      continue
    else:
      (dfs(s+str(num)))
    
  return

ans = []
dfs("")
#print(ans)
print((min(ans)))
# Original solution
kingaku, n = input().split()
no_num = input().split()
result = []
ok_num = []
kuriage_num = [-1, 0]

def kuriage(l):
    for i in ok_num:
        if result[l] < i:
            kuriage_num = [l, i]
            return
    if l <= 0:
        return
    return kuriage(l-1)

#使える数字のリストを作成
for i in range(10):
    if not str(i) in no_num:
        ok_num.append(str(i))

set_flg = False
for i in kingaku:
    set_flg = True
    for j in ok_num:
        if i == j:
            set_flg = False
            result.append(j)
            break
        elif i < j:
            result.append(j)
            print(("".join(result).ljust(len(kingaku), ok_num[0])))
            exit()
            break
    
    if set_flg:
        n = 0
        if ok_num[0] == '0':
            n = 1
        print((ok_num[n].ljust(len(kingaku)+1, ok_num[0])))
        exit()

print((''.join(result)))


# Optimized solution
kingaku, n = input().split()
no_num = input().split()
ok_num = []
result = []

for i in range(10):
    if not str(i) in no_num:
        ok_num.append(str(i))

set_flg = False
for i in kingaku:
    set_flg = True
    for j in ok_num:
        if i == j:
            set_flg = False
            result.append(j)
            break
        elif i < j:
            result.append(j)
            print(("".join(result).ljust(len(kingaku), ok_num[0])))
            exit()
            break
    
    if set_flg:
        n = 0
        if ok_num[0] == '0':
            n = 1
        print((ok_num[n].ljust(len(kingaku)+1, ok_num[0])))
        exit()

print((''.join(result)))
# Original solution
N, L = list(map(int, input().split()))
S = {int(x) for x in input().split()}
flag = False
while flag == False:
    if N%10 in S:
        N += 1
        continue
    if int((N%100-N%10)/10) in S and N >= 10:
        N += 1
        continue
    if int((N%1000-N%100)/100) in S and N >= 100:
        N += 1
        continue
    if int((N%10000-N%1000)/1000) in S and N >= 1000:
        N += 1
        continue
    if int((N%100000-N%10000)/10000) in S and N >= 10000:
        N += 1
        continue
    flag = True
print(N)


# Optimized solution
N, L = list(map(int, input().split()))
S = {int(x) for x in input().split()}
flag = False
while 1:
    if N%10 in S:
        N += 1
        continue
    if int((N%100-N%10)/10) in S and N >= 10:
        N += 1
        continue
    if int((N%1000-N%100)/100) in S and N >= 100:
        N += 1
        continue
    if int((N%10000-N%1000)/1000) in S and N >= 1000:
        N += 1
        continue
    if int((N%100000-N%10000)/10000) in S and N >= 10000:
        N += 1
        continue
    break
print(N)
# Original solution
N, K = list(map(int, input().split()))
D = set(map(str, input().split()))

for i in range(N, 100000):
    a = list(str(i))
    a = set(a)
    if a & D == set():
        print(i)
        break


# Optimized solution
N, K = list(map(int, input().split()))
D = set(list(input().split()))

while True:
    if set(list(str(N))) & D:
        N += 1
    else:
        print(N)
        exit()
# Original solution
Num = [int(n) for n in input().rstrip().split()]

D = [int(n) for n in input().rstrip().split()]

for i in range(Num[0],100000):
    if len(set([str(n) for n in D]) & set(str(i))) == 0:
        print((int(i)))
        break
    



# Optimized solution
Num = [int(n) for n in input().rstrip().split()]

D = [int(n) for n in input().rstrip().split()]

# numbers = [ n for n in range(10) if not n in D]

flag = 0

for i in range(Num[0],10*Num[0]+1):
    for j in str(i):
        if int(j) in D:
            flag = 1
            break

    if flag == 0:
        print(i)
        break
    else:
        flag =0
# Original solution
Num = [int(n) for n in input().rstrip().split()]

D = [int(n) for n in input().rstrip().split()]

# numbers = [ n for n in range(10) if not n in D]

flag = 0

for i in range(Num[0],10*Num[0]+1):
    for j in str(i):
        if int(j) in D:
            flag = 1
            break

    if flag == 0:
        print(i)
        break
    else:
        flag =0 
        

# Optimized solution
N ,K = list(map(int,input().split()))
D = [int(n) for n in input().split()]

flag = False

for i in range(N,100000):
    N = i
    while i > 0:
        flag = True
        if i%10 in D:
            flag = False
            break
        else:
            i //=10

    if flag == True:
        print(N)
        break
# Original solution
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()

def main():
    N, K = list(map(int, input().split()))
    D = set(map(int, input().split()))
    while True:
        check = True
        for c in str(N):
            if int(c) in D:
                check = False
        if check:
            print(N)
            return
        N += 1


if __name__ == "__main__":
    main()


# Optimized solution
import sys
from itertools import product
def input(): return sys.stdin.readline().strip()

def main():
    """
    itertools.productを使うと高速なようなので写経
    参考：https://atcoder.jp/contests/abc042/submissions/16188969
    """
    N, K = list(map(int, input().split()))
    D = set(map(int, input().split()))
    ok = [i for i in range(10) if i not in D]
    l = len(str(N))
    if int(str(ok[-1]) * l) < N:
        if ok[0] == 0:
            print((ok[1] * 10 ** l))
        else:
            print((str(ok[0]) * (l + 1)))
    else:
        for x in product(ok, repeat=l):
            x = int("".join(map(str, x)))
            if x >= N:
                print(x)
                return



if __name__ == "__main__":
    main()
# Original solution
def main():
    N, K = list(map(int, input().split()))
    D = set(map(int, input().split()))
    numset = set(range(0,10))
    d = numset.difference(D)

    for n in range(N, pow(10, 6) + 10):
        check = set(list(str(n)))
        ngflag = False
        for i in check:
            if int(i) in D:
                ngflag = True
                break
        if not ngflag:
            break
    print(n)

if __name__ == "__main__":
    main()


# Optimized solution
def minN(N:int, usable:list, restrict=True):
    usable.sort()
    keta = False
    if restrict:
        for i in usable:
            if i >= N:
                return str(i)
        # 桁が増える
        return '1'+str(usable[0])
    else:
        return str(usable[0])

def rote(N:list, D:set, d:set):
    ans = []
    flag = True
    lenNstr = len(N)
    for i, n in enumerate(N):
        n = int(n)
        keta = 10**(len(N) - i - 1)
        if flag:
            if n in D:
                ans.append(int(minN(n, d)) * keta)
                flag = False
            else:
                ans.append(keta * n)
        else:
            ans.append(keta * min(d))
    return sum(ans)

def main():
    N, K = list(map(int, input().split()))
    D = set(map(int, input().split()))
    numset = set(range(0,10))
    d = list(numset.difference(D))
    d.sort()
    for _ in range(10):
        Nstr = list(str(N))
        ans = rote(Nstr, D, d)
        N = ans    
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
n, k = list(map(int, input().split()))
d_array = [int(x) for x in input().split()]

for i in range(n, 100000):
    flag = True
    price = str(i)
    for d in d_array:
        str_d = str(d)
        if str_d in price:
            flag = False
    else:
        if flag:
            print(i)
            exit()

            

# Optimized solution
n, k = list(map(int, input().split()))
d_array = [str(x) for x in input().split()]
price = 0 
while True:
    flag = True
    for s in str(price):
        if s in d_array:
            flag = False
    if price >= n and flag:
        print(price)
        break
    price+=1
# Original solution
def main():
    n, k = list(map(int, input().split()))
    D = list(map(int, input().split()))

    while 1:
        f = 0
        hoge = str(n)
        for i in range(len(hoge)):
            for j in range(k):
                if(int(hoge[i]) == D[j]):
                    f = 1
                    break
                if f == 1:
                    break
            if f == 1:
                break
        if f == 0:
            print(n)
            break
        n += 1

if __name__ == "__main__":
    main()

# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    D = list(input().split())

    i = 0
    while True:
        S = str(N)
        f = True
        for s in S:
            if s in D:
                f = False
                break
        if f:
            break
        N += 1
    print(N)
    
if __name__ == "__main__":
    main()
# Original solution
def getN():
    return int(eval(input()))
def getNM():
    return list(map(int, input().split()))
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(eval(input())) for i in range(intn)]

from collections import defaultdict, deque
from sys import exit
import heapq
import math
import copy
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
# list(map(int, input().split()))
N, K = getNM()
intlist = [i for i in range(10)]
D = getList()
for i in D:
    if i in D:
        intlist.pop(intlist.index(i))

anslist = []
def dfs(i, num):
    anslist.append(num)
    if i < 5:
        for j in intlist:
            newnum = int(str(num) + str(j))
            dfs(i + 1, newnum)

for i in intlist:
    if i != 0:
        dfs(1, i)

anslist.sort()

for i in anslist:
    if i >= N:
        ans = i
        break
print(ans)

# Optimized solution
def getN():
    return int(eval(input()))
def getNM():
    return list(map(int, input().split()))
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(eval(input())) for i in range(intn)]

from collections import defaultdict, deque
from sys import exit
import heapq
import math
import copy
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)

N, K = getNM()
intlist = [i for i in range(10)]
D = getList()
for i in D:
    if i in D:
        # 嫌いな数字を消す
        intlist.pop(intlist.index(i))

anslist = []
# intlist内の数字でできる５桁以内の数字を全列挙（５桁の数字も列挙するのは無駄な気もする）
# dfsで解いてる人はあんまりいない
# 基本的に全探索
def dfs(i, num):
    anslist.append(num)
    # num < Nの条件で計算量を制限
    if i < 5 and num < N:
        for j in intlist:
            newnum = int(str(num) + str(j))
            dfs(i + 1, newnum)

for i in intlist:
    if i != 0:
        dfs(1, i)

anslist.sort()

for i in anslist:
    if i >= N:
        ans = i
        break
print(ans)
# Original solution
from itertools import product
n,k = list(map(int, input().split()))
d = list(map(int, input().split()))

nums = []
for i in range(10):
  if i not in d: nums.append(i)

for i in range(len(str(n)), 6):
  for j in product(nums, repeat=i):
    t = ""
    if j[0] != 0:
      for k in j:
        t += str(k)
      if int(t) >= n:
        print(t)
        exit()
    

# Optimized solution
from itertools import product
n,k = list(map(int, input().split()))
d = list(map(int, input().split()))

nums = []
for i in range(10):
  if i not in d: nums.append(i)

m = str(n)
for i in range(len(m), len(m)+2):
  for j in product(nums, repeat=i):
    ans = ""
    for k in j: ans += str(k)
    if int(ans) >= n:
      print(ans)
      exit()
# Original solution
from itertools import product
n,k = list(map(int, input().split()))
d = list(map(int, input().split()))

nums = []
for i in range(10):
  if i not in d: nums.append(i)

m = str(n)
for i in range(len(m), len(m)+2):
  for j in product(nums, repeat=i):
    ans = ""
    for k in j: ans += str(k)
    if int(ans) >= n:
      print(ans)
      exit()

# Optimized solution
from itertools import product
n,k = list(map(int, input().split()))
d = list(map(int, input().split()))

nums = []
for i in range(10):
  if i not in d: nums.append(i)

m = len(str(n))
for i in range(m, m+2):
  for j in product(nums, repeat=i):
    ans = ""
    for k in j: ans += str(k)
    if int(ans) >= n:
      print(ans)
      exit()
# Original solution
N, K = list(map(int,input().split()))

D = list(map(int,input().split()))
OK = []
for i in range(10):
  if i not in D:
    OK.append(str(i))
#print(OK)
sn = str(N)
l = len(sn)

ans = N*1
Flag = 0
while Flag == 0:
  sans = str(ans)
  if all(sans[i] in OK for i in range(len(sans))):
    Flag = 1
  else:
    ans += 1
print(ans)
      
    

# Optimized solution
N,K = list(map(int,input().split()))
A = set(map(int,input().split()))
#A = set(A)
ok = []
for i in range(10):
  if i not in A:
    ok.append(i)
#print(ok)
while True:
  nstr = str(N)
  nstr = list(nstr)
  #print(nstr)
  Flag = True
  for x in nstr:
    x = int(x)
    if x in A:
      Flag = False
      break
  #print(Flag)
  if Flag:
    print(N)
    exit()
  else:
    N += 1
# Original solution
N,K=list(map(int,input().split()))
D=set([int(i) for i in input().split()])

while any(int(j) in D for j in str(N)):
    N+=1

print(N)




# Optimized solution
N, K = list(map(int, input().split()))
D = [i for i in input().split()]

while True:
    if all(n not in D for n in str(N)):
        print(N)
        exit()
    else:
        N += 1
# Original solution
n,k= list(map(int, input().split()))
d= list(input().split())
for i in range(n,10*n+1):
    x=str(i)
    ans=True
    for j in range(len(x)):
        if x[j] in d:
            ans=False
    if ans:
        print(i)
        break


# Optimized solution
n,k=list(map(int,input().split()))
d=list(input().split())
for i in range(n,10*n):
    a=str(i)
    b=len(a)
    ans=True
    for j in a:
        if j in d:
            ans=False
            break 
    if ans:
        print(i)
        exit()