Language: C++

#include<bits/stdc++.h>
using namespace std;
struct edge;
using Graph=vector<vector<edge>>;
using ll=long long;
#define INF 100000000000000
#define p(a,b) make_pair(a,b)
using matrix=vector<vector<ll>>;

struct edge
{
    ll to;
    ll cap;
    bool reverse;
    ll rev;
};

void add_edge(ll from,ll to,ll cap,Graph &graph,bool reverse=0)
{
    graph[from].push_back((edge){to,cap,0,(ll)graph[to].size()});
    graph[to].push_back((edge){from,0,1,(ll)graph[from].size()-1});
}

bool dijkstra(ll s,ll t,Graph graph,vector<pair<ll,ll>> &prev)
{
    ll n=graph.size();
    for (ll i=0;i<n;i++) prev[i]=p(-1,-1);
    vector<ll> d(n,INF);
    d[s]=0;prev[s]=p(s,-1);
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> que;
    que.push(p(d[s],s));
    ll v;
    edge &e;
    while(!que.empty())
    {
        v=que.top().second;
        que.pop();
        for (ll i=0;i<graph[v].size();i++)
        {
            e=graph[v][i];
            if (e.cap>0)
            {
                if (d[e.to]>d[v]+1)
                {
                    d[e.to]=d[v]+1;
                    prev[e.to]=p(v,i);
                    que.push(p(d[e.to],e.to));
                }
            }
        }
    }
return prev[t].first!=-1;
}

void update(ll from,ll to,ll f, ll edge_num, Graph &graph,matrix &flow,matrix cap)
{
    edge &e=graph[from][edge_num];
    edge &e_r=graph[e.to][e.rev];
    if (e.reverse)
    {
        flow[to][from]-=f;
        e_r.cap=cap[to][from]-flow[to][from];
        e.cap=flow[to][from];
    }
    else
    {
        flow[from][to]+=f;
        e.cap=cap[from][to]-flow[from][to];
        e_r.cap=flow[from][to];
    }
return;
}

ll max_flow(ll s,ll t,Graph graph,matrix cap)
{
    ll n=graph.size();
    matrix flow(n,vector<ll> (n,0));
    vector<pair<ll,ll>> prev(n);
    ll from,to,f,ans=0;
    while (dijkstra(s,t,graph,prev))
    {
        
        from=-1;to=t;f=INF;
        while (from!=s)
        {
            from=prev[to].first;
            f=min(f,graph[from][prev[to].second].cap);
            to=from;
        }
        ans+=f;
        from=-1;to=t;
        while(from!=s)
        {
            from=prev[to].first;
            update(from,to,f,prev[to].second,graph,flow,cap);
            to=from;
        }
    }
return ans;
}

int main()
{
    ll N;cin >> N;
    ll ans=0;
    Graph graph(N+2);
    matrix cap(N+2,vector<ll> (N+2,0));
    ll a;
    for (ll i=0;i<N;i++)
    {
        cin >> a;
        if (a<0)
        {
            add_edge(0,i+1,-a,graph);
            cap[0][i+1]=-a;
            for (ll j=2;j*(i+1)<=N;j++) 
            {
                add_edge(i+1,j*(i+1),INF,graph);
                cap[i+1][j*(i+1)]=INF;
            }
        }
        else
        {
            ans+=a;
            add_edge(i+1,N+1,a,graph);
            for (ll j=2;j*(i+1)<=N;j++) 
            {
                add_edge(i+1,j*(i+1),INF,graph);
                cap[i+1][j*(i+1)]=INF;
            }
        }
    }
    cout << ans-max_flow(0,N+1,graph,cap) << endl;
return 0;
}

Compiler output:
a.cc: In function 'bool dijkstra(ll, ll, Graph, std::vector<std::pair<long long int, long long int> >&)':
a.cc:33:11: error: 'e' declared as reference but not initialized
   33 |     edge &e;
      |           ^