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

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
// #define TEST
// //*******************************************************************************************************************************************

//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << (A) << " "
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)

#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2529878#1

vector<int> a;

bool canMakeNumber(int startIndex, int key) {

  //    dout << "canMakeNumber?(" << startIndex << ", " << key << ")\\n";

  if (a[startIndex] == key) {
    //        dout << "OK\\n";
    return true;
  } else if (startIndex == a.size() - 1) {
    //        dout << "NG\\n";
    return false;
  } else if (key - a[startIndex] < 0) {
    return canMakeNumber(startIndex + 1, key);
  } else {
    return canMakeNumber(startIndex + 1, key) ||
           canMakeNumber(startIndex + 1, key - a[startIndex]);
  }
}

int main(void) {

  int N, numOfQuery;
  cin >> N;

  int x;
  rep(i, 0, N) {
    cin >> x;
    a.push_back(x);
  }

  //    dispAll(a,N);
  //    sort(a, a+N);
  //    dispAll(a,N);

  cin >> numOfQuery;

  int q[numOfQuery];
  rep(i, 0, numOfQuery) cin >> q[i];

  //    dispAll(q,numOfQuery);

  //    numOfQuery = 1;
  rep(i, 0, numOfQuery) {

    //        dout << "========\\n";
    //        disp(key);

    cout << (canMakeNumber(0, q[i]) ? "yes" : "no") << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

////////////////////////////////////////
// ALDS1_5_A:   Exhaustive Search
//
//?????°??¢??°
// TLE
// http://abc012.contest.atcoder.jp/submissions/1587158
////////////////////////////////////////

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
// #define TEST
// //*******************************************************************************************************************************************

//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << (A) << " "
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)

#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2529878#1

vector<int> a;

bool canMakeNumber(int startIndex, int key) {

  dout << "canMakeNumber?(" << startIndex << ", " << key << ")\\n";

  if (a[startIndex] == key) {
    dout << "OK\\n";
    return true;
  } else if (startIndex == a.size() - 1) {
    dout << "NG\\n";
    return false;
  } else if (key - a[startIndex] < 0) {
    return canMakeNumber(startIndex + 1, key);
  } else {
    return canMakeNumber(startIndex + 1, key) ||
           canMakeNumber(startIndex + 1, key - a[startIndex]);
  }
}

int main(void) {

  int N, numOfQuery;
  cin >> N;

  int x;
  rep(i, 0, N) {
    cin >> x;
    a.push_back(x);
  }

  dispAll(a, N);
  //    sort(a, a+N);
  //    dispAll(a,N);

  cin >> numOfQuery;

  int q[numOfQuery];
  rep(i, 0, numOfQuery) cin >> q[i];

  dispAll(q, numOfQuery);

  int key;
  bool ans;

  //    numOfQuery = 1;
  rep(i, 0, numOfQuery) {
    ans = false;
    key = q[i];

    dout << "========\\n";
    disp(key);

    ans = canMakeNumber(0, key);

    cout << (ans ? "yes" : "no") << endl;
  }

  return 0;
}