Problem p03553 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll MOD = (1e+9) + 7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int n;
ll a[101];
int used[101] = {};
ll dfs(int k) {
  if (k > n)
    return 0;
  if (used[k])
    return dfs(k + 1);
  ll res = 0;
  if (k * 2 > n) {
    Rep1(i, k, n) {
      if (!used[i])
        res += max((ll)0, a[i]);
    }
  } else {
    ll memo[101];
    rep1(i, 100) memo[i] = used[i];
    res = a[k] + dfs(k + 1);
    rep1(i, 100) used[i] = memo[i];
    for (int i = k; i <= n; i += k) {
      used[i] = 1;
    }
    res = max(res, dfs(k + 1));
    rep1(i, 100) used[i] = memo[i];
  }
  return res;
}
int main() {
  cin >> n;
  rep1(i, n) { cin >> a[i]; }
  ll out = dfs(2);
  out = max((ll)0, out + a[1]);
  cout << out << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll MOD = (1e+9) + 7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int n;
ll a[101];
int used[101] = {};
ll dfs(int k) {
  if (k > n)
    return 0;
  if (used[k])
    return dfs(k + 1);
  ll res = 0;
  if (k * 2 > n) {
    Rep1(i, k, n) {
      if (!used[i])
        res += max((ll)0, a[i]);
    }
  } else if (k * 3 > n) {
    if (used[2 * k]) {
      res = max((ll)0, a[k]);
      res += dfs(k + 1);
    } else {
      used[2 * k] = 1;
      res = max({(ll)0, a[k], a[k] + a[2 * k]});
      res += dfs(k + 1);
    }
  } else {
    ll memo[101];
    rep1(i, 100) memo[i] = used[i];
    res = a[k] + dfs(k + 1);
    rep1(i, 100) used[i] = memo[i];
    for (int i = k; i <= n; i += k) {
      used[i] = 1;
    }
    res = max(res, dfs(k + 1));
    rep1(i, 100) used[i] = memo[i];
  }
  return res;
}
int main() {
  cin >> n;
  rep1(i, n) { cin >> a[i]; }
  ll out = dfs(2);
  out = max((ll)0, out + a[1]);
  cout << out << endl;
  return 0;
}