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

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
long long int ans(int p, int q);
int n;
vector<int> lot;
int main() {
  cin >> n;
  for (int i = 0, a; i < n; i++) {
    cin >> a;
    lot.push_back(a);
  }
  cout << ans(1, lot[0]) << endl;
  return 0;
}
long long int ans(int p, int q) {
  long long int x = 0;
  if (p == n - 1 && q == lot[p])
    return 1;
  if (p == n - 1)
    return 0;
  if (q < 0 || q > 20)
    return 0;
  if (lot[p] == 0)
    return ans(p + 1, q) * 2;
  if (p < n - 3 && lot[p] + lot[p + 1] == lot[p + 2] && lot[p + 1] != 0 &&
      lot[p + 2] != 0) {
    int u = 0;
    if (q + lot[p] <= 20 && q + lot[p] + lot[p + 1] <= 20 &&
        q + lot[p] + lot[p + 1] + lot[p + 2] <= 20)
      x += ans(p + 3, q + lot[p] + lot[p + 1] + lot[p + 2]);
    if (q + lot[p] <= 20 && q + lot[p] + lot[p + 1] <= 20 &&
        q + lot[p] + lot[p + 1] - lot[p + 2] >= 0)
      u++;
    if (q + lot[p] <= 20 && q + lot[p] - lot[p + 1] >= 0 &&
        q + lot[p] - lot[p + 1] + lot[p + 2] <= 20)
      x += ans(p + 3, q + lot[p] - lot[p + 1] + lot[p + 2]);
    if (q + lot[p] <= 20 && q + lot[p] - lot[p + 1] >= 0 &&
        q + lot[p] - lot[p + 1] - lot[p + 2] >= 0)
      x += ans(p + 3, q + lot[p] - lot[p + 1] - lot[p + 2]);
    if (q - lot[p] >= 0 && q - lot[p] + lot[p + 1] <= 20 &&
        q - lot[p] + lot[p + 1] + lot[p + 2] <= 20)
      x += ans(p + 3, q - lot[p] + lot[p + 1] + lot[p + 2]);
    if (q - lot[p] >= 0 && q - lot[p] + lot[p + 1] <= 20 &&
        q - lot[p] + lot[p + 1] - lot[p + 2] >= 0)
      x += ans(p + 3, q - lot[p] + lot[p + 1] - lot[p + 2]);
    if (q - lot[p] >= 0 && q - lot[p] - lot[p + 1] >= 0 &&
        q - lot[p] - lot[p + 1] + lot[p + 2] <= 20)
      u++;
    if (q - lot[p] >= 0 && q - lot[p] - lot[p + 1] >= 0 &&
        q - lot[p] - lot[p + 1] - lot[p + 2] >= 0)
      x += ans(p + 3, q - lot[p] - lot[p + 1] - lot[p + 2]);
    if (u > 0)
      x += u * ans(p + 3, q);
  } else if (p < n - 3 && lot[p] == lot[p + 1] && lot[p + 1] == lot[p + 2]) {
    int u = 0;
    if (q + lot[p] + lot[p] <= 20)
      u++;
    if (q + lot[p] <= 20)
      u++;
    if (q - lot[p] >= 0)
      u++;
    if (u > 0)
      x += u * ans(p + 3, q + lot[p]);
    u = 0;
    if (q - lot[p] - lot[p] >= 0)
      u++;
    if (q - lot[p] >= 0)
      u++;
    if (q + lot[p] <= 20)
      u++;
    if (u > 0)
      x += u * ans(p + 3, q - lot[p]);
    if (q + lot[p] * 3 <= 20)
      x += ans(p + 3, q + 3 * lot[p]);
    if (q - lot[p] * 3 >= 0)
      x += ans(p + 3, q - 3 * lot[p]);
  } else if (p < n - 2 && lot[p] == lot[p + 1]) {
    if (q + lot[p] <= 20 && q - lot[p] >= 0)
      x += ans(p + 2, q) * 2;
    else
      x += ans(p + 2, q);
    if (q + lot[p] + lot[p + 1] <= 20)
      x += ans(p + 2, q + lot[p] + lot[p + 1]);
    if (q - lot[p] - lot[p + 1] >= 0)
      x += ans(p + 2, q - lot[p] - lot[p + 1]);
  } else {
    if (q + lot[p] <= 20)
      x += ans(p + 1, q + lot[p]);
    if (q - lot[p] >= 0)
      x += ans(p + 1, q - lot[p]);
  }
  return x;
}