Problem p03553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

#define LL long long

const int CP = 110;
const int CE = CP * CP * 20;

const LL INF = 0x3f3f3f3f3f3f3f3f;

LL read() {
  LL s = 0, ne = 1;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-')
      ne = -1;
  for (; c >= '0' && c <= '9'; c = getchar())
    s = (s << 1) + (s << 3) + c - '0';
  return s * ne;
}

class fs {
public:
  int from, to, nxt;
  LL cap, flow;
  void init(int f, int t, int n, LL c, LL fl) {
    from = f;
    to = t;
    nxt = n;
    cap = c;
    flow = fl;
  }
} E[CE];
int hd[CP], ecnt = 1;
void add(int x, int y, LL z) {
  E[++ecnt].init(x, y, hd[x], z, 0);
  hd[x] = ecnt;
  E[++ecnt].init(y, x, hd[y], 0, 0);
  hd[y] = ecnt;
}

// v define
int n;
LL a[CP];
void copy(int *a, int *b, int pos, int sz) {
  for (int i = pos; i < pos + sz; i++)
    b[i] = a[i];
}

// dinic
int dep[CP];
bool build(int s, int t) { // 构造分层图
  memset(dep, 0, sizeof(dep));
  dep[s] = 1;

  queue<int> Q;
  Q.push(s);

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();

    for (int k = hd[u]; k; k = E[k].nxt) {
      int v = E[k].to;
      if (!dep[v] && E[k].cap - E[k].flow > 0) {
        dep[v] = dep[u] + 1;
        Q.push(v);
      }
    }
  }

  return dep[t];
}
int cur[CP];                       // 当前弧优化
LL augment(int u, int t, LL rst) { // 多路增广
  if (u == t)
    return rst;

  LL used = 0;
  for (int k = cur[u]; k; k = E[k].nxt) {
    cur[u] = k;
    fs &e = E[k];

    if (dep[e.to] == dep[u] + 1) {
      LL a = augment(e.to, t, min(rst - used, e.cap - e.flow));
      if (a) {
        used += a;
        E[k].flow += a;
        E[k ^ 1].flow -= a;
        if (used == rst)
          return rst;
      }
    }
  }

  return used;
}
LL mf(int s, int t) {
  LL _mf = 0;
  while (build(s, t)) {
    // copy(hd,cur,1,n+2);
    memcpy(cur, hd, sizeof(int) * (n + 4));
    _mf += augment(s, t, INF);
  }
  return _mf;
}

int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();

  for (int i = 1; i <= n; i++)
    for (int k = 2; i * k <= n; k++)
      add(i, i * k, INF);
  int s = n + 1, t = n + 2;
  for (int i = 1; i <= n; i++)
    if (a[i] > 0)
      add(i, t, a[i]);
    else
      add(s, i, -a[i]);

  LL sum = 0, sigma = 0;
  for (int i = 1; i <= n; i++) // 求负权的绝对值之和
    if (a[i] < 0)
      sigma -= a[i];
  for (int i = 1; i <= n; i++)
    sum += a[i];

  printf("%lld", sum + (sigma - mf(s, t)));

  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

#define LL long long

const int CP = 110;
const int CE = CP * CP * 20;

const LL INF = 0x3f3f3f3f3f3f3f3f;

LL read() {
  LL s = 0, ne = 1;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-')
      ne = -1;
  for (; c >= '0' && c <= '9'; c = getchar())
    s = (s << 1) + (s << 3) + c - '0';
  return s * ne;
}

class fs {
public:
  int from, to, nxt;
  LL cap, flow;
  void init(int f, int t, int n, LL c, LL fl) {
    from = f;
    to = t;
    nxt = n;
    cap = c;
    flow = fl;
  }
} E[CE];
int hd[CP], ecnt = 1;
void add(int x, int y, LL z) {
  E[++ecnt].init(x, y, hd[x], z, 0);
  hd[x] = ecnt;
  E[++ecnt].init(y, x, hd[y], 0, 0);
  hd[y] = ecnt;
}

// v define
int n;
LL a[CP];
void copy(int *a, int *b, int pos, int sz) {
  for (int i = pos; i < pos + sz; i++)
    b[i] = a[i];
}

// dinic
int dep[CP];
bool build(int s, int t) { // 构造分层图
  memset(dep, 0, sizeof(dep));
  dep[s] = 1;

  queue<int> Q;
  Q.push(s);

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();

    for (int k = hd[u]; k; k = E[k].nxt) {
      int v = E[k].to;
      if (!dep[v] && E[k].cap - E[k].flow > 0) {
        dep[v] = dep[u] + 1;
        Q.push(v);
      }
    }
  }

  return dep[t];
}
int cur[CP];                       // 当前弧优化
LL augment(int u, int t, LL rst) { // 多路增广
  if (u == t)
    return rst;

  LL used = 0;
  for (int k = cur[u]; k; k = E[k].nxt) {
    // cur[u] = k;
    fs &e = E[k];

    if (dep[e.to] == dep[u] + 1) {
      LL a = augment(e.to, t, min(rst - used, e.cap - e.flow));
      if (a) {
        used += a;
        E[k].flow += a;
        E[k ^ 1].flow -= a;
        if (used == rst)
          return rst;
      }
    }
  }

  return used;
}
LL mf(int s, int t) {
  LL _mf = 0;
  while (build(s, t)) {
    // copy(hd,cur,1,n+2);
    memcpy(cur, hd, sizeof(int) * (n + 4));
    _mf += augment(s, t, INF);
  }
  return _mf;
}

int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();

  for (int i = 1; i <= n; i++)
    for (int k = 2; i * k <= n; k++)
      add(i, i * k, INF);
  int s = n + 1, t = n + 2;
  for (int i = 1; i <= n; i++)
    if (a[i] > 0)
      add(i, t, a[i]);
    else
      add(s, i, -a[i]);

  LL sum = 0, sigma = 0;
  for (int i = 1; i <= n; i++) // 求负权的绝对值之和
    if (a[i] < 0)
      sigma -= a[i];
  for (int i = 1; i <= n; i++)
    sum += a[i];

  printf("%lld", sum + (sigma - mf(s, t)));

  return 0;
}