alexjercan/bugnet huggingface dataset:
Problem p01096 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define REP(i, n) FOR(i, 0, (n))

typedef long long ll;

const int M = 10000000;
int dp[400][400];

vector<int> wass;
bool ok(int a, int b) { return abs(wass[a] - wass[b]) <= 1; }
int solve(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l == r)
    return dp[l][r] = 0;
  else if (l == r + 1)
    return dp[l][r] = 0;
  else {
    int ans = 0;
    for (int p = l + 1; p < r; ++p) {
      ans = max(ans, solve(l, p) + solve(p, r));
    }
    if (ok(l, r - 1)) {
      if (solve(l + 1, r - 1) == r - l - 2) {
        ans = r - l;
      }
    }
    return dp[l][r] = ans;
  }
}
int main() {
  while (true) {
    for (int i = 0; i < 400; ++i) {
      for (int j = 0; j < 400; ++j) {
        dp[i][j] = -1;
      }
    }
    int N;
    cin >> N;
    if (!N)
      break;
    wass.assign(N, 0);

    for (int i = 0; i < N; ++i) {
      cin >> wass[i];
    }
    int ans = solve(0, N);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define INF0 static_cast<int>(10e6)
#define INF1 static_cast<int>(10e7)

using namespace std;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vvi = vector<vector<int>>;
using vi = vector<int>;

int n;
int w_[304];
int *w = &w_[2];
vpii ss;
// vi ss_[304];
// int *ss = &ss[2]; // 閉区間

bool ok(int a, int b) { return abs(w[a] - w[b]) <= 1; }

int dp(int, int);

int main() {
  while (cin >> n, n) {
    ss.clear();
    w[-2] = INF0;
    w[-1] = INF1;
    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    w[n] = INF0;
    w[n + 1] = INF1;

    // make sections
    queue<pii> que;
    for (int i = 0; i < n; i++) {
      if (ok(i, i + 1)) {
        ss.emplace_back(make_pair(i, i + 1));
        que.push(make_pair(i, i + 1));
      }
    }

    while (!que.empty()) {
      auto q = que.front();
      que.pop();
      int b = q.first, e = q.second;
      // 1ずつのばす
      if (ok(b - 1, e + 1)) {
        ss.push_back(make_pair(b - 1, e + 1));
        que.push(make_pair(b - 1, e + 1));
      }
      // 上に2
      if (ok(e + 1, e + 2)) {
        ss.push_back(make_pair(b, e + 2));
        que.push(make_pair(b, e + 2));
      }
      // 下に2
      if (ok(b - 2, b - 1)) {
        ss.push_back(make_pair(b - 2, e));
        que.push(make_pair(b - 2, e));
      }
    }
    sort(ss.begin(), ss.end());
    unique(ss.begin(), ss.end());
    // for (auto &x: ss) {
    //     cout << "[" << x.first+1 << ", " << x.second+1 << "]" << " ";
    // }
    cout << dp(0, -1) << endl;
  }
  return 0;
}

int dp(int i, int pre) {
  int res;
  if (i == ss.size()) {
    res = 0;
  } else if (ss[i].first > pre) {
    res = max(dp(i + 1, ss[i].second) + (ss[i].second - ss[i].first + 1),
              dp(i + 1, pre));
  } else {
    res = dp(i + 1, pre);
  }
  return res;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using VI = vector<int>;
using VVI = vector<VI>;
int rec(int l, int r, VI &a, VVI &memo) {
  if (memo[l][r] != -1)
    return memo[l][r];
  if (r - l <= 1)
    return memo[l][r] = 0;
  if (r - l == 2) {
    if (abs(a[l + 1] - a[l]) <= 1)
      return memo[l][r] = 2;
    return memo[l][r] = 0;
  }
  int res = rec(l + 1, r, a, memo);
  for (int i = l + 1; i < r; i++) {
    if (abs(a[i] - a[l]) <= 1 and rec(l + 1, i, a, memo) == i - (l + 1)) {
      res = max(res, i + 1 - l + rec(i + 1, r, a, memo));
    }
  }
  return memo[l][r] = res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    VI a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    VVI memo(n + 1, VI(n + 1, -1));
    cout << rec(0, n, a, memo) << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using VI = vector<int>;
using VVI = vector<VI>;
int rec(int l, int r, VI &a) {
  if (r - l <= 1)
    return 0;
  if (r - l == 2) {
    if (abs(a[l + 1] - a[l]) <= 1)
      return 2;
    return 0;
  }
  int res = rec(l + 1, r, a);
  for (int i = l + 1; i < r; i++) {
    if (abs(a[i] - a[l]) <= 1 and rec(l + 1, i, a) == i - (l + 1)) {
      res = max(res, i + 1 - l + rec(i + 1, r, a));
    }
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    VI a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cout << rec(0, n, a) << '\\n';
  }
  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

bool check(int a, int b) { return abs(a - b) <= 1; }

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> w(n);
    rep(i, n) { cin >> w[i]; }

    int dp[300][300] = {{0}};
    range(W, 2, n + 1) {
      rep(i, n) {
        int j = i + W - 1;
        if (i + W - 1 >= 300)
          break;
        if (check(w[i], w[j]) && dp[i + 1][j - 1] == W - 2) {
          dp[i][j] = W;
        }
        range(k, i, j) { dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]); }
      }
    }
    cout << dp[0][n - 1] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

bool check(int a, int b) { return abs(a - b) <= 1; }

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> w(n);
    rep(i, n) { cin >> w[i]; }

    int dp[300][300] = {{0}};
    range(W, 2, n + 1) {
      rep(i, n) {
        int j = i + W - 1;
        if (check(w[i], w[j]) && dp[i + 1][j - 1] == W - 2) {
          dp[i][j] = W;
        }
        range(k, i, j) { dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]); }
      }
    }
    cout << dp[0][n - 1] << endl;
  }
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const int INF = 1 << 17;

