Problem p03553 (C++)

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

#define DEBUG(x) cout << #x << ": " << x << endl;
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  for (int i = 0; i < v.size(); i++)                                           \
    cout << " " << v[i];                                                       \
  cout << endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
const int inf = 1000000001;
const ll INF = 1e18 * 4;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout << setprecision(15) << fixed << p << endl;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx2[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy2[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int n;
vii yakusuu;
ll cnt = 0;
ll br(int i0, vector<bool> used, vl a) {
  for (int i = i0 + 1; i <= n; i++) {
    if (a[i] >= 0) {
      continue;
    }
    bool flag = false;
    for (int j = 0; j < yakusuu[i].size(); j++) {
      if (used[yakusuu[i][j]]) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      ll ans1 = br(i, used, a);
      used[i] = true;
      for (int j = 1; i * j <= n; j++) {
        a[i * j] = 0;
      }
      ll ans2 = br(i, used, a);
      return max(ans1, ans2);
    }
  }
  ll res = 0;
  for (int j = 1; j <= n; j++) {
    res += a[j];
  }
  return res;
}

int main() {
  int i, j;
  cin >> n;
  yakusuu.resize(n + 1);
  vl a(n + 1);
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (i >= n / 2 + 1) {
      a[i] = max(0LL, a[i]);
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 2; j <= i; j++) {
      if (i % j == 0) {
        yakusuu[i].push_back(j);
      }
    }
  }
  vector<bool> used(n + 1, false);
  ll ans = 0;
  for (i = 2; i <= n; i++) {
    ll ans1 = 0, ans2 = 0;
    if (a[i] < 0) {
      ll ans1 = br(i, used, a);
      used[i] = true;
      for (j = 1; i * j <= n; j++) {
        a[i * j] = 0;
      }
      ll ans2 = br(i, used, a);
      ans = max(ans1, ans2);
      break;
    }
  }
  ll sum = 0;
  for (i = 1; i <= n; i++) {
    sum += a[i];
  }
  ans = max(ans, sum);
  ans = max(ans, 0LL);
  cout << ans << endl;
  /*
  vll b;
  vl s;
  s.push_back(sum);
  b.push_back(a);
  for (i = n / 2; i > 0; i--) {
          for (int k = 0; k < b.size(); k++) {
                  if (b[k][i] < 0) {
                          ll get = 0;
                          for (j = 1; i*j <= n; j++) {
                                  get -= b[k][i*j];
                          }
                          if (get >= 0) {
                                  for (j = 1; i*j <= n; j++) {
                                          b[k][i*j] = 0;
                                  }
                                  s[k] += get;
                          }
                          else if (get == 0) {
                                  b.push_back(b[k]);
                                  s.push_back(s[k]);
                                  for (j = 1; i*j <= n; j++) {
                                          b[k][i*j] = 0;
                                  }
                                  s[k] += get;
                          }
                  }
          }
  }
  */
}

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

#define DEBUG(x) cout << #x << ": " << x << endl;
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  for (int i = 0; i < v.size(); i++)                                           \
    cout << " " << v[i];                                                       \
  cout << endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define vs vector<string>
#define pii pair<int, int>
#define pis pair<int, string>
#define psi pair<string, int>
#define pll pair<ll, ll>
const int inf = 1000000001;
const ll INF = 1e18 * 4;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout << setprecision(15) << fixed << p << endl;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx2[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy2[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int n;
vii yakusuu;
ll cnt = 0;
ll br(int i0, vector<bool> used, vl a) {
  for (int i = i0 + 1; i <= n / 2; i++) {
    if (a[i] >= 0) {
      continue;
    }
    bool flag = false;
    for (int j = 0; j < yakusuu[i].size(); j++) {
      if (used[yakusuu[i][j]]) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      ll ans1 = br(i, used, a);
      used[i] = true;
      for (int j = 1; i * j <= n; j++) {
        a[i * j] = 0;
      }
      ll ans2 = br(i, used, a);
      return max(ans1, ans2);
    }
  }
  ll res = 0;
  for (int j = 1; j <= n; j++) {
    res += a[j];
  }
  return res;
}

int main() {
  int i, j;
  cin >> n;
  yakusuu.resize(n + 1);
  vl a(n + 1);
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (i >= n / 2 + 1) {
      a[i] = max(0LL, a[i]);
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 2; j <= i; j++) {
      if (i % j == 0) {
        yakusuu[i].push_back(j);
      }
    }
  }
  vector<bool> used(n + 1, false);
  ll ans = 0;
  for (i = 2; i <= n; i++) {
    ll ans1 = 0, ans2 = 0;
    if (a[i] < 0) {
      ll ans1 = br(i, used, a);
      used[i] = true;
      for (j = 1; i * j <= n; j++) {
        a[i * j] = 0;
      }
      ll ans2 = br(i, used, a);
      ans = max(ans1, ans2);
      break;
    }
  }
  ll sum = 0;
  for (i = 1; i <= n; i++) {
    sum += a[i];
  }
  ans = max(ans, sum);
  ans = max(ans, 0LL);
  cout << ans << endl;
  /*
  vll b;
  vl s;
  s.push_back(sum);
  b.push_back(a);
  for (i = n / 2; i > 0; i--) {
          for (int k = 0; k < b.size(); k++) {
                  if (b[k][i] < 0) {
                          ll get = 0;
                          for (j = 1; i*j <= n; j++) {
                                  get -= b[k][i*j];
                          }
                          if (get >= 0) {
                                  for (j = 1; i*j <= n; j++) {
                                          b[k][i*j] = 0;
                                  }
                                  s[k] += get;
                          }
                          else if (get == 0) {
                                  b.push_back(b[k]);
                                  s.push_back(s[k]);
                                  for (j = 1; i*j <= n; j++) {
                                          b[k][i*j] = 0;
                                  }
                                  s[k] += get;
                          }
                  }
          }
  }
  */
}