#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<vector<long long>> mll;
typedef vector<pair<long long, long long>> vpl;
typedef long double ld;
typedef vector<long double> vld;
typedef vector<vector<long double>> mld;
typedef vector<bool> vbl;
typedef vector<vector<bool>> mbl;
#define minimize(a, b) (a = min(a, b))
#define maximize(a, b) (a = max(a, b))
const long long MOD = 1e9 + 7;

template <typename Type>
istream &operator>>(istream &in, vector<Type> &vec) {
    ll n = vec.size();
    for (int i = 0; i < n; i++)
        in >> vec[i];
    return in;
}

template <typename Type>
ostream &operator<<(ostream &out, vector<Type> &vec) {
    for (auto val : vec)
        out << val << " ";
    out << endl;
    return out;
}

struct Dinic {
    struct Edge {
        int u, v;
        ll cap, flow;
        Edge() : u(0), v(0), cap(0), flow(0) {
        }
        Edge(int uu, int vv, ll ccap) : u(uu), v(vv), cap(ccap), flow(0) {
        }
    };

    int size;
    vector<Edge> edges;
    vector<vector<int>> adjacency;
    vector<int> d, pt;
    Dinic(int NN) : size(NN), edges(0), adjacency(size), d(size), pt(size) {
    }
    void add_edge(int u, int v, ll cap_f, ll cap_r = 0) {
        if (u != v) {
            edges.emplace_back(Edge(u, v, cap_f));
            adjacency[u].emplace_back(edges.size() - 1);
            edges.emplace_back(Edge(v, u, cap_r));
            adjacency[v].emplace_back(edges.size() - 1);
        }
    }
    bool __bfs(int source, int sink) {
        queue<int> q({source});
        fill(d.begin(), d.end(), size + 1);
        d[source] = 0;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            if (u == sink)
                break;
            for (int k : adjacency[u]) {
                Edge &e = edges[k];
                if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
                    d[e.v] = d[e.u] + 1;
                    q.emplace(e.v);
                }
            }
        }
        return d[sink] != size + 1;
    }

    ll __dfs(int u, int sink, ll flow = -1) {
        if (u == sink || flow == 0)
            return flow;
        for (int &i = pt[u]; i < (int)adjacency[u].size(); ++i) {
            Edge &e = edges[adjacency[u][i]];
            Edge &oe = edges[adjacency[u][i] ^ 1];
            if (d[e.v] == d[e.u] + 1) {
                ll amt = e.cap - e.flow;
                if (flow != -1 && amt > flow)
                    amt = flow;
                if (ll pushed = __dfs(e.v, sink, amt)) {
                    e.flow += pushed;
                    oe.flow -= pushed;
                    return pushed;
                }
            }
        }
        return 0;
    }
    ll max_flow(int source, int sink) {
        ll total = 0;
        while (__bfs(source, sink)) {
            fill(pt.begin(), pt.end(), 0);
            while (ll flow = __dfs(source, sink))
                total += flow;
        }
        return total;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll n;
    cin >> n;
    vll a(n);
    cin >> a;
    Dinic graph(n + 2);

    vll v(n, 0);
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j += i) {
            v[i - 1] += a[j - 1];
            graph.add_edge(i - 1, j - 1, INT64_MAX);
        }
    }
    ll sum = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] > 0)
            sum += a[i];
    }
    const ll SOURCE = n + 0, SINK = n + 1;
    for (int i = 0; i < n; i++) {
        if (a[i] > 0) {
            graph.add_edge(i, SINK, abs(a[i]));
        } else {
            graph.add_edge(SOURCE, i, abs(a[i]));
        }
        // if ((i + 1) * 2 > n)
        //     graph.add_edge(i, SINK, INT64_MAX);
    }
    ll flow = graph.max_flow(SOURCE, SINK);
    cout << sum - flow << endl;
}

Measured memory_bytes: 384