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


#include <bits/stdc++.h>
using namespace std;

// Preprocessor loop
#define CAT_I(x, y) x##y
#define CAT(x, y) CAT_I(x, y)

#define INC0 1
#define INC1 2
#define INC2 3
#define INC3 4
#define INC4 5
#define INC5 6
#define INC6 7
#define INC7 8
#define INC8 9
#define INC(i) CAT(INC, i)

#define EMPTY(...)
#define DEF_COMMA0 _, 1 EMPTY
#define COMMA0() , 0
#define IS_EMPTY_III(f, s) s
#define IS_EMPTY_II(t) IS_EMPTY_III t
#define IS_EMPTY_I(x) IS_EMPTY_II((DEF_##x()))
#define IS_EMPTY(x, ...) IS_EMPTY_I(x COMMA0)

#define IF_0(x, y) y
#define IF_1(x, y) x
#define IF(cond, x, y) CAT(IF_, cond)(x, y)

#define FOR_EACH_I9(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I8(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I7(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I6(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I5(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I4(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I3(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I2(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I1(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I0(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I(i, F, ...)                                                  \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH(F, ...) FOR_EACH_I(0, F, __VA_ARGS__)
#define IF_0(x, y) y
#define IF_1(x, y) x
#define IF(cond, x, y) CAT(IF_, cond)(x, y)

#define FOR_EACH_I9(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I8(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I7(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I6(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I5(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I4(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I3(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I2(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I1(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I0(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I(i, F, ...)                                                  \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH(F, ...) FOR_EACH_I(0, F, __VA_ARGS__)
// Preprocessor loop end

