Problem p03553 (C++)

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

const int kNmax = 110;
const int kInf = numeric_limits<int>::max();

int n, a[kNmax];
int source, sink, flow[kNmax][kNmax], cap[kNmax][kNmax], father[kNmax];
vector<int> g[kNmax];
bool used[kNmax];

bool bfs() {
  for (int i = source; i <= sink; i++) {
    used[i] = false;
    father[i] = -1;
  }

  queue<int> q;
  q.push(source);
  used[source] = true;

  while (!q.empty()) {
    int node = q.front();
    q.pop();
    used[node] = true;

    for (int vec : g[node]) {
      if (!used[vec] && cap[node][vec] > flow[node][vec]) {
        father[vec] = node;
        q.push(vec);
      }
    }
  }
  return used[sink];
}

long long maxFlow() {
  long long flowRes = 0;
  while (bfs()) {
    for (int vec : g[sink]) {
      if (used[vec] && cap[vec][sink] != flow[vec][sink]) {
        father[sink] = vec;
        int minFlow = kInf;
        for (int node = sink; node != source; node = father[node]) {
          minFlow =
              min(minFlow, cap[father[node]][node] - flow[father[node]][node]);
        }
        for (int node = sink; node != source; node = father[node]) {
          flow[father[node]][node] += minFlow;
          flow[node][father[node]] -= minFlow;
        }
        flowRes += minFlow;
      }
    }
  }
  return flowRes;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }

  source = 0;
  sink = n + 1;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > 0) {
      sum += a[i];
      g[i].push_back(sink);
      g[sink].push_back(i);
      cap[i][sink] = a[i];
    } else if (a[i] < 0) {
      g[source].push_back(i);
      g[i].push_back(source);
      cap[source][i] = -a[i];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      g[i].push_back(j);
      g[j].push_back(i);
      cap[i][j] = kInf;
    }
  }

  printf("%lld\n", sum - maxFlow());

  return 0;
}

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

const int kNmax = 110;
const int kInf = numeric_limits<int>::max();

int n, a[kNmax];
int source, sink, flow[kNmax][kNmax], cap[kNmax][kNmax], father[kNmax];
vector<int> g[kNmax];
bool used[kNmax];

bool bfs() {
  for (int i = source; i <= sink; i++) {
    used[i] = false;
    father[i] = -1;
  }

  queue<int> q;
  q.push(source);
  used[source] = true;

  while (!q.empty()) {
    int node = q.front();
    q.pop();
    used[node] = true;
    if (node == sink) {
      break;
    }

    for (int vec : g[node]) {
      if (!used[vec] && cap[node][vec] > flow[node][vec]) {
        father[vec] = node;
        q.push(vec);
      }
    }
  }
  return used[sink];
}

long long maxFlow() {
  long long flowRes = 0;
  while (bfs()) {
    for (int vec : g[sink]) {
      if (used[vec] && cap[vec][sink] != flow[vec][sink]) {
        father[sink] = vec;
        int minFlow = kInf;
        for (int node = sink; node != source; node = father[node]) {
          minFlow =
              min(minFlow, cap[father[node]][node] - flow[father[node]][node]);
        }
        for (int node = sink; node != source; node = father[node]) {
          flow[father[node]][node] += minFlow;
          flow[node][father[node]] -= minFlow;
        }
        flowRes += minFlow;
      }
    }
  }
  return flowRes;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }

  source = 0;
  sink = n + 1;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > 0) {
      sum += a[i];
      g[i].push_back(sink);
      g[sink].push_back(i);
      cap[i][sink] = a[i];
    } else if (a[i] < 0) {
      g[source].push_back(i);
      g[i].push_back(source);
      cap[source][i] = -a[i];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      g[i].push_back(j);
      g[j].push_back(i);
      cap[i][j] = kInf;
    }
  }

  printf("%lld\n", sum - maxFlow());

  return 0;
}