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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main{
    
    public static void main(String args[]) throws IOException{
	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	String s[]=br.readLine().split(" ");

        int n=Integer.parseInt(s[0]);
        int d[]=new int[n];

	int max=0;

	ArrayList<Integer> nlist=new ArrayList<Integer>();

	s=br.readLine().split(" ");

	for(int i=0;i<n;i++){
	    d[i]=Integer.parseInt(s[i]);
	    max+=d[i];
	    nlist.add(i+1);
	}
	ArrayList<ArrayList<Integer>> list=getSubsets(nlist,0);
	
	for(int i=0;i<list.size();i++){
	    int sum=0;
	    int dd[]=new int[n];
	    for(int j=0;j<n;j++){
		dd[j]=d[j];
	    }
	    for(int j=0;j<list.get(i).size();j++){
		for(int k=0;k<n;k++){
		    if((k+1)%list.get(i).get(j)==0){
			dd[k]=0;
		    }
		}
	    }
	    for(int k=0;k<n;k++){
		sum+=dd[k];
	    }
	    max=Math.max(max,sum);
	}
	System.out.println(max);
	
    }

    static  ArrayList<ArrayList<Integer>> getSubsets(ArrayList<Integer> set, int index){
	ArrayList<ArrayList<Integer>> allSubsets;
	
	if(set.size() == index){
	    allSubsets = new ArrayList<ArrayList<Integer>>();
	    allSubsets.add(new ArrayList<Integer>());	//空集合追加
	}else{
	    allSubsets = getSubsets(set, index+1);
	    int item = set.get(index);
	    ArrayList<ArrayList<Integer>> moreSubset = new ArrayList<ArrayList<Integer>>();
	    for (ArrayList<Integer> subset : allSubsets) {
		ArrayList<Integer> newSubset = new ArrayList<Integer>();
		newSubset.addAll(subset);	//いままでの全部いれたのと
		newSubset.add(item);		//新しい数字をたして
		moreSubset.add(newSubset);	//新しいArrayListを作ってmoreにいれる！
	    }
	    allSubsets.addAll(moreSubset);
	}
	return allSubsets;
    }

    
    public static void permutation(String q, String ans){
        // Base Case
        if(q.length() <= 1) {
	    //   comb.add(ans + q);
        }
        // General Case
        else {
            for (int i = 0; i < q.length(); i++) {
                permutation(q.substring(0, i) + q.substring(i + 1),
                        ans + q.charAt(i));
            }
        }
    }
}