typedef long long ll;
typedef string str;
#define down_queue(x) priority_queue<x>
#define up_queue(x) priority_queue<x, vector<x>, greater<x>>
#define vec(x) vector<x>
ll gcd(ll a, ll b) {
  while (b) {
    ll tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define rep(x, y) for (int x = 0; x < (y); x++)
#define REP(x, z, y) for (int x = (z); x < (y); x++)
#define all(x) x.begin(), x.end()
#define split_str(str, sp_word)                                                \\
  istringstream stream(str);                                                   \\
  string res;                                                                  \\
  for (int cnt = 0; getline(stream, res, sp_word); cnt++)
#define digit(x) ((int)log10((double)(x)) + 1)
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define out(x)                                                                 \\
  for (auto i : x)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl;
#define outln(x)                                                               \\
  for (auto i : x)                                                             \\
    cout << i << " " << endl;
#define vec_cpy(to, from) copy(all(to), back_inserter(from))
#define ary_cpy(to, from) memcpy(to, from, sizeof from)
#define MOD = 1000000007;

#define DEBUG_VAR(v) << #v << "=" << v << " "
#ifdef DEBUG
#define debug_echo(e) cout << "L" << __LINE__ << ": " << e << endl
#define debug_var(...)                                                         \\
  cout << "L" << __LINE__ << ": " FOR_EACH(DEBUG_VAR, __VA_ARGS__) << endl
#define debug_echo_dp1(dp, N)                                                  \\
  {                                                                            \\
    cout << "[ ";                                                              \\
    rep(i, N) cout << dp[i] << " ";                                            \\
    cout << "]" << endl;                                                       \\
  }
#else
#define debug_echo(e)
#define debug_var(...)
#define debug_echo_dp1(dp, N)
#endif

// Implementation

#define MAX_N 5000
int N;
vector<int> p(MAX_N);

int greed(vector<int> &z, int n) {
  if (n >= N - 1)
    return 0;
  debug_var(n, max(0, z[n]), max(0, z[n + 1]));
  z[n + 1] -= max(0, z[n]);
  return greed(z, n + 1) + max(0, z[n]) * 2;
}

int main() {
  cin >> N;
  rep(i, N) cin >> p[i];

  int ans = INT_MAX;
  for (int t = 0; t <= p[0]; t++) {
    vector<int> z = p;
    debug_var(t);
    z[0] -= t;
    z[1] -= t;
    ans = min(ans, p[0] - t + t * 2 + greed(z, 1) + max(0, z[N - 1]));
    debug_var(ans);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;

// Preprocessor loop
#define CAT_I(x, y) x##y
#define CAT(x, y) CAT_I(x, y)

#define INC0 1
#define INC1 2
#define INC2 3
#define INC3 4
#define INC4 5
#define INC5 6
#define INC6 7
#define INC7 8
#define INC8 9
#define INC(i) CAT(INC, i)

#define EMPTY(...)
#define DEF_COMMA0 _, 1 EMPTY
#define COMMA0() , 0
#define IS_EMPTY_III(f, s) s
#define IS_EMPTY_II(t) IS_EMPTY_III t
#define IS_EMPTY_I(x) IS_EMPTY_II((DEF_##x()))
#define IS_EMPTY(x, ...) IS_EMPTY_I(x COMMA0)

#define IF_0(x, y) y
#define IF_1(x, y) x
#define IF(cond, x, y) CAT(IF_, cond)(x, y)

#define FOR_EACH_I9(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I8(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I7(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I6(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I5(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I4(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I3(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I2(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I1(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I0(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I(i, F, ...)                                                  \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH(F, ...) FOR_EACH_I(0, F, __VA_ARGS__)
#define IF_0(x, y) y
#define IF_1(x, y) x
#define IF(cond, x, y) CAT(IF_, cond)(x, y)

#define FOR_EACH_I9(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I8(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I7(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I6(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I5(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I4(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I3(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I2(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I1(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I0(i, F, x, ...)                                              \\
  F(x)                                                                         \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH_I(i, F, ...)                                                  \\
  IF(IS_EMPTY(__VA_ARGS__), EMPTY, CAT(FOR_EACH_I, i))(INC(i), F, __VA_ARGS__)
#define FOR_EACH(F, ...) FOR_EACH_I(0, F, __VA_ARGS__)
// Preprocessor loop end

typedef long long ll;
typedef string str;
#define down_queue(x) priority_queue<x>
#define up_queue(x) priority_queue<x, vector<x>, greater<x>>
#define vec(x) vector<x>
ll gcd(ll a, ll b) {
  while (b) {
    ll tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define rep(x, y) for (int x = 0; x < (y); x++)
#define REP(x, z, y) for (int x = (z); x < (y); x++)
#define all(x) x.begin(), x.end()
#define split_str(str, sp_word)                                                \\
  istringstream stream(str);                                                   \\
  string res;                                                                  \\
  for (int cnt = 0; getline(stream, res, sp_word); cnt++)
#define digit(x) ((int)log10((double)(x)) + 1)
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define out(x)                                                                 \\
  for (auto i : x)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl;
#define outln(x)                                                               \\
  for (auto i : x)                                                             \\
    cout << i << " " << endl;
#define vec_cpy(to, from) copy(all(to), back_inserter(from))
#define ary_cpy(to, from) memcpy(to, from, sizeof from)
#define MOD = 1000000007;

#define DEBUG_VAR(v) << #v << "=" << v << " "
#ifdef DEBUG
#define debug_echo(e) cout << "L" << __LINE__ << ": " << e << endl
#define debug_var(...)                                                         \\
  cout << "L" << __LINE__ << ": " FOR_EACH(DEBUG_VAR, __VA_ARGS__) << endl
#define debug_echo_dp1(dp, N)                                                  \\
  {                                                                            \\
    cout << "[ ";                                                              \\
    rep(i, N) cout << dp[i] << " ";                                            \\
    cout << "]" << endl;                                                       \\
  }
#else
#define debug_echo(e)
#define debug_var(...)
#define debug_echo_dp1(dp, N)
#endif

// Implementation

#define MAX_N 5000
int N;
int p[MAX_N];

int rec(int n) {
  if (n >= N - 1)
    return 0;
  p[n + 1] -= max(0, p[n]);
  debug_var(n, max(0, p[n]), max(0, p[n + 1]));
  return rec(n + 1) + max(0, p[n]) * 2;
}

int main() {
  while (cin >> N, N) {
    rep(i, N) cin >> p[i];

    if (p[0] <= p[1]) {
      cout << rec(0) + max(0, p[N - 1]) << endl;
    } else {
      cout << p[0] + rec(1) + max(0, p[N - 1]) << endl;
    }
  }
}