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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, s, e) for (int i = s; i <= e; i++)
#define repd(i, s, e) for (int i = s; i >= e; i--)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;

int n, m;
const int MAX_N = 101;
const int MAX_I = 21;
ll dp[MAX_N][MAX_I];

int main() {
  cin >> n;
  memset(&dp[0][0], 0, sizeof(ll) * MAX_N * MAX_I);
  cin >> m;
  dp[1][m] = 1;
  for (int i = 2; i < n; i++) {
    cin >> m;
    for (int j = 0; j < MAX_I; j++) {
      if (j + m < MAX_I)
        dp[i][j + m] += dp[i - 1][j];
      if (j - m >= 0)
        dp[i][j - m] += dp[i - 1][j];
    }
  }

  cin >> m;
  printf("%lld\\n", dp[n - 1][m]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, s, e) for (int i = s; i <= e; i++)
#define repd(i, s, e) for (int i = s; i >= e; i--)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;

int n, m;
const int MAX_N = 101;
const int MAX_I = 21;
ll dp[MAX_N][MAX_I];

int main() {
  while (cin >> n, n) {
    memset(&dp[0][0], 0, sizeof(ll) * MAX_N * MAX_I);
    cin >> m;
    dp[1][m] = 1;
    for (int i = 2; i < n; i++) {
      cin >> m;
      for (int j = 0; j < MAX_I; j++) {
        if (j + m < MAX_I)
          dp[i][j + m] += dp[i - 1][j];
        if (j - m >= 0)
          dp[i][j - m] += dp[i - 1][j];
      }
    }

    cin >> m;
    printf("%lld\\n", dp[n - 1][m]);
  }
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

// 線形探索やら範囲探索？やらすると、計算量が半端ない。正解の解の数自体が多すぎるので、限定もしづらい。
// N回目に符号をつける数は、どのパターンでも一緒であるので、
// N-1回目までに演算して出てきた値が同じのものは、N回目の演算における結果が一致する。
// 結果のバリエーションが少なく、結果が同じものはその前に依存せず、次の結果も同じになる。
// よって、結果によって処理をまとめておこなう、動的計画法とやらが使えそう。
#include <stdio.h>

int main() {
  FILE *fp1, *fp2;
  int num;
  int i, j, d, ans;
  long long data[99][21] = {
      0}; // data[i][j]には、i回目の演算でjという値がでる場合の数を保存.

  // fp1 = fopen("input.txt","r");
  // fp2 = fopen("output.txt","w");
  fscanf(stdin, "%d", &num);
  fscanf(stdin, "%d", &d); // 最初の数字は0以上20以下である。
  data[0][d] = 1;
  for (i = 1; i < num - 1; i++) {
    fscanf(stdin, "%d", &d);
    for (j = 0; j < 21; j++) {
      if (j + d <= 20)
        data[i][j + d] += data[i - 1][j];
      if (j - d >= 0)
        data[i][j - d] += data[i - 1][j];
    }
  }
  fscanf(stdin, "%d", &ans);
  fprintf(stdout, "%lld\\n", data[num - 2][ans]);
  // fclose(fp1);
  // fclose(fp2);
  return 0;
}

Error: -11

Fixed submission:

// 線形探索やら範囲探索？やらすると、計算量が半端ない。正解の解の数自体が多すぎるので、限定もしづらい。
// N回目に符号をつける数は、どのパターンでも一緒であるので、
// N-1回目までに演算して出てきた値が同じのものは、N回目の演算における結果が一致する。
// 結果のバリエーションが少なく、結果が同じものはその前に依存せず、次の結果も同じになる。
// よって、結果によって処理をまとめておこなう、動的計画法とやらが使えそう。
#include <stdio.h>

int main() {
  FILE *fp1, *fp2;
  int num;
  int i, j, d, ans;
  long long data[99][21] = {
      0}; // data[i][j]には、i回目の演算でjという値がでる場合の数を保存.

  fp1 = fopen("input.txt", "r");
  fp2 = fopen("output.txt", "w");
  fscanf(fp1, "%d", &num);
  fscanf(fp1, "%d", &d); // 最初の数字は0以上20以下である。
  data[0][d] = 1;
  for (i = 1; i < num - 1; i++) {
    fscanf(fp1, "%d", &d);
    for (j = 0; j < 21; j++) {
      if (j + d <= 20)
        data[i][j + d] += data[i - 1][j];
      if (j - d >= 0)
        data[i][j - d] += data[i - 1][j];
    }
  }
  fscanf(fp1, "%d", &ans);
  fprintf(fp2, "%lld", data[num - 2][ans]);
  fclose(fp1);
  fclose(fp2);
  return 0;
}
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 <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int n;
int a[101];
ll dp[101][1000];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 1000; j++) {
      dp[i][j] = 0ll;
    }
  }
  dp[0][a[0]] = 1ll;
  for (int i = 0; i < n - 2; i++) {
    for (int j = 0; j <= 20; j++) {
      if (j - a[i + 1] >= 0)
        dp[i + 1][j] += dp[i][j - a[i + 1]];
      if (j + a[i + 1] <= 20)
        dp[i + 1][j] += dp[i][j + a[i + 1]];
    }
  }
  cout << dp[n - 2][a[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 <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
ll ans = 0;
int n;
int a[101];
int memo[25][101];
void rec(int sum, int d) {
  if (d == n - 1) {
    if (sum == a[n - 1])
      ans++;
    return;
  }
  if (sum < 0 || sum > 20)
    return;
  rec(sum + a[d], d + 1);
  rec(sum - a[d], d + 1);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  rec(0, 0);
  cout << ans << endl;
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int n;
  cin >> n;
  int num[101];
  for (int i = 0; i < n - 1; i++)
    cin >> num[i];
  int dest;
  cin >> dest;

  long long a[21][101] = {};
  a[num[0]][0] = 1;
  for (int i = 1; i < n - 1; i++)
    for (int j = 0; j < 21; j++)
      a[j][i] += ((j + num[i] < 21) ? a[j + num[i]][i - 1] : 0) +
                 ((j - num[i] >= 0) ? a[j - num[i]][i - 1] : 0);
  cout << a[dest][n - 1 - 1] << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int n;
  cin >> n;
  int num[100];
  for (int i = 0; i < n - 1; i++)
    cin >> num[i];
  int dest;
  cin >> dest;

  long long a[20][100] = {};
  a[0][0] = a[num[0]][0] = 1;
  for (int i = 1; i < n - 1; i++)
    for (int j = 0; j < 20; j++)
      a[j][i] += (j + num[i] < 20) * a[j + num[i]][i - 1] +
                 (j - num[i] >= 0) * a[j - num[i]][i - 1];
  cout << a[dest][n - 1 - 1] << endl;

  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

int main() {
  long long dp[21][100] = {};
  std::vector<int> numbers;
  int num, x;

  std::cin >> num;

  for (int i = 0; i < num; ++i) {
    std::cin >> x;
    numbers.push_back(x);
  }

  dp[numbers.at(0)][0] = 1; // first step

  for (int i = 1; i < num - 1; ++i) {

    for (int j = numbers.at(i); j < 21; ++j) { // 0 < j - numbers.at(i)
      dp[j - numbers.at(i)][i] += dp[j][i - 1];
    }

    for (int j = 0; j < 21 - numbers.at(i); ++j) { // j + numbers.at(i) < 21
      dp[j + numbers.at(i)][i] += dp[j][i - 1];
    }
  }

  std::cout << dp[numbers.at(num - 1)][num - 2] << std::endl; // n-2 = (n-1)-1
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>

int main() {
  int n, number, plus, minus;
  long long start;

  while (std::cin >> n) {

    std::vector<int> numbers;

    for (int i = 0; i < n; ++i) {
      std::cin >> number;
      numbers.push_back(number);
    }

    int whole = (int)pow(2.0, (double)(n - 2));
    std::vector<long long> process;
    std::vector<long long> exception(whole, 0);

    for (int j = 0; j < n - 1; ++j) {

      if (j == 0) { // first step

        process.push_back(numbers.at(j));

      } else {

        start = (long long)pow(2.0, (double)(j - 1));

        for (long long k = 0; k < start; ++k) {
          plus = process.at(start - 1 + k) + numbers.at(j);
          minus = process.at(start - 1 + k) - numbers.at(j);

          if (plus > 20 || plus < 0) {
            process.push_back(plus);
            for (int l =
                     (int)process.size() * pow(2.0, (double)(n - 1 - (j + 1)));
                 l < (int)process.size() * pow(2.0, (double)(n - 1 - (j + 1))) +
                         pow(2.0, (double)(n - 1 - (j + 1)));
                 ++l) {
              exception.at(l - whole) = 1;
            }
            process.push_back(minus);

          } else if (minus > 20 || minus < 0) {
            process.push_back(plus);

            process.push_back(minus);
            for (int l =
                     (int)process.size() * pow(2.0, (double)(n - 1 - (j + 1)));
                 l < (int)process.size() * pow(2.0, (double)(n - 1 - (j + 1))) +
                         pow(2.0, (double)(n - 1 - (j + 1)));
                 ++l) {
              exception.at(l - whole) = 1;
            }

          } else {
            process.push_back(plus);
            process.push_back(minus);
          }
        }
      }
    }

    long long size = (long long)process.size();
    long long sum = 0;
    int j = 0;

    for (long long i = size - whole; i < size; ++i) {
      if (process.at(i) == numbers.back() && exception.at(j) != 1) {
        ++sum;
      }
      ++j;
    }

    std::cout << sum << std::endl;
  }
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define loop(i, n) for (int i = 0; i < n; i++)
#define int long long
int n;
int d[102];
int num[21];
int mem[21];

signed main() {
  int v;
  cin >> n;
  n--;
  loop(i, n) cin >> d[i];
  cin >> v;
  loop(i, n) {
    if (i == 0) {
      num[d[i]] = 1;
      continue;
    }
    loop(j, 21) mem[j] = 0;
    loop(j, 21) {
      if (0 <= j - d[i])
        mem[j - d[i]] += num[j];
      if (j + d[i] <= 20)
        mem[j + d[i]] += num[j];
    }
    loop(j, 21) num[j] = mem[j];
  }
  cout << num[v] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int n;
int d[102];

int dfs(int i, char sign, int sum) {
  if (sign == '+')
    sum += d[i];
  if (sign == '-')
    sum -= d[i];
  if (sum < 0 || 20 < sum)
    return 0;
  if (i == n - 2)
    return sum == d[n - 1] ? 1 : 0;
  return dfs(i + 1, '+', sum) + dfs(i + 1, '-', sum);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  cout << dfs(0, '+', 0) << endl;
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<vector<long long unsigned>> dp(N, vector<long long unsigned>(21, 0));
  int num;
  cin >> num;
  dp[0][num] = 1;
  rep(i, 1, N) {
    cin >> num;
    repe(j, 0, 20) {
      if (j + num <= 20)
        dp[i][j + num] += dp[i - 1][j];
      if (j - num >= 0)
        dp[i][j - num] += dp[i - 1][j];
    }
  }
  cin >> num;
  cout << dp[N - 2][num] << endl;

  return 0;
}

Error: -11

Fixed submission:

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

#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<vector<long long unsigned>> dp(N, vector<long long unsigned>(21, 0));
  int num;
  cin >> num;
  dp[0][num] = 1;
  rep(i, 0, N - 1) {
    cin >> num;
    repe(j, 0, 20) {
      if (j + num <= 20)
        dp[i][j + num] += dp[i - 1][j];
      if (j - num >= 0)
        dp[i][j - num] += dp[i - 1][j];
    }
  }
  cin >> num;
  cout << dp[N - 2][num] << endl;

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

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ll;
ll dfs(int sum, int cou);
int n;
int num[101];
ll dp[30][110];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> num[i];
  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < 110; j++) {
      dp[i][j] = -1;
    }
  }
  cout << dfs(num[0], 1) << endl;
}

ll dfs(int sum, int cou) {
  if (cou == n - 1) {
    if (sum == num[cou])
      return 1;
    else
      return 0;
  }
  if (dp[sum][cou] != -1)
    return dp[sum][cou];
  ll ret = 0;
  if (sum - num[cou] >= 0)
    ret += dfs(sum - num[cou], cou + 1);
  if (sum + num[cou] <= 20)
    ret += dfs(sum + num[cou], cou + 1);
  return dp[sum][cou] = ret;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;

ll dfs(int now, int cou);
int n;
int num[101];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  cout << dfs(0, 0) << endl;
}

ll dfs(int now, int cou) {
  if (cou == n - 1) {
    if (now == num[cou])
      return 1;
    else
      return 0;
  }
  if (now < 0 || now > 20)
    return 0;
  ll res = 0;
  res += dfs(now + num[cou], cou + 1);
  res += dfs(now - num[cou], cou + 1);
  return res;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
long long int fie[101][21];
int main() {
  long long int N, a;
  cin >> N;
  cin >> a;
  fie[0][a] = 1;
  for (int i = 1; i < N - 1; i++) {
    cin >> a;
    for (int j = 0; j <= 20; j++) {
      if (0 <= j - a)
        fie[i][j] += fie[i - 1][j - a];
      if (j + a <= 20)
        fie[i][j] += fie[i - 1][j + a];
    }
  }
  cin >> a;
  cout << fie[N - 2][a] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
int n, num[100], ansnum;
long long int ans = 0;
void judge(int a, int nowsum) {
  if (a + 2 == n) {
    if (nowsum == ansnum)
      ans++;
  } else if (0 <= nowsum && nowsum <= 20) {
    judge(a + 1, nowsum + num[a + 1]);
    judge(a + 1, nowsum - num[a + 1]);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++)
    scanf("%d", &num[i]);
  scanf("%d", &ansnum);
  judge(0, num[0]);
  printf("%lld\\n", ans);
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n, a[110] = {0};
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  unsigned long long int dp[110][21] = {{0}};
  dp[1][a[1]] = 1;
  for (int i = 2; i < n; 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]];
      }
    }
  }
  printf("%lld\\n", dp[n - 1][a[n]]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int a[1000] = {0};
int x;
long long solve(int n, int ans, int sum) {
  // cout << "in" << endl;
  if (sum > 20 || sum < 0)
    return 0;
  if (n == x - 1 && sum == a[x - 1])
    return 1;
  if (n == x - 1)
    return 0;
  return solve(n + 1, ans, sum + a[n]) + solve(n + 1, ans, sum - a[n]);
}
int main() {
  scanf("%d", &x);
  for (int i = 0; i < x; i++) {
    scanf("%d", &a[i]);
  }
  printf("%lld\\n", solve(1, a[x - 1], a[0]));
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
// typedef pair<int, int> P;

/** Problem0557 : A First Grader **/
int main() {
  int N, num[100], a;
  long long int dp[21][100] = {0};

  cin >> N;
  rep(i, N - 1) { cin >> num[i]; }
  cin >> a;

  dp[num[0]][0] = 1;

  REP(i, 1, N - 1) {
    rep(j, 21) {
      int b = j + num[i];
      int c = j - num[i];
      if (0 <= b && b <= 20)
        dp[b][i] += dp[j][i - 1];
      if (0 <= c && c <= 20)
        dp[c][i] += dp[j][i - 1];
    }
  }

  cout << dp[a][N - 2] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
// typedef pair<int, int> P;

/** Problem0557 : A First Grader **/
int main() {
  int N, num[100], a;
  long int dp[21][100] = {0};

  cin >> N;
  rep(i, N - 1) { cin >> num[i]; }
  cin >> a;

  dp[num[0]][0] = 1;

  REP(i, 1, N - 1) {
    rep(j, 21) {
      int b = j + num[i];
      int c = j - num[i];
      if (0 <= b && b <= 20)
        dp[b][i] += dp[j][i - 1];
      if (0 <= c && c <= 20)
        dp[c][i] += dp[j][i - 1];
    }
    cerr << endl;
  }

  cout << dp[a][N - 2] << endl;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

long long dp[128][21] = {0};

int main(void) {

  int N;
  int field[128];
  int ans;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> field[i];
  }
  cin >> ans;
  dp[0][field[0]] = 1;
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < 21; j++) {
      if (dp[i][j] != 0) {
        if (j + field[i + 1] <= 20) {
          dp[i + 1][j + field[i + 1]] += dp[i][j];
        }
        if (j - field[i + 1] >= 0) {
          dp[i + 1][j - field[i + 1]] += dp[i][j];
        }
      }
    }
  }

  cout << dp[N - 2][ans] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>

#define INF (1e+9)
#define LLINF (1e+18)
#define LL long long

using namespace std;

int num;
int array[128];

LL func(int sum, int i) {
  LL n = 0;
  if (sum < 0)
    return 0;
  if (sum > 20)
    return 0;
  if (i == num - 1) {
    if (sum == array[num - 1])
      return 1;
    else
      return 0;
  }
  n += func(sum + array[i], i + 1);
  n += func(sum - array[i], i + 1);
  return n;
}

int main(void) {

  scanf("%d", &num);
  for (int i = 0; i < num; i++) {
    scanf("%d", array + i);
  }

  printf("%lld\\n", func(0, 0));

  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
long long dp[100][21];
int main() {
  int n, s[100], w;
  cin >> n;
  for (int i = 0; i < n - 1; i++)
    cin >> s[i];
  cin >> w;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 20; j++) {
      dp[i][j] = 0;
    }
  }
  dp[0][s[0]] = 1;
  for (int i = 1; i < n - 1; i++) {
    for (int j = 0; j <= 20; j++) {
      if (j - s[i] >= 0)
        dp[i][j] += dp[i - 1][j - s[i]];
      if (j + s[i] <= 20)
        dp[i][j] += dp[i - 1][j + s[i]];
    }
  }
  cout << dp[n - 2][w] << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>

using namespace std;
int dp[100][1000];
int main() {
  int a;
  int s[100];
  int b;
  int n;
  int w = 0;
  cin >> n;
  cin >> a;
  for (int i = 0; i < n - 2; i++) {
    cin >> s[i];
    w += s[i];
  }
  w /= 2;
  cin >> b;
  w += b;
  w -= a;
  for (int i = 0; i < n - 2; i++) {
    for (int j = 0; j < w + 1; j++) {
      dp[i][j] += dp[i - 1][j];
      dp[i][j] += dp[i][j - s[i]];
    }
  }
  cout << dp[n - 3][w] << endl;
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define __int64 long long
__int64 dp[110][21];
int ta[110], t;
const int INF = 0;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++)
    cin >> ta[i];
  cin >> t;
  for (int i = 0; i < 110; i++)
    for (int j = 0; j < 21; j++)
      dp[i][j] = INF;
  dp[1][ta[1]] = 1;
  for (int i = 2; i <= n - 1; i++) {
    for (int j = 0; j <= 20; j++) {
      if (j - ta[i] >= 0 && dp[i - 1][j - ta[i]] != INF)
        dp[i][j] += dp[i - 1][j - ta[i]];
      if (j + ta[i] <= 20 && dp[i - 1][j + ta[i]] != INF)
        dp[i][j] += dp[i - 1][j + ta[i]];
    }
  }
  cout << dp[n - 1][t] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int ban[500], a;
int ans;
int dfs(int sum, int hukasa) {
  if (hukasa == a - 1) {
    if (sum == ban[a - 1]) {
      // cout<<sum;
      return 1;
    }
    return 0;
  }
  int res = 0;
  if (sum + ban[hukasa] <= 20)
    res += dfs(sum + ban[hukasa], hukasa + 1);
  // return res;
  if (sum - ban[hukasa] >= 0)
    res += dfs(sum - ban[hukasa], hukasa + 1);
  return res;
}
int main() {
  cin >> a;
  for (int i = 0; i < a; i++)
    cin >> ban[i];

  cout << dfs(0, 0) << endl;
  // cout<<x<<endl;
  return 0;
}
Problem p00480 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
ll num[101];
ll memo[101][101];
ll limit;
ll math(ll p, ll sum) {
  if (sum < 0 || sum > 20)
    return 0;
  else if (p == limit - 1)
    return sum == num[p];
  else if (memo[p][sum] > 0)
    return memo[p][sum];
  else
    return memo[p][sum] = math(p + 1, sum + num[p]) + math(p + 1, sum - num[p]);
}

int main() {
  cin >> limit;
  for (int i = 0; i < limit; i++)
    cin >> num[i];
  cout << math(1, num[0]) << endl;
}

Error: TLE

Fixed submission:

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

vector<int> num;
int total, limit;
long long cont = 0;
void math(int pl, int res) {
  int add, sub;
  add = res + num[pl];
  sub = res - num[pl];
  if (add <= 20 && add >= 0) {
    if (pl == limit - 2 && add == total) {
      cont++;
    }
    math(pl + 1, add);
  }
  if (sub <= 20 && sub >= 0) {
    if (pl == limit - 2 && sub == total) {
      cont++;
    }
    math(pl + 1, sub);
  }
}

int main() {
  string equa;
  cin >> limit;
  cin.ignore();
  getline(cin, equa);
  for (int i = 0; i < equa.size(); i++) {
    if (equa[i] != ' ')
      num.push_back(equa[i] - '0');
  }
  total = equa[equa.size() - 1] - '0';
  math(0, 0);
  cout << cont << endl;
}