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 Rust (1.15.1) (judge verdict: Accepted)

// use std::ops::{Index, IndexMut};
// use std::cmp::{Ordering, min, max};
// use std::collections::{BinaryHeap, BTreeMap};
// use std::collections::btree_map::Entry::{Occupied, Vacant};
// use std::clone::Clone;

fn getline() -> String{
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! mvec {
    ($v: expr, $s: expr) => {
        vec![$v; $s]
    };
    ($v: expr, $s: expr, $($t: expr),*) => {
        vec![mvec!($v, $($t),*); $s]
    };
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator, 
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

#[derive(Debug)]
struct Solver {
    n: usize,
    a: Vec<i64>,
    cnt: Vec<u32>,
}

impl Solver {
    fn solve(&mut self, p: usize) -> i64 {
        if p*3 > self.n {
            let mut res = (0..p-1).filter(|x| self.cnt[*x] == 0).map(|x| self.a[x]).sum();
            // debug!(p);
            // debug!(self.cnt);
            // debug!(res);
            for i in (p..self.n+1).rev() {
                if i%2 == 0 && i/2 >= p {
                    continue;
                }
                if i+i > self.n {
                    if self.a[i-1] > 0 && self.cnt[i-1] == 0 {
                        res += self.a[i-1];
                    }
                } else {
                    use std::cmp::max;
                    if self.cnt[i-1] == 0 {
                        if self.cnt[i+i-1] > 0 {
                            res += max(0, self.a[i-1]);
                        } else {
                            res += max(0, max(self.a[i-1], self.a[i-1]+self.a[i+i-1]));
                        }
                    }
                }
            }
            res
        } else {
            let mut ret = self.solve(p+1);
            if self.cnt[p-1] == 0 {
                let mut j = p;
                while j <= self.n {
                    self.cnt[j-1] += 1;
                    j += p;
                }
                ret = std::cmp::max(ret, self.solve(p+1));
                j = p;
                while j <= self.n {
                    self.cnt[j-1] -= 1;
                    j += p;
                }
            }
            ret
        }
    }
}

fn main() {
    let n = readl!(usize);
    let a = readlvec!(i64);
    let mut solver = Solver {n: n, a: a, cnt: vec![0;n]};
    // debug!(solver.solve(1));
    println!("{}", solver.solve(1));
    // debug!(solver);
}