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

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

// define-----------------------------------------------------------------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, s, n) for (int i = s; i < (int)n; i++)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n)                                                             \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\\n";
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << "\\n";
#define sq(n) (n) * (n)

// typedef----------------------------------------------------------------------------------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
// const------------------------------------------------------------------------------------------------------------------------
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
// global-----------------------------------------------------------------------------------------------------------------------
int n, s;
VI a;
LL dp[100][21];

LL solve(int now, int f) {
  if (dp[now][f] >= 0) {
    return dp[now][f];
  }
  ull res = 0;
  if (now == n - 1) {
    res = s == f;
  } else {
    if (f + a[now] <= 20) {
      res += solve(now + 1, f + a[now]);
    }
    if (f - a[now] >= 0) {
      res += solve(now + 1, f - a[now]);
    }
  }
  return dp[now][f] = res;
}
int main() {
  scanf("%d", &n);
  a.resize(n - 1);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d", &a[i]);
  }
  scanf("%d", &s);
  memset(dp, -1, sizeof(dp));
  printf("%lld\\n", solve(1, a[0]));
  return 0;
}

Error: TLE

Fixed submission:

// まともに使い方を理解していないくせにincludeしまくる人間のクズ
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

// using namespace stdしないと表記がめんどくさいからね、仕方ないね
using namespace std;

// 可読性を上げるためのdefineが仇となる典型的パターン
#define rep(i, x) for (int i = 0; i < x; i++)

// ローカル変数とグローバル変数は使い分けよう！
stack<short> x, y;
short h, m, a[100], n, i;
long long int ans;
// 関数って楽しいよね！（魔王スマイル）

void solve(short i, short sum) {
  if (i == n - 1) {
    if (sum == a[n - 1])
      ans++;
    return;
  }
  if (sum < 0 || sum > 20)
    return;
  solve(i + 1, sum + a[i]);
  solve(i + 1, sum - a[i]);
}

int main() {
  scanf("%d", &n);
  ans = 0;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  solve(0, 0);
  printf("%lld\\n", ans);
  return 0;
}