Problem p03553 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define pb push_back
#define P pair<int, int>
#define vp vector<P>
#define PP pair<P, int>
#define vpp vector<PP>
#define fi first
#define se second
#define INF (1LL << 50)
#define MOD 1000000007
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
const int MAX_V = 52;
// 辺を表す構造体(行き先，容量，逆辺)
struct edge {
  int to, cap, rev;
};
vector<edge> G[MAX_V]; // 隣接リスト
bool used[MAX_V];      // dfsの探索済フラグ

// fromからtoに向かう容量capの辺をグラフに追加
void add_edge(int from, int to, int cap) {
  G[from].pb((edge){to, cap, (int)G[to].size()});
  G[to].pb((edge){from, 0, (int)G[from].size() - 1});
}

// 増加パスをDFSで探す
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  REP(i, G[v].size()) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

// sからtの最大流
int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

signed main() {
  int n;
  cin >> n;
  int sum = 0;
  FOR(i, 1, n + 1) {
    int t;
    cin >> t;
    if (t >= 0) {
      sum += t;
      add_edge(0, i, t);
      add_edge(i, n + 1, 0);
    } else {
      add_edge(0, i, 0);
      add_edge(i, n + 1, -t);
    }
    for (int j = 2; i * j <= n; j++) {
      add_edge(i * j, i, INF);
    }
  }
  cout << sum - max_flow(0, n + 1) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define pb push_back
#define P pair<int, int>
#define vp vector<P>
#define PP pair<P, int>
#define vpp vector<PP>
#define fi first
#define se second
#define INF (1LL << 50)
#define MOD 1000000007
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
const int MAX_V = 100;
// 辺を表す構造体(行き先，容量，逆辺)
struct edge {
  int to, cap, rev;
};
vector<edge> G[MAX_V]; // 隣接リスト
bool used[MAX_V];      // dfsの探索済フラグ

// fromからtoに向かう容量capの辺をグラフに追加
void add_edge(int from, int to, int cap) {
  G[from].pb((edge){to, cap, (int)G[to].size()});
  G[to].pb((edge){from, 0, (int)G[from].size() - 1});
}

// 増加パスをDFSで探す
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  REP(i, G[v].size()) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

// sからtの最大流
int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

signed main() {
  int n;
  cin >> n;
  int sum = 0;
  FOR(i, 1, n + 1) {
    int t;
    cin >> t;
    if (t >= 0) {
      sum += t;
      add_edge(0, i, t);
      add_edge(i, n + 1, 0);
    } else {
      add_edge(0, i, 0);
      add_edge(i, n + 1, -t);
    }
    for (int j = 2; i * j <= n; j++) {
      add_edge(i * j, i, INF);
    }
  }
  cout << sum - max_flow(0, n + 1) << endl;
  return 0;
}