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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    ReadData re;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        re = new ReadData();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = re.i();
        N++;
        long[] A = new long[N];
        for(int i=1;i<N;i++){
            A[i] = re.i();
        }
        long count = 0;
        for(int i=0;i<N;i++){
            count += A[i];
            A[i] *= -1;
        }
        long max = 0;
        for(int k=0;k<65536;k++){
            bool[] b = new bool[17];
            long C = 0;
            for(int i=1;i<17;i++){
                b[i] = ((1 << (i-1)) & k) != 0;
            }
            bool[] get = new bool[N];
            for(int i=1;i<17;i++){
                if(b[i]){
                    for(int j=1;j*i<N;j++){
                        get[j*i] = true;
                    }
                }
            }
            for(int i=0;i<17;i++){
                if(i < N && get[i]){
                    C += A[i];
                }
            }
            for(int i=17;i<N;i++){
                if(get[i]){
                    C += A[i];
                }
                else{
                    long sum = A[i];
                    for(int j=2;j*i<N;j++){
                        if(!get[i*j] && A[i*j] < 0){
                            sum += A[i*j];
                        }
                    }
                    if(sum > 0){
                        C += A[i];
                        for(int j=1;j*i<N;j++){
                            if(get[i*j]){
                                get[i*j] = true;
                            }
                        }
                    }
                }
            }
            for(int i=17;i<N;i++){
                if(!get[i]){
                    long sum = A[i];
                    for(int j=2;j*i<N;j++){
                        if(!get[i*j] && A[i*j] < 0){
                            sum += A[i*j];
                        }
                    }
                    if(sum > 0){
                        C += A[i];
                        for(int j=1;j*i<N;j++){
                            if(get[i*j]){
                                get[i*j] = true;
                            }
                        }
                    }
                }
            }
            for(int i=18;i<N;i+=6){
                if(!get[i/3] && !get[i/2]){
                    long sum = A[i];
                    for(int j=i;j<N;j++){
                        if(j % (i/3) == 0 && j % (i/2) == 0 && !get[j] && A[j] < 0){
                            sum += A[j];
                        }
                    }
                    if(sum > 0){
                        C += A[i];
                        for(int j=i;j<N;j++){
                            if(j % (i/3) == 0 && j % (i/2) == 0 && !get[j] && A[j] < 0){
                                get[j] = true;;
                            }
                        }
                    }
                }
            }
            for(int i=24;i<N;i+=12){
                if(!get[i/3] && !get[i/4]){
                    long sum = A[i];
                    for(int j=i;j<N;j++){
                        if(j % (i/3) == 0 && j % (i/4) == 0 && !get[j] && A[j] < 0){
                            sum += A[j];
                        }
                    }
                    if(sum > 0){
                        C += A[i];
                        for(int j=i;j<N;j++){
                            if(j % (i/3) == 0 && j % (i/4) == 0 && !get[j] && A[j] < 0){
                                get[j] = true;;
                            }
                        }
                    }
                }
            }
            max = Math.Max(max,C);
        }
        count += max;
        sb.Append(count+"\n");
    }
}
class ReadData{
    string[] str;
    int counter;
    public ReadData(){
        counter = 0;
    }
    public string s(){
        if(counter == 0){
            str = Console.ReadLine().Split(' ');
            counter = str.Length;
        }
        counter--;
        return str[str.Length-counter-1];
    }
    public int i(){
        return int.Parse(s());
    }
    public long l(){
        return long.Parse(s());
    }
    public double d(){
        return double.Parse(s());
    }
    public int[] ia(int N){
        int[] ans = new int[N];
        for(int j=0;j<N;j++){
            ans[j] = i();
        }
        return ans;
    }
    public int[] ia(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        int[] ans = new int[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = int.Parse(str[j]);
        }
        return ans;
    }
    public long[] la(int N){
        long[] ans = new long[N];
        for(int j=0;j<N;j++){
            ans[j] = l();
        }
        return ans;
    }
    public long[] la(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        long[] ans = new long[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = long.Parse(str[j]);
        }
        return ans;
    }
    public double[] da(int N){
        double[] ans = new double[N];
        for(int j=0;j<N;j++){
            ans[j] = d();
        }
        return ans;
    }
    public double[] da(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        double[] ans = new double[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = double.Parse(str[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int[] f,int[] t){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<f.Length;j++){
            ans[f[j]].Add(t[j]);
            ans[t[j]].Add(f[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int M){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
    public List<int>[] g(){
        int N = i();
        int M = i();
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
}
public static class Define{
    public const long mod = 1000000007;
}
public static class Debug{
    public static void Print(double[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(long[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(int[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
}