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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
int A[20];
int m[200];
int n;
int q;
bool solve(int i, int m) {
  bool res;
  if (m == 0) {
    return true;
  }
  if (i >= n) {
    return false;
  }
  res = solve(i + 1, m) || solve(i + 1, m - A[i]);
  return res;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m[i];
    if (solve(0, m[i]) == true) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
 * main.cpp
 *
 *  Created on: 2016/06/15
 *      Author: acmicpc
 */

#include <iostream>
#include <string>
using namespace std;
int flag[20];
int a[20];
int m[200];
int n;
int w;
string sas(int pos) {
  for (int i = 0; i < flag[n]; i++) {
    int ans = 0;
    for (int j = 0; j < n; j++) {
      if (i & flag[j]) {
        ans += a[j];
      }
    }
    if (ans == m[pos])
      return "yes";
  }
  return "no";
}

int main(void) {
  for (int i = 0, f = 2; i < 21; i++, f <<= 1) {
    flag[i] = f;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> w;
  for (int i = 0; i < w; i++) {
    cin >> m[i];
    cout << sas(i) << endl;
  }
}