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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

void print(vector<int> array) {
  for (int i = 0; i < array.size() - 1; i++) {
    cout << array[i] << " ";
  }
  cout << array[array.size() - 1] << endl;
}
vector<int> Exaustive(vector<int> &array) {
  // print(array);
  int n = array.size();
  if (n == 1) {
    return array;
  } else if (n == 2) {
    int app = array[0] + array[1];
    array.push_back(app);
    // cout<<"app;"<<endl;
    // print(array);
    return array;
  } else {
    int half = n / 2;
    vector<int> left(0);
    vector<int> right(0);
    for (int i = 0; i < half; i++) {
      left.push_back(array[i]);
    }
    for (int i = half; i < n; i++) {
      right.push_back(array[i]);
    }
    left = Exaustive(left);
    right = Exaustive(right);

    vector<int> app(0);
    for (int i = 0; i < right.size(); i++) {
      for (int j = 0; j < left.size(); j++) {
        int x = right[i] + left[j];
        app.push_back(x);
      }
    }
    for (int i = 0; i < right.size(); i++) {
      app.push_back(right[i]);
    }
    for (int i = 0; i < left.size(); i++) {
      app.push_back(left[i]);
    }
    // cout<<"app;"<<endl;
    // print(app);
    return app;
  }
}
int main() {
  int n, m, judge;
  cin >> n;
  vector<int> list(n);
  for (int i = 0; i < n; i++) {
    cin >> list[i];
  }
  cin >> m;
  vector<int> ms(m);
  for (int i = 0; i < m; i++) {
    cin >> ms[i];
  }

  list = Exaustive(list);

  for (int j = 0; j < ms.size(); j++) {
    judge = 0;
    for (int i = 0; i < list.size(); i++) {
      if (ms[j] == list[i]) {
        cout << "yes" << endl;
        judge = 1;
        break;
      }
    }
    if (judge == 0)
      cout << "no" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// this Print func would cause Time exceed.
/*
void print(vector<int> array)
{
  for(int x:array){
    cout<<x<<" ";
  }
  cout<<endl;
}
*/
void print(vector<int> array) {
  for (int i = 0; i < array.size() - 1; i++) {
    cout << array[i] << " ";
  }
  cout << array[array.size() - 1] << endl;
}
vector<int> Exaustive(vector<int> &array) {
  int n = array.size();
  if (n == 1) {
    return array;
  } else if (n == 2) {
    int app = array[0] + array[1];
    array.push_back(app);
    return array;
  } else {
    int half = n / 2;
    vector<int> left(0);
    vector<int> right(0);
    for (int i = 0; i < half; i++) {
      left.push_back(array[i]);
    }
    for (int i = 0; i <= half; i++) {
      right.push_back(array[i + half]);
    }
    left = Exaustive(left);
    right = Exaustive(right);
    vector<int> app(0);
    for (int i = 0; i < right.size(); i++) {
      for (int j = 0; j < left.size(); j++) {
        int x = right[i] + left[j];
        app.push_back(x);
      }
    }
    app.insert(app.end(), right.begin(), right.end());
    app.insert(app.end(), left.begin(), left.end());
    return app;
  }
}
int main() {
  int n, m, judge;
  cin >> n;
  vector<int> list(n);
  for (int i = 0; i < n; i++) {
    cin >> list[i];
  }
  cin >> m;
  vector<int> ms(m);
  for (int i = 0; i < m; i++) {
    cin >> ms[i];
  }

  list = Exaustive(list);

  for (int x : ms) {
    judge = 0;
    for (int i = 0; i < list.size(); i++) {
      if (x == list[i]) {
        cout << "yes" << endl;
        judge = 1;
        break;
      }
    }
    if (judge == 0)
      cout << "no" << endl;
  }
  return 0;
}