Problem: We have N gemstones labeled 1 through N.  You can perform the following operation any number of times (possibly zero).    * Select a positive integer x, and smash all the gems labeled with multiples of x.  Then, for each i, if the gem labeled i remains without getting smashed, you will receive a_i yen (the currency of Japan). However, a_i may be negative, in which case you will be charged money.  By optimally performing the operation, how much yen can you earn?

Submission attempts (in order):
N = int(input())
As = list(map(int, input().split()))

def func(Bs):
    for x in range(N):
        if Bs[x] >= 0: continue
        if max(Bs[x::x + 1]) <= 0:
            Bs[x] = 0

func(As)

for x in range(34, 51):
    if As[x - 1] + As[x * 2 - 1] < 0:
        As[x - 1] = 0
        As[x * 2 - 1] = 0

for x in [27, 29, 31, 33]:
    if As[x - 1] + As[x * 2 - 1] + As[x * 3 - 1] < 0:
        As[x - 1] = 0
        As[x * 2 - 1] = 0
        As[x * 3 - 1] = 0
zs = [27, 29, 31, 33] + list(range(34, 51))

ns = [1, 2, 4, 8, 16, 32] + [3, 6, 9, 12, 18, 24, 27, 36, 48] \
    + [5, 10, 15, 20, 25, 30, 40, 45, 50] + [7, 14, 21, 28, 35, 42, 49] \
    + [11, 22, 33, 44] + [13, 26, 39] + [17, 34] + [19, 38] + [23, 46] \
    + [29, 31, 37, 41, 43, 47]

memo = {}
memo[tuple(As)] = sum(As)

cs = [n - 1 for n in ns if n <= N and n not in zs and As[n - 1] < 0]
for c in cs:
    for tupleA in list(memo.keys()):
        if tupleA[c] == 0:
            continue

        listA = list(tupleA)
        for x in range(c, N, c + 1):
            listA[x] = 0

        func(listA)
        memo[tuple(listA)] = sum(listA)

print(max(memo.values()))


from collections import deque


def addEdge(adjL, vFr, vTo, cap):
    adjL[vFr].append([vTo, cap, len(adjL[vTo])])
    adjL[vTo].append([vFr, 0, len(adjL[vFr]) - 1]) 



def Dinic(adjL, vSt, vEn):

    
    def BFS(vSt):
        dist[vSt] = 0
        Q = deque([vSt])
        while Q:
            vNow = Q.popleft()

            for i, (v2, cap, iRev) in enumerate(adjL[vNow]):
                if dist[v2] == -1 and cap > 0:
                    
                    dist[v2] = dist[vNow] + 1
                    Q.append(v2)


    
    def DFS(vNow, vEn, fNow):
        if vNow == vEn:
            
            return fNow

        
        iSt = iNext[vNow]
        for i, (v2, cap, iRev) in enumerate(adjL[vNow][iSt:], iSt):
            if dist[vNow] < dist[v2] and cap > 0:
                
                df = DFS(v2, vEn, min(fNow, cap))
                if df > 0:
                    
                    adjL[vNow][i][1] -= df
                    adjL[v2][iRev][1] += df
                    return df

            iNext[vNow] += 1

        
        return 0


    numV = len(adjL)
    MaximumFlow = 0
    while True:
        
        dist = [-1] * numV
        BFS(vSt)
        if dist[vEn] == -1:
            
            return MaximumFlow

        iNext = [0] * numV
        while True:
            
            df = DFS(vSt, vEn, float('inf'))

            if df == 0:
                
                break

            
            MaximumFlow += df


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

adjList = [[] for v in range(N + 2)]
for i, A in enumerate(As, 1):
    if A <= 0:
        addEdge(adjList, 0, i, -A)
    else:
        addEdge(adjList, i, N + 1, A)

for i in range(1, N + 1):
    for j in range(2 * i, N + 1, i):
        addEdge(adjList, i, j, float('inf'))


mf = Dinic(adjList, 0, N + 1)
print(sum([A for A in As if A > 0]) - mf)


Verdicts: Runtime Error

Accepted