Problem p03553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <math.h>
#include <stdio.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, P> PP;

struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};

int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int ddx[] = {1, 1, 1, 0, -1, -1, -1, 0}, ddy[] = {1, 0, -1, -1, -1, 0, 1, 1};

//-----------------------------------------------------------------------------

struct Edge {
  int to, cap, rev;
  Edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
};

int n, a[105];
vector<Edge> g[105];
bool used[105];

void add_edge(int from, int to, int cap) {
  g[from].pb(Edge(to, cap, g[to].size()));
  g[to].pb(Edge(from, 0, g[from].size() - 1));
}

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(e.cap, f));
      if (d > 0) {
        e.cap -= d;
        g[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
}

ll max_flow(int s, int t) {
  ll f = 0;
  while (true) {
    fill(used, used + 105, false);
    int d = dfs(s, t, INF);
    if (d == 0)
      return f;
    f += d;
  }
  return f;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  REP(i, n) cin >> a[i + 1];

  ll sum = 0;
  int s = 0, t = n + 1;
  FOR(i, 1, n + 1) {
    if (a[i] <= 0)
      add_edge(s, i, -a[i]);
    else {
      sum += a[i];
      add_edge(i, t, a[i]);
    }
  }
  FOR(i, 1, n + 1) for (int j = 2 * i; j <= n; j += i) add_edge(i, j, INF);

  cout << sum - max_flow(s, t) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
#include <stdio.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, P> PP;

struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};

int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int ddx[] = {1, 1, 1, 0, -1, -1, -1, 0}, ddy[] = {1, 0, -1, -1, -1, 0, 1, 1};

//-----------------------------------------------------------------------------

struct Edge {
  int to, cap, rev;
  Edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
};

int n, a[105];
vector<Edge> g[105];
bool used[105];

void add_edge(int from, int to, int cap) {
  g[from].pb(Edge(to, cap, g[to].size()));
  g[to].pb(Edge(from, 0, g[from].size() - 1));
}

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(e.cap, f));
      if (d > 0) {
        e.cap -= d;
        g[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

ll max_flow(int s, int t) {
  ll f = 0;
  while (true) {
    fill(used, used + 105, false);
    int d = dfs(s, t, INF);
    if (d == 0)
      return f;
    f += d;
  }
  return f;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  REP(i, n) cin >> a[i + 1];

  ll sum = 0;
  int s = 0, t = n + 1;
  FOR(i, 1, n + 1) {
    if (a[i] <= 0)
      add_edge(s, i, -a[i]);
    else {
      sum += a[i];
      add_edge(i, t, a[i]);
    }
  }
  FOR(i, 1, n + 1) for (int j = 2 * i; j <= n; j += i) add_edge(i, j, INF);

  cout << sum - max_flow(s, t) << endl;

  return 0;
}