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

// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N;
LL A[80];

LL ans = 0;

void solve() {
  // i-1日目に
  // 株を全部売却した時の
  // 所持金の最大値をdp[i]とする
  vector<LL> dp(N + 1, 0);
  dp[0] = 1000;
  // ばらまくイメージ
  // i+1日目になった時点での所持金の最大値を求める
  for (int i = 0; i < N; i++) {
    // 最後に売った日付がj日目の時を考える
    for (int j = 0; j <= i; j++) {
      // j~i日目のうち最も株価が安い日に所持金を目一杯使って買っておく
      LL stock_minimum = INF;
      for (int k = j; k <= i; k++)
        stock_minimum = min(stock_minimum, A[k]);
      dp[i + 1] =
          max(dp[i + 1], dp[j] / stock_minimum * A[i] + dp[j] % stock_minimum);
    }
  }
  ans = dp[N];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  solve();

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N;
LL A[80];

LL ans = 0;

void solve() {
  // i日目に
  // j枚株を持てる時の
  // 所持金の最大値
  vector<map<LL, LL>> dp(N + 1);
  dp[0][0] = 1000;
  // ばらまく
  for (int i = 0; i < N; i++) {
    // map[株] = 所持金
    map<LL, LL> cred;
    for (auto iter = dp[i].begin(); iter != dp[i].end(); iter++) {
      // 現在持っている株の枚数
      LL stock = iter->first;
      // 現在の所持金
      LL money = iter->second;
      // j枚株を買う場合
      for (int j = 0;; j++) {
        LL n_stock = stock + j;
        LL n_money = money - j * A[i];
        if (n_money < 0)
          break;
        cred[n_stock] = max(cred[n_stock], n_money);
      }
      // j枚株を売る場合
      for (int j = 0;; j++) {
        LL n_stock = stock - j;
        LL n_money = money + j * A[i];
        if (n_stock < 0)
          break;
        cred[n_stock] = max(cred[n_stock], n_money);
      }
    }
    LL prev_money = -1;
    for (auto it = (--cred.end()); it != cred.begin(); it--) {
      LL stock = (it)->first;
      LL money = (it)->second;
      // cout << stock << "," << money << endl;
      if (prev_money < money) {
        dp[i + 1][stock] = max(dp[i + 1][stock], money);
        prev_money = money;
      }
    }
    LL stock = (cred.begin())->first;
    LL money = (cred.begin())->second;
    // cout << stock << "," << money << endl;
    if (prev_money < money) {
      dp[i + 1][stock] = max(dp[i + 1][stock], money);
      prev_money = money;
    }
  }

  for (auto iter = dp[N].begin(); iter != dp[N].end(); iter++) {
    ans = max(ans, iter->second);
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  solve();

  cout << ans << endl;
  return 0;
}