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

import collections,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
A = sorted(LI())
ans_bool = [0]*(10**6+1)
for a in A:
    num = a
    while num<=10**6:
        ans_bool[num] += 1
        num += a
ans = 0
for a in A:
    if ans_bool[a]==1:
        ans += 1
print(ans)


### FIXED CODE ###

import collections,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
A = sorted(LI())
counter_A = collections.Counter(A)
sequence = [0]*(10**6+1)
ans = 0
for k,v in counter_A.items():
    if sequence[k]==1:
        continue
    if v==1:
        ans += 1
    for i in range(k,10**6+1,k):
        sequence[i] = 1
print(ans)
### BUGGY CODE ###

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

t = 0
now = 1
footprint = [1]

while t < K:
  if A[now-1] not in footprint:
    footprint.append(A[now-1])
    now = A[now-1]
    t += 1
    continue
  else:
    s = footprint.index(A[now-1])
    l = t - s + 1
    e = (K-s) % l
    now = footprint[s+e]
    break

print(now)

### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
 
t = 0
now = 1
footprint = [1]
past = [0]*N
past[0] = 1

while t < K:
  if past[A[now-1]-1] == 0:
    now = A[now-1]
    footprint.append(now)
    past[now-1]=1
    t += 1
  else:
    s = footprint.index(A[now-1])
    l = t - s + 1
    e = (K-s) % l
    now = footprint[s+e]
    break
    
print(now)
### BUGGY CODE ###

n = int(input())
c = list(input())
sc = list(reversed(c))
ans = 0
flg = True

r = 0
l = 0

if 'W' not in set(c) or 'R' not in set(c):
    flg = False


while(flg):

    l = 0
    for i in range(l, n):
        r = 0
        if not flg:
            break

        if c[i] == 'W':
            for j in range(r, n):
                if sc[j] == 'R':
                    if len(c) >= i+j+2:
                        c[i] = 'R'
                        c[-1*j-1] = 'W'
                        ans += 1
                        sc = list(reversed(c))
                        break
                    else:
                        flg = False
                        break

                else:
                    r += 1
        else:
            l += 1
        
        if l >= n-1:
            flg = False
            break


print(ans)



### FIXED CODE ###

n = int(input())
c = list(input())
sc = list(reversed(c))
ans = 0
r = 0
flg  = False

if 'W' not in set(c) or 'R' not in set(c):
    print(ans)


else:
    for i in range(n):
        if flg:
            break
        if c[i] == 'W':
            for j in range(r, n):
                r += 1
                if sc[j] == 'R':
                    if n >= i+j+2:
                        c[i] = 'R'
                        c[-1*j-1] = 'W'
                        ans += 1
                        break
                    else:
                        flg = True
                        break

                
            

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

n = int(input())


