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>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct max_flow{
    public:
    struct edge {int to;ll cap;int rev;};
    int V;
    vector<vector<edge>> G;
    vector<int> level,iter;
 
    void init(int v){
        V = v; G.resize(v);
    }
 
    void add_edge(int from, int to, ll cap){
        int a=G[to].size(),b=G[from].size()-1;
        G[from].push_back((edge){to,cap,a});
        G[to].push_back((edge){from,0,b});
    }
 
    void bfs(int s){
        level.assign(V,-1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while(!que.empty()){
            int v = que.front();
            que.pop();
            rep(i,G[v].size()){
                edge &e = G[v][i];
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
        }
    }
 
    ll dfs(int v, int t, ll f){
        if(v == t) return f;
        for(int &i = iter[v]; i < G[v].size(); i++){
            edge &e = G[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                ll d = dfs(e.to,t,min(f,e.cap));
                if(d > 0){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
 
    ll run(int s, int t){
        int flow = 0;
        while(1){
            bfs(s);
            if(level[t] < 0) return flow;
            iter.assign(V,0);
            ll tmp;
            while((tmp = dfs(s,t,INF)) > 0){
                flow += tmp;
            }
        }
    }
};

int n;
ll ans=0;
void solve(){
    cin >> n;
    max_flow f;f.init(n+2);
    rep(i,n){
        ll c;
        cin >> c;
        if (c>=0) {
            f.add_edge(i+1,n+1,c);
            ans+=c;
        }
        else f.add_edge(0,i+1,-c);
    }
    rep(i,n){
        int k=2;
        while ((i+1)*k<=n){
            f.add_edge(i+1,k*(i+1),INF);
            k+=1;
        }
    }

    rep(i,n){
        rep(j,f.G[i].size()){
            //cout << i << " " << f.G[i][j].to << " " << f.G[i][j].cap << endl;
        }
    }

    cout << ans-f.run(0,n+1) << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
}