int dp[300][300]; // dp[i][j]:=[i,j)で取り除けるブロックの最大値
vector<int> W;

int rec(int l, int r) { //[r,l)
  if (~dp[l][r])
    return dp[l][r];
  if (r - l <= 1)
    return dp[l][r] = 0;
  else if (r - l == 2) {
    if (abs(W[r - 1] - W[l]) <= 1)
      return dp[l][r] = 2;
    else
      dp[l][r] = 0;
  }
  int ret = 0;
  if (l + 1 < r - 1 && abs(W[l] - W[r - 1]) <= 1 &&
      rec(l + 1, r - 1) == r - l - 2) {
    ret = max(ret, rec(l + 1, r - 1) + 2);
    return dp[l][r] = ret;
  }
  for (int i = l + 1; i < r; i++) {
    ret = max(ret, rec(l, i) + rec(i, r));
  }
  return dp[l][r] = ret;
}

int main() {
  int n;
  while (cin >> n) {
    if (!n)
      break;
    W.clear();
    for (int i = 0; i < n; i++) {
      int w;
      cin >> w;
      W.push_back(w);
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(0, n) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const int INF = 1 << 17;

int dp[300][300]; // dp[i][j]:=[i,j)で取り除けるブロックの最大値
vector<int> W;

int rec(int l, int r) { //[r,l)
  if (~dp[l][r])
    return dp[l][r];
  if (r - l == 0)
    return dp[l][r] = 0;
  else if (r - l == 1)
    return dp[l][r] = 0;
  else if (r - l == 2) {
    if (abs(W[r - 1] - W[l]) <= 1)
      return dp[l][r] = 2;
    else
      dp[l][r] = 0;
  }
  int ret = 0;
  for (int i = l; i < r; i++) {
    for (int j = i + 1; j < r; j++) {
      if (rec(i + 1, j) == j - i - 1 && abs(W[i] - W[j]) <= 1 && i + 1 < j)
        ret = max(ret, rec(i + 1, j) + 2);
      else
        ret = max(ret, rec(i, j) + rec(j, r));
    }
  }
  return dp[l][r] = ret;
}

int main() {
  int n;
  while (cin >> n) {
    if (!n)
      break;
    W.clear();
    for (int i = 0; i < n; i++) {
      int w;
      cin >> w;
      W.push_back(w);
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(0, n) << endl;
  }
  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int N, w[1000];
int dp[1001][1001];

int rec(int left, int right) {
  if (~dp[left][right])
    return dp[left][right];
  if (left >= right)
    return 0;

  bool ret = 0;
  if (!(abs(left - right) % 2)) {
    rec(left + 1, right), rec(left, right - 1);
  } else {
    for (int i = left + 1; i < right; i += 2) {
      ret |= rec(left, i) & rec(i + 1, right);
    }
    ret |= rec(left + 1, right - 1) & (abs(w[left] - w[right]) <= 1);
  }
  return dp[left][right] = ret;
}

int maxi[1001];
int calc(int idx) {

  if (maxi[idx])
    return maxi[idx];
  if (idx > N)
    return 0;

  int ret = 0;
  ret = calc(idx + 1);
  for (int i = idx + 1; i < N; i += 2) {
    if (dp[idx][i]) {
      ret = max(calc(i + 1) + (i - idx + 1), ret);
    }
  }
  return maxi[idx] = ret;
}

int main() {
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      cin >> w[i];
    }

    fill_n(maxi, 1001, 0);
    fill_n(*dp, 1001 * 1001, -1);
    for (int i = 0; i < N - 1; i++) {
      if (abs(w[i] - w[i + 1]) <= 1) {
        dp[i][i + 1] = true;
      }
    }
    rec(0, N - 1);
    cout << calc(0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int N, w[1000];
bool dp[1000][1000];

bool rec(int left, int right) {
  if (dp[left][right])
    return true;
  if (left >= right)
    return false;

  bool ret = false;
  if (!(abs(left - right) % 2)) {
    rec(left + 1, right), rec(left, right - 1);
  } else {
    for (int i = left + 1; i < right; i += 2) {
      ret |= rec(left, i) & rec(i + 1, right);
    }
    ret |= rec(left + 1, right - 1) & (abs(w[left] - w[right]) <= 1);
  }
  return dp[left][right] = ret;
}

int maxi[1001];
int calc(int idx) {

  if (maxi[idx])
    return maxi[idx];
  if (idx > N)
    return 0;

  int ret = 0;
  ret = calc(idx + 1);
  for (int i = idx + 1; i < N; i += 2) {
    if (dp[idx][i]) {
      ret = max(calc(i + 1) + (i - idx + 1), ret);
    }
  }
  return maxi[idx] = ret;
}

int main() {
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      cin >> w[i];
    }

    fill_n(maxi, 1001, 0);
    fill_n(*dp, 1000 * 1000, false);
    for (int i = 0; i < N - 1; i++) {
      if (abs(w[i] - w[i + 1]) <= 1) {
        dp[i][i + 1] = true;
      }
    }
    rec(0, N - 1);
    cout << calc(0) << endl;
  }
}
Problem p01096 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
/*
?????????
*/

constexpr int NUM = 300;
int w[NUM] = {0};
bool dp[NUM][NUM] = {{false}};
int ddp[NUM + 1] = {0};

// a~a+1??¨a+2~b?????\\??\\??\\???a~d??¨d+1~r?????\\??\\??\\???a~b-2??¨b-1~b????????????????????¨???????????????
bool isDroped(int a, int b) {
  if (b - a <= 1)
    return false;
  for (int i = a + 1; i < b; i += 2) {
    if (dp[a][i] && dp[i + 1][b])
      return true;
  }
  return false;
}

int main() {
  int n;
  while (std::cin >> n && n) {
    std::memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++)
      std::cin >> w[i];

    for (int d = 1; d < n; d += 2) {    //?????¨?????????
      for (int i = 0; i < n - d; i++) { //?????¨?????´???
        if ((d == 1 || dp[i + 1][i + d - 1]) &&
            std::abs(w[i] - w[i + d]) <= 1) {
          dp[i][i + d] = true; //???????????????????????¨????????¨??????
        }
        if (isDroped(i, i + d)) {
          dp[i][i + d] = true;
        }
      }
    }
    std::memset(ddp, 0, sizeof(ddp));
    for (int i = 0; i < n; ++i) {
      ddp[i + 1] = std::max(ddp[i + 1], ddp[i]);
      for (int j = i + 1; j < n; j += 2) {
        if (dp[i][j]) {
          ddp[j + 1] = std::max(ddp[j + 1], ddp[i] + j - i + 1);
        }
      }
    }
    std::cout << ddp[n] << std::endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstring>
#include <iostream>

constexpr int NUM = 300;
int w[NUM] = {0};
bool dp[NUM] = {false}; //[???][???] = ?????¨??????????????????

bool isDropedBetween(int a, int b) {
  if ((b - a) <= 1)
    return true;
  for (int i = a + 1; i < b; i++)
    if (!dp[i])
      return false;
  return true;
}

bool isDroped(int a, int b) {
  for (int i = a; i <= b; i++)
    if (!dp[i])
      return false;
  return true;
}

int main() {
  int n;
  while (std::cin >> n && n) {
    std::memset(dp, 0, sizeof(bool) * NUM * NUM);
    for (int i = 0; i < n; i++)
      std::cin >> w[i];

    for (int d = 1; d < n; d += 2) {    //?????¨?????????
      for (int i = 0; i < n - d; i++) { //?????¨?????´???
        bool mightDrop = false;
        if (isDropedBetween(i, i + d))
          mightDrop = true; //???????????????????????¨????????¨??????
        if (isDroped(i, i + d) ||
            (mightDrop && std::abs(w[i] - w[i + d]) <= 1)) {
          for (int j = 0; j <= d; j++)
            dp[i + j] = true;
        }
      }
    }
    int count[2] = {0};
    for (int i = 0; i < NUM; i++) {
      if (dp[i])
        count[1]++;
      else {
        count[0] += count[1] / 2 * 2;
        count[1] = 0;
      }
    }
    std::cout << count[0] << std::endl;
  }
  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<int> a; // 命令 a[1]-a[n] 1-n ->0-9,u
vector<vector<int>> ok;
// vector<vector<int> > doteki;
int n;
int sec(int p, int q);
void pr();

int main() {
  scanf("%d", &n);
  while (n) {
    int f = 0;
    a.assign(n, 0);
    ok.assign(n, a);
    // doteki.assign(n,a);
    for (int t = 0; t < n; t++) {
      scanf("%d", &a[t]);
    }
    for (int s = 0; (s + 1) < n; ++s) {
      ok[s][s + 1] = sec(s, s + 1);
    }
    for (int t = 3; t < n; t = t + 2) {
      for (int s = 0; (s + t) < n; ++s) {
        f = 0;
        for (int u = 1; u < t - 1; u = u + 2) {
          if (ok[s][s + u] && ok[s + u + 1][s + t]) {
            f = 1;
          }
        }
        if (f == 1 || (sec(s, s + t) && ok[s + 1][s + t - 1])) {
          ok[s][s + t] = t + 1;
        }
      }
    }

    for (int x = 1; x < n; ++x) {
      for (int y = 0; y + x < n; ++y) {
        for (int z = y; z < y + x; ++z) {
          ok[y][y + x] = max(ok[y][y + x], ok[y][z] + ok[z + 1][y + x]);
        }
        // pr();
      }
    }
    printf("%d\\n", ok[0][n - 1]);
    scanf("%d", &n);
  }
}

// 変化したところだけ見る

int sec(int p, int q) {
  if (-1 <= (a[p] - a[q]) && (a[p] - a[q]) <= 1) {
    return 2;
    // ok[p][q]=1;
  } else {
    return 0;
    // ok[p][q]=0;
  }
}

void pr() {
  for (int x = 0; x < n; ++x) {
    for (int y = 0; y < n - 1; ++y) {
      printf("%d ", ok[x][y]);
    }
    printf("%d\\n", ok[x][n - 1]);
  }
  printf("\\n");
}


Error: TLE

Fixed submission:

#include <bitset>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<int> a(1); // 命令 a[1]-a[n] 1-n ->0-9,u
int ans;
void next(int z, int r, int y, vector<int> b);
int n;

int main() {
  scanf("%d", &n);
  while (n) {
    ans = n;
    a.assign(n, 0);
    for (int t = 0; t < n; t++) {
      scanf("%d", &a[t]);
    }
    for (int t = 0; t < n - 1; t++) {
      if (-1 <= (a[t] - a[t + 1]) && (a[t] - a[t + 1]) <= 1) {
        next(t, t + 1, n, a);
      }
    }

    ans = n - ans;
    printf("%d\\n", ans);
    scanf("%d", &n);
  }
}

void next(int z, int r, int y, vector<int> b) {
  b[z] = 0;
  b[r] = 0;
  y = y - 2;
  // printf("%ld\\n",b.size());
  if (y < ans) {
    ans = y;
  }
  int s;
  int u;
  for (s = 0; b[s] == 0 && s < n - 1; s++) {
  }
  if (s == n - 1)
    return;
  for (; s < n - 1; s++) {
    for (u = s + 1; b[u] == 0 && u < n; u++) {
    }
    if (u == n)
      continue;
    if (-1 <= (b[s] - b[u]) && (b[s] - b[u]) <= 1) {
      next(s, u, y, b);
    }
  }
}

// 変化したところだけ見る
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
typedef pair<int, int> P;
int N, W[300], dp[300][300];

int rec(int l, int r) {
  if (l >= r)
    return 0;
  if (dp[l][r] != -1)
    return (dp[l][r]);
  int ret = 0;
  if (abs(W[l] - W[r]) <= 1 && rec(l + 1, r - 1) == r - l - 1)
    ret = r - l + 1;
  for (int m = l; m < r; m++)
    ret = max(ret, rec(l, m) + rec(m + 1, r));
  return (dp[l][r] = ret);
}

int main() {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> W[i];
    memset(dp, -1, sizeof(dp));
    cout << rec(0, N - 1) << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
typedef pair<int, int> P;
int N, W[300], dp[300][300];

int rec(int l, int r) {
  if (l >= r)
    return 0;
  if (dp[l][r] != -1)
    return (dp[l][r]);
  int ret = 0;
  if (abs(W[l] - W[r]) <= 1 && rec(l + 1, r - 1) == r - l - 1)
    ret = r - l + 1;
  for (int m = l; m < r; m++)
    ret = max(ret, rec(l, m) + rec(m + 1, r));
  return (dp[l][r] = ret);
}

int main() {
  while (1) {
    cin >> N;
    for (int i = 0; i < N; i++)
      cin >> W[i];
    memset(dp, -1, sizeof(dp));
    cout << rec(0, N - 1) << endl;
  }
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, w[300], maxx[301][301], dp[301][301];
  cin >> n;
  while (n != 0) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        dp[i][j] = 0;
        maxx[i][j] = 0;
      }
    for (int i = 0; i < n; i++)
      cin >> w[i];
    for (int i = 0; i < n - 1; i++)
      if (w[i] - w[i + 1] >= -1 && w[i] - w[i + 1] <= 1)
        dp[i][i + 1] = 1;
    for (int k = 2; k <= n; k++)
      for (int i = 0; i + k < n; i++) {
        if (dp[i + 1][i + k - 1] == 1 && w[i] - w[i + k] >= -1 &&
            w[i] - w[i + k] <= 1)
          dp[i][i + k] = 1;
        else
          for (int j = i + 1; j < i + k - 1; j++)
            if (dp[i][j] == 1 && dp[j + 1][i + k] == 1) {
              dp[i][i + k] = 1;
              break;
            }
      }

    for (int k = 1; k < n; k++)
      for (int i = 0; i + k < n; i++) {
        if (dp[i][i + k] == 1)
          maxx[i][i + k] = k + 1;
        else
          for (int j = i; j < i + k; j++)
            if (maxx[i][i + k] < maxx[i][j] + maxx[j + 1][i + k])
              maxx[i][i + k] = maxx[i][j] + maxx[j + 1][i + k];
      }
    cout << maxx[0][n - 1] << endl;
    cin >> n;
  }
}


Error: TLE

Fixed submission:

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

int dfs(int w[300], int n) {
  int b[300], maxx = 0, a;
  /* for(int i=0;i<n;i++)
     cout<<w[i]<<" ";
   cout<<endl;*/
  if (n < 2)
    return 0;
  if (n == 2 && w[0] - w[1] <= 1 && w[0] - w[1] >= -1)
    return 2;
  for (int i = 0; i < n - 1; i++) {
    if (w[i] - w[i + 1] <= 1 && w[i] - w[i + 1] >= -1) {
      for (int j = 0; j < n; j++) {
        if (j < i)
          b[j] = w[j];
        else if (j > i + 1)
          b[j - 2] = w[j];
      }
      a = dfs(b, n - 2) + 2;
      if (maxx < a)
        maxx = a;
    }
  }
  return maxx;
}

int main() {
  int n, w[300], maxx;
  cin >> n;
  while (n != 0) {
    for (int i = 0; i < n; i++)
      cin >> w[i];
    maxx = dfs(w, n);
    cout << maxx << endl;
    cin >> n;
  }
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

bool ok(int a, int b) { return abs(a - b) <= 1; }

vector<vector<int>> memo(1000, vector<int>(1000, -1));

int solve(const int l, const int r, vector<int> &v) {
  if (memo[l][r] != -1)
    return memo[l][r];
  if (l == r)
    return memo[l][r] = 0;
  else if (l + 1 == r)
    return memo[l][r] = 0;
  else if (l + 2 == r) {
    if (ok(v[l], v[l + 1]))
      return memo[l][r] = 2;
    else
      return memo[l][r] = 0;
  } else {
    int ans = 0;
    for (int k = l + 1; k <= r - 1; ++k) {
      ans = max(ans, solve(l, k, v) + solve(k, r, v));
    }
    if (ok(v[l], v[r - 1]) && solve(l + 1, r - 1, v) == (r - l - 2)) {
      ans = r - l;
    }
    return memo[l][r] = ans;
  }
}

int main() {
  while (1) {
    memo = vector<vector<int>>(1000, vector<int>(1000, -1));
    int N;
    cin >> N;
    vector<int> v(N);
    for (int i = 0; i < N; ++i)
      cin >> v[i];
    if (!N)
      break;
    int ans = solve(0, N, v);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

bool ok(int a, int b) { return abs(a - b) <= 1; }

int solve(int l, int r, vector<int> &v) {
  if (l == r)
    return 0;
  else {
    if (l + 1 == r)
      return 0;
    else if (l + 2 == r) {
      if (ok(v[l], v[l + 1]))
        return 2;
      else
        return 0;
    } else {
      int ans = 0;
      for (int k = l + 1; k < r - 1; ++k) {
        ans = max(ans, solve(l, k, v) + solve(k, r, v));
      }
      if (ok(v[l], v[r - 1]) && solve(l + 1, r - 1, v) == (r - l - 2)) {
        ans = r - l;
      }
      return ans;
    }
  }
}

int main() {
  while (1) {
    int N;
    cin >> N;
    vector<int> v(N);
    for (int i = 0; i < N; ++i)
      cin >> v[i];
    if (!N)
      break;
    int ans = solve(0, N, v);
    cout << ans << endl;
  }
  return 0;
}
Problem p01096 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int n;
vector<int> w;
int dp[310][310];

int calc(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l + 1 >= r)
    return dp[l][r] = 0;
  int res = 0;
  rep(i, l, r) {
    int tmp = 0;
    int cent = calc(l + 1, i);
    int len = i - l - 1;
    if (abs(w[l] - w[i]) <= 1 && len == cent * 2)
      tmp++;
    tmp += cent + calc(i + 1, r);
    res = max(res, tmp);
  }
  return dp[l][r] = res;
}

int main() {
  while (cin >> n, n) {
    w.clear();
    w.resize(n);
    rep(i, 0, n) cin >> w[i];
    rep(i, 0, n + 1) rep(j, 0, n + 1) dp[i][j] = -1;
    int ans = calc(0, n);
    cout << ans * 2 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int n;
vector<int> w;
int dp[310][310];

int calc(int l, int r) {
  if (dp[l][r] != -1)
    return dp[l][r];
  if (l + 1 >= r)
    return dp[l][r] = 0;
  int res = 0;
  rep(i, l, r - 1) {
    rep(j, i + 1, r) {
      int tmp = 0;
      tmp += calc(l, i) + calc(i + 1, j) + calc(j + 1, r);
      int cent = calc(i + 1, j);
      int len = j - i - 1;
      if (abs(w[i] - w[j]) <= 1 && len == cent * 2)
        tmp++;
      res = max(res, tmp);
    }
  }
  return dp[l][r] = res;
}

int main() {
  while (cin >> n, n) {
    w.clear();
    w.resize(n);
    rep(i, 0, n) cin >> w[i];
    rep(i, 0, n + 1) rep(j, 0, n + 1) dp[i][j] = -1;
    int ans = calc(0, n);
    cout << ans * 2 << endl;
  }
}