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

#include <iostream>

int main() {
  int input_num, finger_num, max_data;
  finger_num = 21, max_data = 100;
  std::cin >> input_num;
  int data[max_data];
  long long fingers[max_data][finger_num];

  for (int i = 0; i < input_num - 1; i++) {
    std::cin >> data[i];
    for (int j = 0; j < finger_num; j++) {
      fingers[i][j] = 0;
    }
  }

  // 初期化

  fingers[0][data[0]] = 1;
  for (int i = 1; i < input_num - 1; i++) {
    for (int j = 0; j < finger_num; j++) {
      if (data[i] + j <= 20) {
        fingers[i][data[i] + j] += fingers[i - 1][j];
      }
      if (j - data[i] >= 0) {
        fingers[i][j - data[i]] += fingers[i - 1][j];
      }
    }
  }

  int tmp;
  std::cin >> tmp;
  std::cout << fingers[input_num - 2][tmp] << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <deque>
#include <iostream>
#include <vector>

// 式を'+'か'-'の二分気に見立てた。
class Node {
private:
  int depth;
  int sum;
  std::vector<bool> signs;

public:
  Node(int depth1, int sum1, std::vector<bool> signs1);
  int GetDepth();
  int GetSum();
  std::vector<bool> GetSigns();
  std::vector<Node> make_offsprings(int depth, int sum, std::vector<bool> signs,
                                    std::vector<int> input);
};

void search(std::vector<int> input); // 等式になる数を求める。
int decode_sum(std::vector<int> input,
               std::vector<bool> signs); // 符号と受験を求める。
inline bool
judge_result(std::vector<int> input,
             std::vector<bool> signs); // 統合が成立するかどうかをしらべる。

Node::Node(int depth1, int sum1, std::vector<bool> signs1) {
  this->depth = depth1;
  this->sum = sum1;
  this->signs = signs1;
}

int Node::GetDepth() { return this->depth; }

int Node::GetSum() { return this->sum; }

std::vector<bool> Node::GetSigns() { return this->signs; }

std::vector<Node> Node::make_offsprings(int depth, int sum,
                                        std::vector<bool> signs,
                                        std::vector<int> input) {
  std::vector<Node> offsprings;
  signs.push_back(1);
  Node node1 = Node(depth + 1, sum + input[depth + 1], signs);
  offsprings.push_back(node1);
  signs.pop_back();
  signs.push_back(0);
  Node node2 = Node(depth + 1, sum - input[depth + 1], signs);
  offsprings.push_back(node2);

  return offsprings;
}

void search(std::vector<int> input) {
  unsigned int ans = 0;
  int end_depth = input.size() - 2;
  std::deque<Node> que;
  std::vector<bool> vec;
  std::vector<Node> offsprings;
  Node start = Node(0, input[0], vec);
  que.push_front(start);
  int i = 0;
  while (!que.empty()) {
    Node tmp = que.front();
    que.pop_front();
    if (tmp.GetDepth() == end_depth) {
      if (judge_result(input, tmp.GetSigns())) {
        ans++;
      }
    } else {

      offsprings = tmp.make_offsprings(tmp.GetDepth(), tmp.GetSum(),
                                       tmp.GetSigns(), input);
      for (Node tmp2 : offsprings) {
        if (tmp2.GetSum() >= 0 && tmp2.GetSum() <= 20) {
          que.push_front(tmp2);
        }
      }
    }
  }
  std::cout << ans << std::endl;
}

int decode_sum(std::vector<int> input, std::vector<bool> signs) {
  int sum = input[0];
  int loop = 0;
  for (bool tmp : signs) {
    if (tmp == true) {
      sum += input[loop + 1];
    } else {
      sum -= input[loop + 1];
    }
    ++loop;
  }
  return sum;
}

inline bool judge_result(std::vector<int> input, std::vector<bool> signs) {
  return (decode_sum(input, signs) == input.back());
}

int main() {
  int input_num;
  std::vector<int> input;
  std::cin >> input_num;

  int tmp;
  while (std::cin >> tmp) {
    input.push_back(tmp);
  }

  search(input);

  return 0;
}