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: Wrong Answer)

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;

int n;
int n0;
long long a[111];

set<long long>st;
set<long long>::iterator it;
int h[111];
int g[111];

long long ans,ss;
int main()
{
    int i,j,k;
    
    cin>>n;
    n0=n;
    ss=0;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        ss+=a[i];
    }
    
    ans=ss;
    
    n=n0/4;
    /*
    for(i=1;i<=n;i++)
    {
         st.insert(1<<(i-1));
    }
    */
    st.insert(0);
    
    
    int c=0;
    it=st.begin();
    while(it!=st.end())
    {
        long long now=*it;
        long long now0=now;
        i=1;
        
        //cout<<now<<endl;
        //memset(h,0,sizeof(h));
        while(now>0)
        {
            h[i]=(now&1);
            now>>=1;
            i++;
        }
        /*
        for(j=1;j<=n;j++)
        {
            cout<<h[j]<<' ';
        }
        cout<<"hhh"<<endl;
        */
        for(j=i;j<=n;j++)
        {
             
             for(k=1;k<=j/k;k++)
             {
                 if(j%k==0)
                 {
                     if(h[k]==1 || h[j/k]==1)break;
                 }
             }
             if(k<=j/k)
             {
                 continue;
             }
             
             st.insert(now0|(1<<(j-1)));
        }
        
        
        
        //////////////////////////
        long long tmp=0;
        memset(g,0,sizeof(g));
        for(i=1;i<=n;i++)
        {
            //cout<<h[i]<<"  ss"<<endl;
            if(h[i]==1)
            {
                for(j=i;j<=n0;j+=i)
                {
                    if(g[j]==0)tmp+=a[j];
                    //cout<<j<<' '<<a[j]<<"   uuu"<<endl;
                    g[j]=1;
                }
            }
        }
        
        //cout<<tmp<<" tmp"<<endl;
        
        for(i=n0/2+1;i<=n0;i++)
        {
            if(g[i]==0 && a[i]<0)
            {
                tmp+=a[i];
                g[i]=1;
            }
        }
        
        //cout<<tmp<<" tmp2"<<endl;
        for(i=n0/4+1;i<=n0/2;i++)
        {
            long long w=0;
            if(g[i]==0)w+=a[i];
            if(i*2<=n0 && g[i*2]==0)w+=a[i*2];
            
            if(w<0)
            {
                tmp+=w;
                g[i]=g[i*2]=1;
            }
        }
        //cout<<ss<<' '<<tmp<<' '<<ss-tmp<<"  ---"<<endl;
        ans=max(ans,ss-tmp);
        
        
        ///////////////////////////////
        c++;
        it++;
    }
    
    //cout<<c<<endl;
    
    cout<<ans<<endl;
    return 0;
}