Problem:
Score : 700 points

Problem Statement

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?

Constraints

All input values are integers.

1 \leq N \leq 100

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum amount of money that can be earned.

Sample Input 1

6
1 2 -6 4 5 3

Sample Output 1

12

It is optimal to smash Gem 3 and 6.

Sample Input 2

6
100 -100 -100 -100 100 -100

Sample Output 2

200

Sample Input 3

5
-1 -2 -3 -4 -5

Sample Output 3

0

It is optimal to smash all the gems.

Sample Input 4

2
-1000 100000

Sample Output 4

99000

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <vector>
#define llint long long 
#define inf 1000000000000000

using namespace std;

struct edge{
	llint to, cap, rev;
	edge(llint a, llint b, llint c){
		to = a, cap = b, rev = c;
	}
};

vector<edge> G[105];
bool used[105];

llint dfs(llint s, llint t, llint c)
{
	if(s == t) return c;
	used[s] = true;
	
	llint ret;
	for(llint i = 0; i < (llint)G[s].size(); i++){
		if(used[G[s][i].to] || G[s][i].cap == 0) continue;
		ret = dfs(G[s][i].to, t, min(c, G[s][i].cap));
		if(ret > 0){
			G[s][i].cap -= ret;
			G[ G[s][i].to ][ G[s][i].rev ].cap += ret;
			return ret;
		}
	}
	return 0;
}

llint N, S, T;
llint a[105], sum, psum;

void add_edge(llint s, llint t, llint c)
{
	G[s].push_back(edge(t, c, G[t].size()));
	G[t].push_back(edge(s, 0, G[s].size()-1));
}

int main(void)
{
	cin >> N;
	sum = psum = 0;
	for(llint i = 1; i <= N; i++){
		cin >> a[i];
		sum += a[i];
		a[i] *= -1;
		if(a[i] >= 0) psum += a[i];
	}
	
	S = 0, T = N+1;
	for(llint i = 1; i <= N; i++){
		for(llint j = 1; j <= N; j++){
			if(j % i == 0) add_edge(i, j, inf);
		}
	}
	
	for(llint i = 1; i <= N; i++){
		if(a[i] >= 0) add_edge(S, i, a[i]);
		else add_edge(i, T, -a[i]);
	}
	
	llint ans = 0, ret;
	while(1){
		for(llint i = S; i <= T; i++) used[i] = false;
		ret = dfs(S, T, inf);
		if(ret == 0) break;
		ans += ret;
	}
	
	cout << sum + (psum-ans) << endl;
	return 0;
}