Problem p02603 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vs64 a(n);
  rep(i, n) cin >> a[i];

  map<s64, s64> dp[n + 1];
  dp[0][0] = 1000;

  rep(i, n) {
    for (auto item : dp[i]) {
      s64 kabu = item.first;
      s64 money = item.second;
      // rep (j, money / a[i] + 1) // buy
      // {
      //     dp[i + 1][kabu + j] = max(dp[i + 1][kabu + j], money - j * a[i]);
      // }
      dp[i + 1][kabu + money / a[i]] =
          max(dp[i + 1][kabu + money / a[i]], money - money / a[i] * a[i]);
      dp[i + 1][0] = max(dp[i + 1][0], money + kabu * a[i]);
      dp[i + 1][kabu] = max(dp[i + 1][kabu], money);
      // rep (j, kabu + 1) // sell
      // {
      //     dp[i + 1][kabu - j] = max(dp[i + 1][kabu - j], money + j * a[i]);
      // }
    }
  }

  s64 ans = 0;
  for (auto item : dp[n]) {
    ans = max(ans, item.second);
  }

  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vs32 a(n);
  rep(i, n) cin >> a[i];

  map<int, s64> dp[n + 1];
  dp[0][0] = 1000;

  rep(i, n) {
    for (auto item : dp[i]) {
      s64 kabu = item.first;
      s64 money = item.second;
      rep(j, money / a[i] + 1) // buy
      {
        dp[i + 1][kabu + j] = max(dp[i + 1][kabu + j], money - j * a[i]);
      }
      rep(j, kabu + 1) // sell
      {
        dp[i + 1][kabu - j] = max(dp[i + 1][kabu - j], money + j * a[i]);
      }
    }
  }

  s64 ans = 0;
  for (auto item : dp[n]) {
    ans = max(ans, item.second);
  }

  cout << ans << "\\n";
  return 0;
}