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 (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>

typedef long long int int64;

const int f=102;
#define POS(i,j) ((i)*f+(j))

int dfs(int v,int64 *g,int *array,int *used){
  used[v]=1;
  array[0]=v;
  if(v==f-1) return 1;
  int i;
  for(i=0;i<f;i++){
    if(!used[i] && g[POS(v,i)]>0){
      int c=dfs(i,g,array+1,used);
      if(c>0) return c+1;
    }
  }
  return 0;
}

int64 calc(int *a,int n){
  int64 *g=(int64 *)calloc(f*f,sizeof(int64));
  int i,j;
  for(i=1;i<=n;i++){
    if(a[i]>=0){
      g[POS(i,f-1)]=a[i];
    } else {
      g[POS(0,i)]=-a[i];
    }
  }
  for(i=1;i<=n;i++){
    for(j=2*i;j<=n;j+=i){
      g[POS(i,j)]=1000000000LL*1000;
    }
  }
  int array[128];
  int used[128]={0};
  int64 ans=0;
  int64 len=dfs(0,g,array,used);
  while(len){
    int64 min=1000000000LL*1000;
    for(i=0;i+1<len;i++){
      int64 t=g[POS(array[i],array[i+1])];
      if(min>t) min=t;
    }
    ans+=min;
    for(i=0;i+1<len;i++){
      int v=array[i];
      int u=array[i+1];
      g[POS(v,u)]-=min;
      g[POS(u,v)]+=min;
    }
    for(i=0;i<f;i++) used[i]=0;
    len=dfs(0,g,array,used);
  }
  free(g);
  return ans;
}

void run(void){
  int n;
  scanf("%d",&n);
  int i;
  int a[101];
  for(i=1;i<=n;i++) scanf("%d",a+i);
  int64 sum=0;
  for(i=1;i<=n;i++) if(a[i]>=0) sum+=a[i];
  int64 ans=sum-calc(a,n);
  printf("%lld\n",ans);
  return;
}

int main(void){
  run();
  return 0;
}