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


#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()

#define pb push_back

using namespace std;
void dout() { cerr << endl; }
typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using hll = __int128;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T> void do_uniq(vector<T> vec) {
  sort(all(vec));
  vec.resize(unique(all(vec)) - vec.begin());
}

#ifdef _getchar_nolock
#else
#define _getchar_nolock getchar_unlocked
#endif

template <typename T> void freadT(T &x) {
  x = 0;
  char ch = _getchar_nolock();
  ll sign = 1;

  while (!isdigit(ch)) {
    if (ch == '-')
      sign *= -1;
    ch = _getchar_nolock();
  }

  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = _getchar_nolock();
  }

  x *= sign;
}

#define solvsh
// #define multi

#ifdef solvsh

// const int MAXN = 1e2 + 2;

#define uint ll

uint ml(uint a, uint b) { return (a * 1ll * b); }

uint add(uint a, uint b) { return (a * 1ll + b); }

clock_t timestamp_start = clock();
void time_calc() {
  cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\\n";
}

const int MAXN = 81;
const int MAXSUM = 81 * 200;

ll dp[MAXN];

void solve() {

  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    int curr;
    cin >> curr;
    v.push_back(curr);
  }
  dp[0] = 1000;
  for (int i = 1; i <= n; ++i) {
    ll maxx = dp[i - 1];
    for (int j = 1; j < i; ++j) {
      if (v[j - 1] < v[i - 1]) {
        ll cnt = dp[j] / v[j - 1];
        ll curr = dp[j] - cnt * v[j - 1] + cnt * v[i - 1];

        maxx = max(maxx, curr);
      }
    }

    dp[i] = maxx;
  }

  ll maxx = dp[n];

  cout << maxx;
}

#else

void solve() {}

#endif

void multisolve() {
  //    prec();
  int t;
  cin >> t;

  //    scanf("%d", &t);

  int i = 1;
  while (t--) {
    cout << "Case #" << (i++) << ":\\n";
    solve();
    //        cout << "\\n";
  }
}

#define int int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(20);

#ifdef multi
  multisolve();
#else
  solve();
//    gen();
#endif
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()

#define pb push_back

using namespace std;
void dout() { cerr << endl; }
typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using hll = __int128;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T> void do_uniq(vector<T> vec) {
  sort(all(vec));
  vec.resize(unique(all(vec)) - vec.begin());
}

#ifdef _getchar_nolock
#else
#define _getchar_nolock getchar_unlocked
#endif

template <typename T> void freadT(T &x) {
  x = 0;
  char ch = _getchar_nolock();
  ll sign = 1;

  while (!isdigit(ch)) {
    if (ch == '-')
      sign *= -1;
    ch = _getchar_nolock();
  }

  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = _getchar_nolock();
  }

  x *= sign;
}

#define solvsh
// #define multi

#ifdef solvsh

// const int MAXN = 1e2 + 2;

#define uint ll

uint ml(uint a, uint b) { return (a * 1ll * b); }

uint add(uint a, uint b) { return (a * 1ll + b); }

clock_t timestamp_start = clock();
void time_calc() {
  cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\\n";
}

const int MAXN = 81;
const int MAXSUM = 81 * 200;

bool dp[MAXSUM][81][MAXN];

void solve() {

  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    int curr;
    cin >> curr;
    v.push_back(curr);
  }

  dp[1000][0][0] = true;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < MAXSUM; ++j) {
      for (int t = 0; t < MAXN; ++t) {
        int curr = j - v[i];
        int cnt = 1;
        while (curr >= 0 && t + cnt <= MAXN) {
          dp[curr][i + 1][t + cnt] |= dp[j][i][t];
          cnt++;
          curr -= v[i];
        }
        curr = j + v[i];
        cnt = 1;

        while (curr < MAXSUM && t >= cnt) {
          dp[curr][i + 1][t - cnt] |= dp[j][i][t];
          curr += v[i];
          ++cnt;
        }
        dp[j][i + 1][t] |= dp[j][i][t];
      }
    }
  }
  int maxx = 0;
  for (int i = 0; i < MAXN; ++i) {
    for (int j = 0; j < MAXSUM; ++j) {
      if (dp[j][n][i])
        maxx = max(maxx, j);
    }
  }

  cout << maxx;
}

#else

void solve() {}

#endif

void multisolve() {
  //    prec();
  int t;
  cin >> t;

  //    scanf("%d", &t);

  int i = 1;
  while (t--) {
    cout << "Case #" << (i++) << ":\\n";
    solve();
    //        cout << "\\n";
  }
}

#define int int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(20);

#ifdef multi
  multisolve();
#else
  solve();
//    gen();
#endif
}