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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

bool FLG;

int N, calc_N, value, PLUS, MINUS;
char table[100][101];
int minimum[100], add[100], rev_minimum[100], rev_add[100];

void recursive_PLUS(vector<int> V, int used_num, int diff) {

  if (FLG)
    return;

  if (used_num == calc_N) {
    FLG = true;
    PLUS = diff;
    return;
  }

  for (int i = 0; i < V.size(); i++) {

    if (diff + minimum[V[i]] >= 0) {

      vector<int> next_V;
      for (int k = 0; k < V.size(); k++) {
        if (k != i)
          next_V.push_back(V[k]);
      }
      recursive_PLUS(next_V, used_num + 1, diff + add[V[i]]);
    }
  }
}

void recursive_MINUS(vector<int> V, int used_num, int diff) {

  if (FLG)
    return;

  if (used_num == calc_N) {
    FLG = true;
    MINUS = diff;
    return;
  }

  for (int i = 0; i < V.size(); i++) {

    if (diff + rev_minimum[V[i]] >= 0) {

      vector<int> next_V;
      for (int k = 0; k < V.size(); k++) {
        if (k != i)
          next_V.push_back(V[k]);
      }
      recursive_MINUS(next_V, used_num + 1, diff + rev_add[V[i]]);
    }
  }
}

int main() {

  FLG = false;

  scanf("%d", &N);

  int open_num = 0, close_num = 0;

  for (int i = 0; i < N; i++) {
    scanf("%s", table[i]);
    for (int k = 0; table[i][k] != '\\0'; k++) {
      if (table[i][k] == '(')
        open_num++;
      else {
        close_num++;
      }
    }
  }

  if (open_num != close_num) {
    printf("No\\n");
    return 0;
  }

  for (int k = 0; k < N; k++) {
    minimum[k] = 0;
    rev_minimum[k] = 0;
  }

  int tmp, length;

  for (int i = 0; i < N; i++) {
    tmp = 0;
    length = 0;
    for (int k = 0; table[i][k] != '\\0'; k++) {
      length++;
      if (table[i][k] == '(')
        tmp++;
      else {
        tmp--;
        minimum[i] = min(minimum[i], tmp);
      }
    }
    add[i] = tmp;
    if (add[i] < 0) {
      tmp = 0;
      for (int k = length - 1; k >= 0; k--) {
        if (table[i][k] == ')')
          tmp++;
        else {
          tmp--;
          rev_minimum[i] = min(rev_minimum[i], tmp);
        }
      }
      rev_add[i] = tmp;
    }
  }

  vector<int> V;
  tmp = N;
  calc_N = N;
  PLUS = 0;
  for (int i = 0; i < tmp; i++) {
    if (minimum[i] == 0 && add[i] == 0) {
      calc_N--;
    } else if (add[i] < 0) {
      calc_N--;
    } else if (minimum[i] == 0) {
      PLUS += add[i];
      calc_N--;
    } else {
      V.push_back(i);
    }
  }

  if (calc_N > 0) {
    recursive_PLUS(V, 0, PLUS);
  } else {
    FLG = true;
  }

  if (!FLG) {
    printf("No\\n");
    return 0;
  }

  FLG = false;

  V.clear();
  tmp = N;
  calc_N = N;
  MINUS = 0;
  for (int i = 0; i < tmp; i++) {
    if (minimum[i] == 0 && add[i] == 0) {
      calc_N--;
    } else if (add[i] >= 0) {
      calc_N--;
    } else if (rev_minimum[i] == 0) {
      MINUS += rev_add[i];
      calc_N--;
    } else {
      V.push_back(i);
    }
  }
  if (MINUS < 0) {
    printf("No\\n");
    return 0;
  }

  recursive_MINUS(V, 0, MINUS);

  if (FLG == true && PLUS == MINUS) {
    printf("Yes\\n");
  } else {
    printf("No\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

bool FLG;

int N, calc_N, value, PLUS;
char table[100][101];
int minimum[100], add[100];

void recursive_PLUS(vector<int> V, int used_num, int diff) {

  if (FLG)
    return;

  if (used_num == calc_N) {
    FLG = true;
    PLUS += diff;
    return;
  }

  for (int i = 0; i < V.size(); i++) {

    if (diff + minimum[V[i]] >= 0) {

      vector<int> next_V;
      for (int k = 0; k < V.size(); k++) {
        if (k != i)
          next_V.push_back(V[k]);
      }
      recursive_PLUS(next_V, used_num + 1, diff + add[V[i]]);
    }
  }
}

void recursive_MINUS(vector<int> V, int used_num, int diff) {

  if (FLG)
    return;

  if (used_num == calc_N) {
    if (diff == 0) {
      FLG = true;
    }
    return;
  }

  for (int i = 0; i < V.size(); i++) {

    if (diff + minimum[V[i]] >= 0) {

      vector<int> next_V;
      for (int k = 0; k < V.size(); k++) {
        if (k != i)
          next_V.push_back(V[k]);
      }
      recursive_MINUS(next_V, used_num + 1, diff + add[V[i]]);
    }
  }
}

int main() {

  FLG = false;

  scanf("%d", &N);

  int open_num = 0, close_num = 0;

  for (int i = 0; i < N; i++) {
    scanf("%s", table[i]);
    for (int k = 0; table[i][k] != '\\0'; k++) {
      if (table[i][k] == '(')
        open_num++;
      else {
        close_num++;
      }
    }
  }

  if (open_num != close_num) {
    printf("No\\n");
    return 0;
  }

  for (int k = 0; k < N; k++) {
    minimum[k] = 0;
  }

  int tmp;

  for (int i = 0; i < N; i++) {
    tmp = 0;
    for (int k = 0; table[i][k] != '\\0'; k++) {
      if (table[i][k] == '(')
        tmp++;
      else {
        tmp--;
        minimum[i] = min(minimum[i], tmp);
      }
    }
    add[i] = tmp;
  }

  vector<int> V;
  tmp = N;
  calc_N = N;
  PLUS = 0;
  for (int i = 0; i < tmp; i++) {
    if (minimum[i] == 0 && add[i] == 0) {
      calc_N--;
    } else if (add[i] < 0) {
      calc_N--;
    } else if (minimum[i] == 0) {
      PLUS += add[i];
      calc_N--;
    } else {
      V.push_back(i);
    }
  }

  if (calc_N > 0) {
    recursive_PLUS(V, 0, PLUS);
  } else {
    FLG = true;
  }

  if (!FLG) {
    printf("No\\n");
    return 0;
  }

  FLG = false;

  V.clear();
  tmp = N;
  calc_N = N;
  for (int i = 0; i < tmp; i++) {
    if (minimum[i] == 0 && add[i] == 0) {
      calc_N--;
    } else if (add[i] > 0) {
      calc_N--;
    } else {
      V.push_back(i);
    }
  }
  if (PLUS < 0) {
    printf("No\\n");
    return 0;
  }

  recursive_MINUS(V, 0, PLUS);

  if (FLG) {
    printf("Yes\\n");
  } else {
    printf("No\\n");
  }

  return 0;
}