Problem p03553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define inf (1LL << 60)
#define N (105)

using namespace std;

struct edge {
  int nt, to;
  ll flow, cap;
} g[10005];

int head[N], d[N], q[N], a[N], S, T, n, num;
ll ans;

il int gi() {
  RG int x = 0, q = 1;
  RG char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    q = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - 48, ch = getchar();
  return q * x;
}

il void insert(RG int from, RG int to, RG ll cap) {
  g[++num] = (edge){head[from], to, 0, cap}, head[from] = num;
  g[++num] = (edge){head[to], from, 0, 0}, head[to] = num;
  return;
}

il int bfs(RG int S, RG int T) {
  memset(d, 0, sizeof(d));
  RG int h = 0, t = 1;
  q[t] = S, d[S] = 1;
  while (h < t) {
    RG int x = q[++h], v;
    for (RG int i = head[x]; i; i = g[i].nt) {
      v = g[i].to;
      if (!d[v] && g[i].cap > g[i].flow) {
        d[v] = d[x] + 1, q[++t] = v;
        if (v == T)
          return 1;
      }
    }
  }
  return d[T];
}

il ll dfs(RG int x, RG int T, RG ll a) {
  if (!a || x == T)
    return a;
  RG ll f, flow = 0;
  for (RG int i = head[x], v; i; i = g[i].nt) {
    v = g[i].to;
    if (d[v] == d[x] + 1 && g[i].cap > g[i].flow) {
      f = dfs(v, T, min(a, g[i].cap - g[i].flow));
      if (!f) {
        d[v] = -1;
        continue;
      }
      g[i].flow += f, g[i ^ 1].flow -= f;
      flow += f, a -= f;
      if (!a)
        return flow;
    }
  }
  return flow;
}

il ll maxflow(RG int S, RG int T) {
  RG ll flow = 0;
  while (bfs(S, T))
    flow += dfs(S, T, inf);
  return flow;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("mul.in", "r", stdin);
  freopen("mul.out", "w", stdout);
#endif
  n = gi(), num = 1, S = n + 1, T = n + 2;
  for (RG int i = 1; i <= n; ++i) {
    a[i] = gi(), ans += (a[i] > 0) * a[i];
    a[i] > 0 ? insert(i, T, a[i]) : insert(S, i, -a[i]);
    for (RG int j = i + i; j <= n; j += i)
      insert(i, j, inf);
  }
  cout << ans - maxflow(S, T);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define inf (1LL << 60)
#define N (105)

using namespace std;

struct edge {
  int nt, to;
  ll flow, cap;
} g[10005];

int head[N], d[N], q[N], a[N], S, T, n, num;
ll ans;

il int gi() {
  RG int x = 0, q = 1;
  RG char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    q = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - 48, ch = getchar();
  return q * x;
}

il void insert(RG int from, RG int to, RG ll cap) {
  g[++num] = (edge){head[from], to, 0, cap}, head[from] = num;
  g[++num] = (edge){head[to], from, 0, 0}, head[to] = num;
  return;
}

il int bfs(RG int S, RG int T) {
  memset(d, 0, sizeof(d));
  RG int h = 0, t = 1;
  q[t] = S, d[S] = 1;
  while (h < t) {
    RG int x = q[++h], v;
    for (RG int i = head[x]; i; i = g[i].nt) {
      v = g[i].to;
      if (!d[v] && g[i].cap > g[i].flow) {
        d[v] = d[x] + 1, q[++t] = v;
        if (v == T)
          return 1;
      }
    }
  }
  return d[T];
}

il ll dfs(RG int x, RG int T, RG ll a) {
  if (!a || x == T)
    return a;
  RG ll f, flow = 0;
  for (RG int i = head[x], v; i; i = g[i].nt) {
    v = g[i].to;
    if (d[v] == d[x] + 1 && g[i].cap > g[i].flow) {
      f = dfs(v, T, min(a, g[i].cap - g[i].flow));
      if (!f) {
        d[v] = -1;
        continue;
      }
      g[i].flow += f, g[i ^ 1].flow -= f;
      flow += f, a -= f;
      if (!a)
        return flow;
    }
  }
  return flow;
}

il ll maxflow(RG int S, RG int T) {
  RG ll flow = 0;
  while (bfs(S, T))
    flow += dfs(S, T, inf);
  return flow;
}

int main() {
  n = gi(), num = 1, S = n + 1, T = n + 2;
  for (RG int i = 1; i <= n; ++i) {
    a[i] = gi(), ans += (a[i] > 0) * a[i];
    a[i] > 0 ? insert(i, T, a[i]) : insert(S, i, -a[i]);
    for (RG int j = i + i; j <= n; j += i)
      insert(i, j, inf);
  }
  cout << ans - maxflow(S, T);
  return 0;
}