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 <bits/stdc++.h>
//#define che
#define debug(...) printf(__VA_ARGS__)
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
const int maxn= 100 + 10;
const LL oo = 1ll<<61;
struct Edge{
	int from ,to;LL  flow, cap;
	int nx;
}edg[maxn*100];

int cur[maxn], h[maxn], a[maxn], edgn =0 , G[maxn],N, s,t, fro, rea, que[maxn];
void AddE(int u, int v, LL cap){
	edg[edgn] = (Edge){ u, v, 0, cap, G[u]};
	G[u]= edgn++;
	edg[edgn] = (Edge){ v, u, 0, 0 , G[v]};
	G[v]= edgn++;
}

LL DFS(int u, LL a);
bool BFS();
LL MaxSubGraph();

int main(){
	scanf("%d", &N);
	for (int i=0;i<=N+1; i++) G[i] = -1;
	for (int i=1; i<=N; i++)
		for (int j=i+i; j<=N; j+=i ) AddE( i, j, oo);
	// must be more carefull the negative !!!!
	s= 0, t=N+1;
	LL ans= 0;
	for (int i=1; i<=N; i++) {
		scanf("%d", &a[i]);
		ans += a[i];
		if ( a[i] <0 ) AddE(s, i, -1ll*a[i] );
			else 
		if ( a[i] >0) AddE(i, t, 1ll*a[i]);
	}
	
	ans +=MaxSubGraph();
	printf("%lld\n", ans); 
	return 0;
}

LL DFS(int u, LL a){
	if (u==t || a==0) return a;
	LL flow=0, f;
	for (int &i = cur[u] ; i!= -1; i = edg[i].nx){
		Edge &e= edg[i];
		if ( (h[e.to] ==h[u]+1) && (f=DFS(e.to, min(a, e.cap-e.flow)))>0){
			e.flow += f;
			edg[i^1].flow -= f;
			flow += f;
			a-= f;
			if ( a==0 ) break;
		}
	}
	return flow;
}

bool BFS(){
	fro= rea =0 ;
	for (int i=0; i<=N+1 ; i++) h[i] = -1;
	h[s] = 0;que[ rea++] = s;
	while ( fro < rea){
		int u= que[fro++];
		#ifdef che
		debug("h[%d] = %d\n", u, h[u]);
		#endif
		for (int i=G[u]; i!= -1; i =edg[i].nx){
			if ( h[edg[i].to] != -1 || edg[i].cap == edg[i].flow) continue;
			h[edg[i].to] = h[u]+1;
			que[rea++] = edg[i].to;
		}
	}
	return h[t] != -1;
	 
}


LL MaxSubGraph(){
	LL ans= 0 ;
	for (int i=1; i<= N; i++)
		if ( a[i] <0) ans += -a[i];
	#ifdef che
	debug("ans int flow= %lld\n", ans);

		for (int i=0; i<edgn ;i++) debug("edge : %d---%d, cap= %lld flow = %lld\n", edg[i].from, edg[i].to, edg[i].cap, edg[i].flow);
		debug("\n");
	#endif
	
	
	while ( BFS()){
		
		for (int i=0; i<=N+1; i++) cur[i] = G[i];
		ans -= DFS(s , oo);
	}
	#ifdef che
	debug("ans int flow= %lld\n", ans);
	#endif
	return ans;
	
}