alexjercan/bugnet huggingface dataset:
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main() {

  int n, answer, a;
  std::cin >> n;
  std::vector<int> number(n - 1);
  std::vector<long int> move(21, 0), moving(21, 0);

  for (int i = 0; i < n - 1; ++i) {
    std::cin >> number[i];
  }
  std::cin >> answer;

  move[number[0]] = 1;

  for (int i = 1; i < n - 1; i++) {
    for (int j = 0; j < 21; j++) {
      if (move[j] != 0) {
        if ((a = j + number[i]) <= 20) {
          moving[a] += move[j];
        }
        if ((a = j - number[i]) >= 0) {
          moving[a] += move[j];
        }
        move[j] = 0;
      }
    }
    for (int j = 0; j < 21; j++) {
      move[j] = moving[j];
      moving[j] = 0;
    }
  }
  std::cout << move[answer] << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

unsigned long int count = 0;
int pointer = 1;
int answer = 0;

void roop(const int *vector, int num, int finish) {
  if (pointer == finish) {
    if (num == answer)
      count++;
    pointer--;
    return;
  }
  if (num + vector[pointer] <= 20) {
    pointer++;
    roop(vector, num + vector[pointer - 1], finish);
  }
  if (num - vector[pointer] >= 0) {
    pointer++;
    roop(vector, num - vector[pointer - 1], finish);
  }
  pointer--;
}

int main() {

  int n;
  std::cin >> n;
  std::vector<int> number(n - 1);

  for (int i = 0; i < n - 1; ++i) {
    std::cin >> number[i];
  }
  std::cin >> answer;

  roop(&number.front(), number[0], n - 1);

  std::cout << count << std::endl;

  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  int x, ans;
  int i = 0;
  std::vector<std::vector<long int>> num(N, std::vector<long int>(21, 0));

  while (N > 0 && std::cin >> x) {
    if (N == 1) {
      ans = x;
      break;
    }

    if (i == 0) {
      ++num[i][x];
      ++i;
      --N;
    } else {
      for (int j = 0; j < 21; ++j) {
        if (num[i - 1][j] > 0) {
          if (j + x <= 20) {
            num[i][j + x] += num[i - 1][j];
          }
          if (j - x >= 0) {
            num[i][j - x] += num[i - 1][j];
          }
        }
      }
      ++i;
      --N;
    }
  }

  for (int j = 0; j < 21; ++j)
    if (ans == j)
      std::cout << num[i - 1][j] << std::endl;
}


Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

int main() {
  int N; // N個の整数
  std::cin >> N;
  int x;
  int n = N;
  std::vector<int> data;
  std::vector<int> sum;
  int ans;
  unsigned int count = 0;

  while (N > 0 && std::cin >> x) {
    data.push_back(x);
    --N;
  }
  ans = data.back();
  data.pop_back();

  for (int i = 0; i < n - 1; ++i) {
    std::vector<int> tmp;
    if (i == 0) {
      sum.push_back(data[i]);
    } else {
      for (int y : sum) {
        if (y + data[i] <= 20) {
          tmp.push_back(y + data[i]);
        }
        if (y - data[i] >= 0) {
          tmp.push_back(y - data[i]);
        }
      }
      sum.clear();
      sum = tmp;
    }
  }

  for (int i = 0; i < sum.size(); ++i)
    if (ans == sum[i])
      count++;

  std::cout << count << std::endl;
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

int main() {
  const int MAX = 20;
  const int MIN = 0;

  int num;
  std::cin >> num;

  std::vector<std::vector<long long int>> dp(
      (num - 1), std::vector<long long int>((MAX + 1), 0));
  int value;
  std::cin >> value;
  dp[0][value] = 1;
  for (int i = 0; i < (num - 2); ++i) {
    std::cin >> value;
    for (int j = 0; j <= MAX; ++j) {
      if ((j + value) <= MAX) {
        dp[i + 1][j] += dp[i][j + value];
      }
      if ((j - value) >= MIN) {
        dp[i + 1][j] += dp[i][j - value];
      }
    }
  }
  std::cin >> value;
  std::cout << dp[num - 2][value] << std::endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int num;
  std::vector<std::vector<int>> dp(num, std::vector<int>(21, 0));
  std::cin >> num;
  std::vector<int> value(num);
  for (int i = 0; i < num; ++i) {
    std::cin >> value[i];
  }
  dp[0][0] = 1;
  for (int i = 0; i < (value.size() - 1); ++i) {
    for (int j = 0; j <= 20; ++j) {
      if ((j + value.at(i)) <= 20) {
        dp[i + 1][j] += dp[i][j + value.at(i)];
      }
      if ((j - value.at(i)) >= 0) {
        dp[i + 1][j] += dp[i][j - value.at(i)];
      }
    }
  }
  std::cout << dp[value.size() - 1][value.at(num - 1)] << std::endl;
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
int main() {
  int a;
  long long int dp[101][101] = {0};
  int x[101];
  std::cin >> a;
  for (int i = 1; i <= a; i++)
    std::cin >> x[i];
  dp[1][x[1]] = 1;

  for (int j = 2; j < a; j++) {
    for (int s = 0; s <= 20; s++) {
      if (s + x[j] <= 20 && s - x[j] >= 0) {
        dp[j][s] = dp[j - 1][s - x[j]] + dp[j - 1][s + x[j]];
      } else if (s + x[j] <= 20 && s - x[j] < 0) {
        dp[j][s] = dp[j - 1][s + x[j]];
      } else if (s + x[j] > 20 && s - x[j] >= 0) {
        dp[j][s] = dp[j - 1][s - x[j]];
      }
    }
  }
  std::cout << dp[a - 1][x[a]] << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
int main() {
  int a;
  int dp[21][101] = {0};
  int x[101];
  std::cin >> a;
  for (int i = 1; i <= a; i++)
    std::cin >> x[i];
  dp[1][x[1]] = 1;

  for (int j = 2; j < a; j++) {
    for (int s = 0; s <= 20; s++) {
      if (s + x[j] <= 20 && s - x[j] >= 0) {
        dp[j][s] = dp[j - 1][s - x[j]] + dp[j - 1][s + x[j]];
      } else if (s + x[j] <= 20 && s - x[j] < 0) {
        dp[j][s] = dp[j - 1][s + x[j]];
      } else if (s + x[j] > 20 && s - x[j] >= 0) {
        dp[j][s] = dp[j - 1][s - x[j]];
      }
    }
  }
  std::cout << dp[a - 1][x[a]] << std::endl;

  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>
using namespace std;
int n;
int a[101];
long long memo[101][21];

long long res(int i, int sum) {
  if (sum < 0 || sum > 20)
    return 0;
  if (memo[i][sum] != 0)
    return memo[i][sum];
  if (i == n - 1)
    return memo[i][sum] = (sum == a[n - 1]);
  return memo[i][sum] = res(i + 1, sum + a[i]) + res(i + 1, sum - a[i]);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  memset(memo, 0, sizeof(memo));
  cout << res(1, a[0]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n;
int a[101];

int res(int i, int sum) {
  if (i == n - 1)
    return sum == a[n - 1];
  if (sum > 20 || sum < 0)
    return 0;
  return res(i + 1, sum + a[i]) + res(i + 1, sum - a[i]);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << res(1, a[0]) << endl;
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define INF 1e8
typedef long long ll;
int n;
vector<int> num;
ll memo[100][21];

ll solve(int index, int ans) {
  if (ans < 0 || ans > 20)
    return 0;
  if (memo[index][ans] != -1)
    return memo[index][ans];
  if (index + 1 == n)
    return (ans == num[index + 1]);
  return (memo[index][ans] = solve(index + 1, ans - num[index + 1]) +
                             solve(index + 1, ans + num[index + 1]));
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    num.push_back(tmp);
  }
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 21; j++) {
      memo[i][j] = -1;
    }
  }
  cout << solve(0, num[0]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef unsigned long long ull;
int n;
ull cnt;
vector<int> num;

void solve(int k, int i) {
  if (k < 0 || k > 20)
    return;
  if (i == n - 2) {
    if (k == num[i + 1])
      cnt++;
    return;
  }
  solve(k + num[i + 1], i + 1);
  solve(k - num[i + 1], i + 1);
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    num.push_back(tmp);
  }
  solve(num[0], 0);
  cout << cnt << endl;
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define LLI long long int

LLI n, num[110], a, memo[1000][30], ans;

using namespace std;

LLI po(int low, int sum) {
  LLI a = 0, b = 0;

  if (memo[low][sum] != 0)
    return memo[low][sum];

  if (low == n - 2) {
    if (sum == num[low + 1])
      return 1;
    return 0;

  } else {
    if (sum + num[low + 1] <= 20)
      a = po(low + 1, sum + num[low + 1]);
    if (sum - num[low + 1] >= 0)
      b = po(low + 1, sum - num[low + 1]);
  }

  ans = a + b;

  memo[low][sum] = ans;
  return ans;
}

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a;
    num[i] = a;
  }

  cout << po(0, num[0]) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LLI long long int

LLI n, num[50], a, memo[1000][30], ans;

using namespace std;

LLI po(int low, int sum) {

  // if(memo[low][sum]!=0)return memo[low][sum];

  if (low == n - 1) {
    if (sum == num[low - 1])
      return 1;
    return 0;
  } else {
    if (sum + num[low] <= 20)
      po(low + 1, sum + num[low]);
    if (sum - num[low] >= 0)
      po(low + 1, sum - num[low]);
  }

  ans = po(low + 1, sum + num[low]) + po(low + 1, sum - num[low]);
  memo[low][sum] = ans;

  return ans;
}

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a;
    num[i] = a;
  }

  cout << po(1, num[0]) << endl;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main() {
  int count;
  std::cin >> count;

  std::vector<int> seq_number;
  seq_number.resize(count);
  for (int i = 0; i < count; ++i) {
    std::cin >> seq_number[i];
  }

  std::vector<std::vector<long int>> table(count, std::vector<long int>(21, 0));
  table[0][seq_number[0]] = 1;

  for (int i = 1; i < count - 1; ++i) {
    for (int j = 0; j < 21; ++j) {
      if (j + seq_number[i] <= 20) {
        table[i][j + seq_number[i]] += table[i - 1][j];
      }
      if (j - seq_number[i] >= 0) {
        table[i][j - seq_number[i]] += table[i - 1][j];
      }
    }
  }

  std::cout << table[count - 2][seq_number.back()] << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

std::vector<int> seq_number;
int count; // count of the number
long long int answer;

// depth-first search
void dfs(int i, int computation) {
  if (i == count - 1) {
    if (computation == seq_number.back()) {
      ++answer;
    }
  } else {
    if (computation <= 20 && computation >= 0) {
      dfs(i + 1, computation + seq_number[i]);
      dfs(i + 1, computation - seq_number[i]);
    }
  }
}

int main() {

  std::cin >> count;
  seq_number.resize(count);
  for (int i = 0; i < count; ++i) {
    std::cin >> seq_number[i];
  }
  answer = 0;
  dfs(0, 0);
  std::cout << answer << std::endl;

  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>

int N, a[112];
long long int dp[112][21];

int main(void) {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &a[i]);
  }

  memset(dp[0], 0, sizeof(dp[0]));
  dp[0][a[0]] = 1;
  for (int i = 1; i < N - 1; ++i) {
    for (int j = 0; j <= 20; ++j) {
      if (0 <= j - a[i]) {
        dp[i][j] += dp[i - 1][j - a[i]];
      }
      if (j + a[i] <= 20) {
        dp[i][j] += dp[i - 1][j + a[i]];
      }
    }
  }

  printf("%lld\\n", dp[N - 2][a[N - 1]]);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

int N, a[112];
long long int dp[112][21];

int do_dp(int num, int sum) {
  if (num == 0) {
    if (sum == a[num]) {
      return dp[num][sum] = 1;
    } else {
      return dp[num][sum] = 0;
    }
  } else {
    if (sum < 0 || 20 < sum) {
      return dp[num][sum] = 0;
    } else {
      return dp[num][sum] =
                 do_dp(num - 1, sum + a[num]) + do_dp(num - 1, sum - a[num]);
    }
  }
}

int main(void) {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &a[i]);
  }

  memset(dp, -1, sizeof(dp));

  printf("%d\\n", do_dp(N - 2, a[N - 1]));
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  long long dp[105][21];
  int n, begin, ans, k;
  cin >> n;
  cin >> begin;
  memset(dp, 0, sizeof(dp));
  dp[0][begin] = 1;
  for (int i = 1; i < n - 1; i++) {
    cin >> k;
    for (int j = 0; j < 21; j++) {
      if (j + k <= 20)
        dp[i][j + k] += dp[i - 1][j];
      if (j - k >= 0)
        dp[i][j - k] += dp[i - 1][j];
    }
  }
  cin >> ans;
  cout << dp[n - 2][ans] << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int NUM[105];
  for (int i = 0; i < n; i++)
    cin >> NUM[i];
  queue<long long> Q;
  Q.push(NUM[0]);
  long long ans = 0;
  for (int i = 1; i < n - 1; i++) {
    int cnt = Q.size();
    int t = 0;
    while (t < cnt) {
      long long u = Q.front();
      Q.pop();
      if (u + NUM[i] <= 20) {
        Q.push(u + NUM[i]);
      }
      if (u - NUM[i] >= 0) {
        Q.push(u - NUM[i]);
      }
      t++;
    }
  }
  while (!Q.empty()) {
    if (Q.front() == NUM[n - 1])
      ans++;
    Q.pop();
  }
  cout << ans << endl;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n;
int d[300];

int main() {
  while (cin >> n) {
    rep(i, n) { cin >> d[i]; }
    int p = 0;
    unsigned long long int dp[100][22] = {0}; // dp[i][j]
    dp[0][d[0]] = 1;
    for (int i = 1; i < n - 1; i++) {
      for (int j = 0; j <= 20; j++) {
        if (dp[i - 1][j] > 0) {
          if (j + d[i] <= 20) {
            dp[i][j + d[i]] += dp[i - 1][j];
          }
          if (j - d[i] >= 0)
            dp[i][j - d[i]] += dp[i - 1][j];
        }
      }
    }
    int sum = 0;
    for (int i = 0; i <= 1000000000; i++) {
      sum += 1;
    }
    cout << dp[n - 2][d[n - 1]] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n;
int d[300];

int main() {
  while (cin >> n) {
    rep(i, n) { cin >> d[i]; }
    int p = 0;
    unsigned long long int dp[100][22] = {0}; // dp[i][j]
    dp[0][d[0]] = 1;
    for (int i = 1; i < n - 1; i++) {
      for (int j = 0; j <= 20; j++) {
        if (dp[i - 1][j] > 0) {
          if (j + d[i] <= 20) {
            dp[i][j + d[i]] += dp[i - 1][j];
          }
          if (j - d[i] >= 0)
            dp[i][j - d[i]] += dp[i - 1][j];
        }
      }
    }
    int sum = 0;
    for (int i = 0; i <= 100000000000; i++) {
      sum += 1;
    }
    cout << dp[n - 2][d[n - 1]] << endl;
  }
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int count_num;
  std::cin >> count_num;

  std::vector<int> fig;
  fig.resize(count_num);
  for (int i = 0; i < count_num; ++i) {
    std::cin >> fig[i];
  }

  std::vector<long int> ans_number;
  ans_number.resize(21);
  ans_number[fig[0]] = 1;
  for (int i = 1; i < count_num - 1; ++i) {
    std::vector<long int> number;
    number.resize(21);

    for (int j = 0; j < 21; ++j) {
      if (ans_number[j] != 0) {
        if (j + fig[i] <= 20)
          number[j + fig[i]] += ans_number[j];
        if (j - fig[i] >= 0)
          number[j - fig[i]] += ans_number[j];
      }
    }

    std::copy(number.begin(), number.end(), ans_number.begin());
  }

  std::cout << ans_number[fig.back()] << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

class Formula {
private:
  std::vector<int> fig;
  int count_ans;
  int size;

public:
  Formula(int n) {
    fig.resize(n);
    size = n;
    count_ans = 0;
  }

  void inFig() {
    for (int i = 0; i < size; ++i) {
      std::cin >> fig[i];
    }
  }

  void formula(int a, int b) {

    if (size == b + 1) {
      if (fig.back() == a) {
        ++count_ans;
      }
      return;
    }

    int p = a + fig[b];
    if (0 <= p && p <= 20)
      formula(p, b + 1);

    int m = a - fig[b];
    if (0 <= m && m <= 20)
      formula(m, b + 1);
  }

  inline void showAnswer() { std::cout << count_ans << std::endl; }
};

int main() {
  int count_num;
  std::cin >> count_num;

  Formula form(count_num);
  form.inFig();
  form.formula(0, 0);
  form.showAnswer();
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define MAX_N 100

int n;
unsigned long long dp[MAX_N][21];
int a[MAX_N];

int main() {
  std::cin >> n;
  for (int i = 0; i < n; ++i)
    std::cin >> a[i];

  dp[0][a[0]] = 1;

  for (int i = 1; i < n - 1; ++i) {
    for (int j = 0; j <= 20; ++j) {
      if (j + a[i] <= 20)
        dp[i][j] += dp[i - 1][j + a[i]];
      if (j - a[i] >= 0)
        dp[i][j] += dp[i - 1][j - a[i]];
    }
  }

  std::cout << dp[n - 2][a[n - 1]] << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#define MAX_N 100

int n;
unsigned long long dp[21][MAX_N];
int a[MAX_N];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);

  dp[0][a[0]] = 1;

  int val;

  for (int i = 1; i < n; ++i) {
    for (int j = 0; j <= 20; ++j) {
      if (dp[i - 1][j] > 0) {
        val = j + a[i];
        if (val <= 20)
          dp[i][val] += dp[i - 1][j];
        val = j - a[i];
        if (val >= 0)
          dp[i][val] += dp[i - 1][j];
      }
    }
  }

  printf("%d\\n", dp[n - 2][a[n - 1]]);
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<long long> dp(21, 0);
  for (int i = 0; i < n - 1; i++) {
    int tmp;
    cin >> tmp;
    if (i) {
      vector<long long> copy = dp;
      dp = vector<long long>(21, 0);
      for (int j = 0; j < copy.size(); j++) {
        int pl = j + tmp, mi = j - tmp;
        if (pl <= 20)
          dp[pl] += copy[j];
        if (mi >= 0)
          dp[mi] += copy[j];
      }
    } else {
      dp[tmp]++;
    }
  }
  int result, count = 0;
  cin >> result;
  cout << dp[result] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n = 0;
  cin >> n;
  vector<int> dp;
  for (int i = 0; i < n - 1; i++) {
    int tmp;
    cin >> tmp;
    if (i) {
      vector<int> copy = dp;
      dp.clear();
      for (int j = 0; j < copy.size(); j++) {
        int pl = copy[j] + tmp, mi = copy[j] - tmp;
        if (pl <= 20)
          dp.push_back(pl);
        if (mi >= 0)
          dp.push_back(mi);
      }
    } else {
      dp.push_back(tmp);
    }
  }
  int result, count = 0;
  cin >> result;
  for (int i = 0; i < dp.size(); i++) {
    if (dp[i] == result)
      count++;
  }
  cout << count << endl;
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 101

int N;
int table[MAX_N];
long long memo[MAX_N][21][2];

long long dfs(int offset, long long val, bool plus) {
  if (memo[offset][val][plus ? 1 : 0] != -1)
    return memo[offset][val][plus ? 1 : 0];
  int v = val;
  if (plus)
    v += table[offset];
  else
    v -= table[offset];
  if (v < 0 || v > 20)
    return 0;
  // end-
  if (offset == N - 1) {
    if (v == table[N])
      return 1;
    else
      return 0;
  }

  return memo[offset][val][plus ? 1 : 0] =
             dfs(offset + 1, v, true) + dfs(offset + 1, v, false);
}

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

  rep(i, MAX_N) rep(j, 21) rep(k, 2) memo[i][j][k] = -1;
  auto answer = dfs(1, table[0], true) + dfs(1, table[0], false);
  cout << answer << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 101

int N;
int table[MAX_N];

int dfs(int offset, int val, bool plus) {
  if (plus)
    val += table[offset];
  else
    val -= table[offset];
  if (val < 0 || val > 20)
    return 0;
  // end-
  if (offset == N - 1) {
    if (val == table[N])
      return 1;
    else
      return 0;
  }

  return dfs(offset + 1, val, true) + dfs(offset + 1, val, false);
}

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

  int answer = dfs(0, 0, true) + dfs(0, 0, false);
  cout << answer << '\\n';
  return 0;
}