def check(n, k):
    while n % k == 0:
        n = n // k
    if n >= k:
        n = n - k * (n // k)
    return n


def count(n):
    total = 0
    cands = set([n, n-1])
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            cands.add(i)
            cands.add(n // i)
    for i in range(2, int(n ** 0.5) + 1):
        if (n-1) % i == 0:
            cands.add(i)
            cands.add((n - 1) // i)
    for k in cands:
        if check(n, k) == 1:
            total += 1
    return total


print(count(n))

### FIXED CODE ###

n = int(input())


def check(n, k):
    while n % k == 0:
        n = n // k
    if n >= k:
        n = n - k * (n // k)
    return n


def count(n):
    total = 0
    cands = set([n, n-1])
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            cands.add(i)
            cands.add(n // i)
    for i in range(2, int(n ** 0.5) + 1):
        if (n-1) % i == 0:
            cands.add(i)
            cands.add((n - 1) // i)
    for k in cands:
        if k == 1:
            continue
        if check(n, k) == 1:
            total += 1
    return total


print(count(n))
### BUGGY CODE ###

import collections as cl

h,w = map(int, input().split())
a = [list(input()) for _ in range(h)]

ds = [[-1]*w for _ in range(h)]

vectors = [(-1,0),(1,0),(0,-1),(0,1)]

stack = cl.deque([])

ans = 0

visited = [[False]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            stack.appendleft((i,j))
            ds[i][j] = 0

while stack:
    nh,nw = stack.pop()
    for vh,vw in vectors:
        if nh+vh>=0 and nh+vh<h and nw+vw>=0 and nw+vw<w:
            if ds[nh+vh][nw+vw] == -1:
                stack.appendleft((nh+vh,nw+vw))
                ds[nh+vh][nw+vw] = ds[nh][nw] + 1
                ans = ds[nh][nw] + 1

print(ans)


### FIXED CODE ###

import numpy as np
import copy

h, w = map(int, input().split())
a = [input() for _ in range(h)]

a = np.array([[0 if (ai[j] == "#") else np.inf for j in range(w)] for ai in a])

for i in range(1, h):
    a[i] = np.minimum(a[i], a[i - 1] + 1)
for i in range(h - 2, -1, -1):
    a[i] = np.minimum(a[i], a[i + 1] + 1)
for i in range(1, w):
    a[:, i] = np.minimum(a[:, i], a[:, i - 1] + 1)
for i in range(w - 2, -1, -1):
    a[:, i] = np.minimum(a[:, i], a[:, i + 1] + 1)

print(int(a.max()))
### BUGGY CODE ###

import itertools as itr

N, M = map(int, input().split())
A = [0] * M
B = [0] * M
C = []
for m in range(M):
    A[m], B[m] = map(int, input().split())
    C.append(set([int(cm) for cm in input().split()]))

INF = (10**5) * (10**3)
dp = [[INF] * 2**N for _ in range(M + 1)]
for m in range(M + 1):
    dp[m][0] = 0


def Set2Bit(st):
    bt = ["0" for _ in range(N)]
    for i in st:
        bt[i-1] = "1"
    return bt


for m in range(0, M):
    key = Set2Bit(C[m])
    for bit in itr.product("01", repeat=N):
        pre_bit = next_bit = int("".join(bit), 2)
        dp[m+1][next_bit] = dp[m][pre_bit]
    for bit in itr.product("01", repeat=N):
        pre_bit = int("".join(bit), 2)
        key_bit = int("".join(key), 2)
        next_bit = pre_bit | key_bit
        dp[m+1][next_bit] = min(dp[m+1][next_bit], dp[m][next_bit], dp[m][pre_bit] + A[m])

if dp[-1][-1] == INF:
    print(-1)
else:
    print(dp[-1][-1])


### FIXED CODE ###

N, M = map(int, input().split())
A = [0] * M
B = [0] * M
C = [0]*M
for m in range(M):
    A[m], B[m] = map(int, input().split())
    C[m] = [int(cm) for cm in input().split()]

INF = 100000000
dp = [[INF] * 2**N for _ in range(M + 1)]
dp[0][0] = 0


def Set2Bit(st):
    bt = ["0"]*N
    for i in st:
        bt[i-1] = "1"
    return bt


for m in range(0, M):
    key = Set2Bit(C[m])
    key_bit = int("".join(key), 2)
    for bit in range(2**N):
        pre_bit = next_bit = bit
        dp[m+1][next_bit] = dp[m][pre_bit]
    for bit in range(2**N):
        pre_bit = bit
        next_bit = pre_bit | key_bit
        dp[m+1][next_bit] = min(dp[m+1][next_bit], dp[m][next_bit], dp[m][pre_bit] + A[m])

if dp[-1][-1] == INF:
    print(-1)
else:
    print(dp[-1][-1])
### BUGGY CODE ###

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

for i in range(k):
  if a>=1:
    a-=1
  elif b>=1:
    b-=1
  else:
    continue

print(a,b)

### FIXED CODE ###

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

if a-k>0:
  a -= k
  print(a,b)
  exit()
else:
  k -= a
  a = 0
if b-k>0:
  b -=k
  print(a,b)
  exit()
else:
  print(a,0)
### BUGGY CODE ###

#!/usr/bin/env python

import sys

MOD=10**9 + 7

def Add(n,m):
    return (n+m)%MOD

def Sub(n,m):
    return (n-m)%MOD

def Mul(n,m):
    return (n*m)%MOD

def Pow(n,p):
    if p<0: return Pow(n,  ( p % ( MOD-1 ) ) )
    elif p==0: return 1
    elif p==1: return n
    elif p%2==0:
        tmp= Pow( n,(p//2) ) % MOD
        return tmp**2 % MOD
    else:
        tmp= Pow( n,(p//2) ) % MOD
        return ( n * tmp**2 ) % MOD
    #
    raise ValueError

def Div(n,m):
    return ( n * Pow(m,MOD-2) ) % MOD
#
#::::::::::::::::::::::::::::::::::::::::::::::::::
n,a,b,c = [ int(x) for x in sys.stdin.readline().split() ]
#n,a,b,c = 100000, 31, 41, 28
#
h= Div(1,100)
pa= Mul(a,h)
pb= Mul(b,h)
cc= Div( 1,Mul(100-c,h) )
# power of n
cn= Pow(cc,n+1)
t = Mul(cn,n)
an= Mul(Pow(pa,n),t)
bn= Mul(Pow(pb,n),t)
# for A
pam= bn
pac= Mul(pa,cc)
# for B
pbm= an
pbc= Mul(pb,cc)
# 
ret= 0
ncm= 1
for m in range(n):
    if m>0:
        ncm= Mul(ncm,n+m)
        if ncm%m==0: ncm//=m
        else: ncm= Div(ncm,m)
        pam= Mul(pam,pac)
        pbm= Mul(pbm,pbc)
    #
    ret= ( ret + Mul(ncm,pbm+pam) ) % MOD
#
print( ret )

### FIXED CODE ###

#!/usr/bin/env python

import sys

MOD=10**9 + 7

def Add(n,m):
    return (n+m)%MOD

def Sub(n,m):
    return (n-m)%MOD

def Mul(n,m):
    return (n*m)%MOD

def Pow(n,p):
    if p<0: return Pow(n,  ( p % ( MOD-1 ) ) )
    elif p==0: return 1
    elif p==1: return n
    elif p%2==0:
        tmp= Pow( n,(p//2) ) % MOD
        return tmp**2 % MOD
    else:
        tmp= Pow( n,(p//2) ) % MOD
        return ( n * tmp**2 ) % MOD
    #
    raise ValueError

def Div(n,m):
    return ( n * Pow(m,MOD-2) ) % MOD
#
#::::::::::::::::::::::::::::::::::::::::::::::::::
n,a,b,c = [ int(x) for x in sys.stdin.readline().split() ]
#
h= Div(1,100)
pa= Mul(a,h)
pb= Mul(b,h)
cc= Div( 1,Mul(100-c,h) )
# power of n
cn= Pow(cc,n+1)
t = Mul(cn,n)
an= Mul(Pow(pa,n),t)
bn= Mul(Pow(pb,n),t)
# for A
pam= bn
pac= Mul(pa,cc)
# for B
pbm= an
pbc= Mul(pb,cc)
# permutation
mbar = n
perm= [1]*n
for m in range(1,n):
    mbar-=1
    perm[m]= Mul(perm[m-1],mbar)
perm.reverse()
nfact= perm[0]
#
ret= 0
ncm= 1
for m in range(n):
    if m>0:
        ncm= Mul(ncm,n+m)
        pam= Mul(pam,pac)
        pbm= Mul(pbm,pbc)
    #
    ret= ( ret + Mul(Mul(ncm,pbm+pam),perm[m]) ) % MOD
#
print( Div(ret,nfact) )
### BUGGY CODE ###

import heapq
xs, ys, xt, yt = map(int, input().split())
N = int(input())
C = [(xs, ys, 0), (xt, yt, 0)]
C += [tuple(map(int, input().split())) for i in range(N)]

G = [[] for i in range(N+2)]
for i in range(N+2):
    for j in range(i+1, N+2):
        cost = max(0, ((C[i][0] - C[j][0]) ** 2 + (C[i][1] - C[j][1]) ** 2) ** 0.5 - (C[i][2] + C[j][2]))
        G[i].append((j, cost))
        G[j].append((i, cost))


def dijkstra(x):
    d = [float('inf')] * (N + 2)
    d[x] = 0
    visited = {x}

    # d, u
    hq = [(0, x)]

    while hq:
        u = heapq.heappop(hq)[1]
        visited.add(u)

        for node, cost in G[u]:
            if (node not in visited) and d[node] > d[u] + cost:
                d[node] = d[u] + cost
                if 1 in visited:
                    return d
                heapq.heappush(hq, (d[u]+cost, node))
    return d


dist = dijkstra(0)
print(dist[1])


### FIXED CODE ###

import sys
import heapq
my_input = sys.stdin.readline

def main():
    xs, ys, xt, yt = map(int, my_input().split())
    N = int(my_input())
    C = [(xs, ys, 0), (xt, yt, 0)]
    C += [tuple(map(int, my_input().split())) for i in range(N)]

    G = [[] for i in range(N+2)]
    for i in range(N + 2):
        for j in range(i + 1, N + 2):
            cost = max(0, ((C[i][0] - C[j][0]) ** 2 + (C[i][1] - C[j][1]) ** 2) ** 0.5 - (C[i][2] + C[j][2]))
            G[i].append((j, cost))
            G[j].append((i, cost))


    def dijkstra(graph, start, inf=float('inf')):
        import heapq
        n = len(graph)
        distances = [inf] * n
        distances[start] = 0
        visited = [False] * n

        # 距離・頂点
        hq = [(0, start)]
        while hq:
            dist, fr = heapq.heappop(hq)
            if distances[fr] < dist:
                continue
            if fr == 1:
                return distances
            visited[fr] = True

            for to, cost in graph[fr]:
                new_dist = distances[fr] + cost
                if (visited[to]) or (distances[to] <= new_dist):
                    continue

                distances[to] = new_dist
                heapq.heappush(hq, (new_dist, to))

        return distances


    dist = dijkstra(G, 0)
    print(dist[1])


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

from sys import stdin
readline = stdin.readline

H, N = map(int, readline().split())
AB = [list(map(int, readline().split())) for _ in range(N)]

# 最初に入力の最大値と最大量でdp配列を作成
dp = [100000000] * (H + 10 ** 4 + 1)
dp[0] = 0

for i in range(H):
    if dp[i] == 100000000:
        continue
    for a, b in AB:
        # tは100000000 + 消費魔力
        t = dp[i] + b
        # dp[i + a] aはダメージ量
        if t < dp[i + a]:
            dp[i + a] = t

print(min(dp[H:]))



### FIXED CODE ###

def main():
    from sys import stdin
    readline = stdin.readline
    H, N = map(int, readline().split())
    AB = [list(map(int, readline().split())) for _ in range(N)]

    dp = [100000000] * (H + 10 ** 4 + 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == 100000000:
            continue
        for a, b in AB:
            t = dp[i] + b
            if t < dp[i + a]:
                dp[i + a] = t
    print(min(dp[H:]))


main()
### BUGGY CODE ###

n = int(input())
s = input()
count = 0
for i in range(1,n-1):
    for j in range(1,n):
        for k in range(1,n+1):
            if not j-i == k-j:
                if i<j<k:
                    if not s[i-1] == s[j-1]:
                        if not s[i-1] == s[k-1]:
                            if not s[j-1] == s[k-1]:
                                count +=1
print(count)

### FIXED CODE ###

n = int(input())
s = input()
all = s.count("R")*s.count("G")*s.count("B")
for x in range(n-1):
    for y in range(1,(n-1-x)//2+1):
        if s[x] != s[x+y] and s[x]!= s[x+2*y]\
        and s[x+y]!=s[x+2*y]:
            all -=1
print(all)
### BUGGY CODE ###

A,B=map(int,input().split())
c=0
for a in range(A,B+1):
    c^=a
print(c)

### FIXED CODE ###

A,B=map(int,input().split())
def calc(A):
    if A%4==0:
        return A
    elif A%4==1:
        return 1
    elif A%4==2:
        return A+1
    elif A%4==3:
        return 0
print(calc(A-1)^calc(B))
### BUGGY CODE ###

S=raw_input()
L=len(S)
 
M=10**9+7
 
dp=[ [ 0 for i in range(4) ] for k in range(L+1)]
all=1
 
for i in range(L):
	if S[i]=="A":
		dp[i+1][1]=( dp[i][1]+all ) %M
		dp[i+1][2]=dp[i][2]%M
		dp[i+1][3]=dp[i][3]%M
	if S[i]=="B":
		dp[i+1][2]=( dp[i][2]+dp[i][1] )%M
		dp[i+1][3]=dp[i][3]%M
		dp[i+1][1]=dp[i][1]%M
	if S[i]=="C":
		dp[i+1][3]=( dp[i][3]+dp[i][2] )%M
		dp[i+1][2]=dp[i][2]%M
		dp[i+1][1]=dp[i][1]%M
 
	if S[i]=="?":
		dp[i+1][1]=( dp[i][1]*3+all )%M
		dp[i+1][2]=( dp[i][2]*3+dp[i][1] )%M
		dp[i+1][3]=( dp[i][3]*3+dp[i][2] )%M
		all*=3	
 
print ( dp[-1][3] ) %M

### FIXED CODE ###

S=raw_input()

M=10**9+7

all=1
abc=0
ab=0
a=0

for x in S:
	if x=="A":
		a+=all
	if x=="B":
		ab+=a
	if x=="C":
		abc+=ab

	if x=="?":
		abc=abc*3+ab
		ab=ab*3+a
		a= a*3+all
		all*=3

	abc%=M
	ab%=M
	a%=M
	all%=M


print abc%M
### BUGGY CODE ###

if __name__ == "__main__":
    
    n = int(input())

    for o in range(1, n+1):
        for x in range(1, 100):
            isFound = False
            isOver = False
            for y in range(1, x+1):
                for z in range(1, y+1):
                    m = x**2+y**2+z**2+x*y+y*z+z*x
                    if m == o:
                        isFound = True
                        if x==y and y==z: print(1)
                        elif x==y or y==z or z==x: print(3)
                        else: print(6)
                        break
                    elif m > o:
                        isOver = True
                        break
                if isFound or isOver: break
            if isFound: break
        if not isFound: print(0)

### FIXED CODE ###

if __name__ == "__main__":
    
    n = int(input())

    """for o in range(1, n+1):
        s = 1
        if o > 6:
            s = -1 + int((o-2)**0.5)
        for x in range(s, 100):
            isFound = False
            for y in range(1, x+1):
                for z in range(1, y+1):
                    m = x**2+y**2+z**2+x*y+y*z+z*x
                    if m == o:
                        isFound = True
                        if x==y and y==z: print(1)
                        elif x==y or y==z or z==x: print(3)
                        else: print(6)
                        break
                    elif m > o:
                        break
                if isFound: break
            if isFound: break
        if not isFound: print(0)"""

    ans = [0 for _ in range(10001)]
    for x in range(1, 101):
        for y in range(1, 101):
            for z in range(1, 101):
                m = x**2+y**2+z**2+x*y+y*z+z*x
                if m > 10000: break
                ans[m] += 1

    for i in range(n):
        print(ans[i+1])
### BUGGY CODE ###

N = int(input())
c = 1
for i in range(N):
    c = c * (i+1)
print(c% (10**9+7))

### FIXED CODE ###

N = int(input())
c = 1
for i in range(N):
    c = (c * (i+1) )% (10**9+7)
print(c)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N,M = map(int,input().split())
LRC = [tuple(map(int,input().split())) for i in range(M)]
LRC.sort()

INF = float('inf')
class SegTreeMin:
    def __init__(self,arr):
        n = 1
        while n < len(arr):
            n *= 2
        self.n = n
        nodes = [INF]*(2*n-1)
        nodes[n-1:n-1+len(arr)] = arr
        for i in range(n-2,-1,-1):
            nodes[i] = min(nodes[2*i+1],nodes[2*i+2])
        self.nodes = nodes

    # thanks
    # https://smijake3.hatenablog.com/entry/2018/11/03/100133
    def update(self,i,val):
        i += self.n-1
        self.nodes[i] = val
        while i >= 0:
            i = (i - 1) // 2
            self.nodes[i] = min(self.nodes[2*i+1], self.nodes[2*i+2])

    def query(self,l,r):
        L = l + self.n; R = r + self.n
        s = INF
        while L < R:
            if R & 1:
                R -= 1
                s = min(s, self.nodes[R-1])
            if L & 1:
                s = min(s, self.nodes[L-1])
                L += 1
            L >>= 1; R >>= 1
        return s

segt = SegTreeMin([0] + [INF]*N)
for l,r,c in LRC:
    mn = segt.query(l-1,r-1)
    org = segt.query(r-1,r)
    if org > mn+c:
        segt.update(r-1, mn+c)

ans = segt.query(N-1,N)
print(-1 if ans==INF else ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline
N,M = map(int,input().split())
LRC = [tuple(map(int,input().split())) for i in range(M)]
LRC.sort()

INF = float('inf')
class SegTreeMin:
    def __init__(self,arr):
        n = 1
        while n < len(arr):
            n *= 2
        self.n = n
        nodes = [INF]*(2*n-1)
        nodes[n-1:n-1+len(arr)] = arr
        for i in range(n-2,-1,-1):
            nodes[i] = min(nodes[2*i+1],nodes[2*i+2])
        self.nodes = nodes

    # thanks
    # https://smijake3.hatenablog.com/entry/2018/11/03/100133
    def update(self,i,val):
        i += self.n-1
        self.nodes[i] = val
        while i >= 0:
            i = (i - 1) // 2
            self.nodes[i] = min(self.nodes[2*i+1], self.nodes[2*i+2])

    def query(self,l,r):
        L = l + self.n; R = r + self.n
        s = INF
        while L < R:
            if R & 1:
                R -= 1
                s = min(s, self.nodes[R-1])
            if L & 1:
                s = min(s, self.nodes[L-1])
                L += 1
            L >>= 1; R >>= 1
        return s

segt = SegTreeMin([0] + [INF]*N)
for l,r,c in LRC:
    mn = segt.query(l-1,r-1)
    org = segt.query(r-1,r)
    if org > mn+c:
        segt.update(r-1, mn+c)

ans = segt.query(N-1,N)
print(-1 if ans==INF else ans)
### BUGGY CODE ###

from bisect import bisect
import sys

p = 10 ** 9 + 7
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if n != len(set(a)) or m!= len(set(b)):
  print(0)
  sys.exit()
a.sort()
b.sort()

q = 1
for x in range(n*m, 0, -1):
  if x in a and x in b:
    q *= 1
  elif x in a:
    q = q * max(m - bisect(b, x), 0)  % p
  elif x in b:
    q = q * max(n - bisect(a, x), 0) % p
  else:
    u = bisect(a, x)
    v = bisect(b, x)
    q = q * (max((n-u) * (m-v) - (n * m - x), 0) % p) % p
print(q)
    
    
    

### FIXED CODE ###

from bisect import bisect
import sys

p = 10 ** 9 + 7
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if n != len(set(a)) or m!= len(set(b)):
  print(0)
  sys.exit()
A = set(a)
B = set(b)
a.sort()
b.sort()

q = 1
for x in range(n*m, 0, -1):
  if x in A and x in B:
    q *= 1
  elif x in A:
    q = q * max(m - bisect(b, x), 0)  % p
  elif x in B:
    q = q * max(n - bisect(a, x), 0) % p
  else:
    u = bisect(a, x)
    v = bisect(b, x)
    q = q * (max((n-u) * (m-v) - (n * m - x), 0) % p) % p
print(q)
### BUGGY CODE ###

def intlog10(n):
    if n <= 0:
        return -1
    i = 0
    while 10**i <= n:
        i += 1
    return i-1

N, M = map(int, input().split())
A = [int(a) for a in input().split()]
L = [2, 5, 5, 4, 5, 6, 3, 7, 6]
B = [[A[i], L[A[i]-1]] for i in range(M)]

X = [0] + [-10**9] * N

for i in range(1, N+1):
    for b in B:
        if b[1] <= i:
            if X[i-b[1]] == 0:
                X[i] = max(X[i], b[0])
            else:
                X[i] = max(X[i], X[i-b[1]]*10 + b[0], X[i-b[1]] + b[0] * (10**intlog10(X[i-b[1]])))
    
print(X[N])


### FIXED CODE ###

def intlog10(n):
    if n <= 0:
        return -1
    i = 0
    while 10**i <= n:
        i += 1
    return i-1

N, M = map(int, input().split())
A = [int(a) for a in input().split()]
    
L = [2, 5, 5, 4, 5, 6, 3, 7, 6]
B = [[A[i], L[A[i]-1]] for i in range(M)]



best = sorted(B, key = lambda x: x[1]*10-x[0])[0]

adj = 0
if N > 500:
    adj = (N - 500)//best[1]
    N -= best[1] * adj

X = [0] + [-10**9] * N
bpre = -1
ll = 1
for i in range(1, N+1):
    if i > 50 and i < N-50:
        X[i] = X[i-best[1]]*10 + best[0]
    else:
        for b in B:
            if b[1] <= i:
                if X[i-b[1]] == 0:
                    if b[0] > X[i]:
                        X[i] = b[0]
                        bpre = b
                        ll = 0
                else:
                    if X[i-b[1]]*10 + b[0] < X[i-b[1]] + b[0] * (10**intlog10(X[i-b[1]])):
                        lll = 1

                    if max(X[i-b[1]]*10 + b[0], X[i-b[1]] + b[0] * (10**intlog10(X[i-b[1]]))) > X[i]:
                        X[i] = max(X[i-b[1]]*10 + b[0], X[i-b[1]] + b[0] * (10**intlog10(X[i-b[1]])))
                        ll = lll
                        bpre = b
                

ans = str(X[N])
if adj > 0:
    ans = ans[:50] + str(best[0]) * adj + ans[50:]

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

# D - Gathering Children
import re

s = input()

# 同じ文字が連続していると端のマスに子供が集約される（Rなら右端、Lなら左端）
# 集約が完了したらあとは何回やってもRL間での位置の入れ替えのみ
# → 集約完了回数分は計算、残りの回数が奇数回なら追加で1回試行
rMatch = re.findall('R{1,}', s)
lMatch = re.findall('L{1,}', s)
rMatch.sort(reverse=True)
lMatch.sort(reverse=True)
integrateCnt = len(rMatch[0]) if len(rMatch[0]) > len(lMatch[0]) else len(lMatch[0])
addCnt = (10**100 - integrateCnt) % 2

sList = list(s)
n = [1] * len(s)
ret = [1] * len(s)
for i in range(integrateCnt + addCnt):
    for j, way in enumerate(s):
        ret[j] -= n[j]
        if way == 'R':
            ret[j + 1] += n[j]
        elif way == 'L':
            ret[j - 1] += n[j]
    n = ret[:]
print(' '.join(map(str, ret)))

### FIXED CODE ###

# D - Gathering Children
import re

s = input()
sList = list(s)

# RR...RLL...Lは、「RL」の部分に子供が集約される
# RLそれぞれの連続する個数が
# ・同じ   → 同値
# ・異なる → 入れ替えごとに交互に余りを足していく
#          → 差が偶数なら同値
#             差が奇数ならいずれかを1大きくする
retList = []
matchList = re.findall('R+L+', s)

for match in matchList:
    rCnt = match.count('R')
    lCnt = len(match) - rCnt
    
    half = len(match) // 2
    dif = abs(rCnt - lCnt)

    # まずは均等配分する
    ret = [0] * len(match)
    ret[rCnt - 1] = half
    ret[-lCnt] = half
    
    # 個数が同じ         → 既に同値にしているので終了
    # 奇数だけど差が偶数 → 既に同値にしているので終了
    # 奇数かつ  差が奇数
    if dif % 2 != 0:

        # 集約完了までに動かした回数を計算
        changeCnt = (rCnt if rCnt < lCnt else lCnt) - 1 + dif
        
        # 集約完了時点で連続する個数が多かった側が1大きくなっている
        # → 残りの試行回数が奇数なら逆転、偶数ならそのまま
        if (rCnt < lCnt and changeCnt % 2 != 0) or (rCnt > lCnt and changeCnt % 2 == 0):
            ret[rCnt - 1] = half + 1
            ret[-lCnt] = half
        else:
            ret[rCnt - 1] = half
            ret[-lCnt] = half + 1
    retList.extend(ret)
print(' '.join(map(str, retList)))