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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
ld eps = 1e-9;

bool check(vector<int> &v) {
  if (v[0] < 0)
    return false;

  int a = v[0] + v[5];
  int b = v[1] + v[4];
  int c = v[2] + v[3];

  if (a + c + 1 < b)
    return false;
  if (a + b + 1 < c)
    return false;
  if (b + c < a)
    return false;

  return true;
}
int sum;
bool flag = false;
int max_e_num = 0;
int now_e_num;
void solve(string &ans, vector<int> &v) {
  if (ans.size() == sum)
    return;
  string way = "ENSW";

  {
    vector<int> nv(v);
    ;
    nv[0] = v[1];
    nv[1] = v[5];
    nv[5] = v[4];
    nv[4] = v[0];
    nv[0]--;
    if (check(nv)) {
      ans += way[0];
      now_e_num += 1;
      solve(ans, nv);
      return;
    }
  }
  if (!flag && max_e_num < now_e_num) {
    max_e_num = now_e_num;
  } else if (!flag && max_e_num > now_e_num) {
    ans.clear();
    return;
  }
  flag = true;
  {
    vector<int> nv(v);
    ;
    nv[0] = v[2];
    nv[2] = v[5];
    nv[5] = v[3];
    nv[3] = v[0];
    nv[0]--;
    if (check(nv)) {
      ans += way[1];
      solve(ans, nv);
      return;
    }
  }
  {
    vector<int> nv(v);
    ;
    nv[0] = v[3];
    nv[3] = v[5];
    nv[5] = v[2];
    nv[2] = v[0];
    nv[0]--;
    if (check(nv)) {
      ans += way[2];
      solve(ans, nv);
      return;
    }
  }
  {
    vector<int> nv(v);
    ;
    nv[0] = v[4];
    nv[4] = v[5];
    nv[5] = v[1];
    nv[1] = v[0];
    nv[0]--;
    if (check(nv)) {
      ans += way[3];
      solve(ans, nv);
      return;
    }
  }
}

int main() {
  vector<int> v(6);
  while (cin >> v[0] >> v[1] >> v[2] >> v[3] >> v[4] >> v[5]) {
    sum = accumulate(v.begin(), v.end(), 0);
    if (sum == 0)
      break;
    else {
      max_e_num = 0;
      sort(v.begin(), v.end());
      string answer(sum, 'Z');
      do {
        flag = false;
        now_e_num = 0;
        string ans;
        solve(ans, v);
        if (!ans.empty()) {
          answer = min(answer, ans);
        }
      } while (next_permutation(v.begin(), v.end()));

      int L, R;
      cin >> L >> R;
      if (answer[0] == 'Z') {
        cout << "impossible" << endl;
      } else {

        cout << answer.substr(L - 1, R - L + 1) << endl;
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
ld eps = 1e-9;

bool check(vector<int> &v) {
  if (v[0] < 0)
    return false;

  int a = v[0] + v[5];
  int b = v[1] + v[4];
  int c = v[2] + v[3];

  if (a + c + 1 < b)
    return false;
  if (a + b + 1 < c)
    return false;
  if (b + c < a)
    return false;

  return true;
}
int sum;
void solve(string &ans, vector<int> &v) {
  if (ans.size() == sum)
    return;
  string way = "ENSW";

  {
    vector<int> nv(v);
    ;
    nv[0] = v[1];
    nv[1] = v[5];
    nv[5] = v[4];
    nv[4] = v[0];
    nv[0]--;
    if (check(nv)) {
      int a = nv[0] + nv[5];
      int b = nv[1] + nv[4];
      int c = nv[2] + nv[3];

      int rest = c - a - b - 100;
      rest = max(0, rest);
      nv[0] -= rest / 4;
      nv[1] -= rest / 4;
      nv[2] -= rest / 4;
      nv[3] -= rest / 4;

      for (int i = 0; i < rest + 1; ++i) {

        ans += way[0];
      }
      solve(ans, nv);
      return;
    }
  }
  {
    vector<int> nv(v);
    ;
    nv[0] = v[2];
    nv[2] = v[5];
    nv[5] = v[3];
    nv[3] = v[0];
    nv[0]--;
    if (check(nv)) {
      ans += way[1];
      solve(ans, nv);
      return;
    }
  }
  {
    vector<int> nv(v);
    ;
    nv[0] = v[3];
    nv[3] = v[5];
    nv[5] = v[2];
    nv[2] = v[0];
    nv[0]--;
    if (check(nv)) {
      ans += way[2];
      solve(ans, nv);
      return;
    }
  }
  {
    vector<int> nv(v);
    ;
    nv[0] = v[4];
    nv[4] = v[5];
    nv[5] = v[1];
    nv[1] = v[0];
    nv[0]--;
    if (check(nv)) {
      ans += way[3];
      solve(ans, nv);
      return;
    }
  }
}

int main() {
  vector<int> v(6);
  while (cin >> v[0] >> v[1] >> v[2] >> v[3] >> v[4] >> v[5]) {
    if (accumulate(v.begin(), v.end(), 0) == 0)
      break;
    else {
      sum = accumulate(v.begin(), v.end(), 0);
      sort(v.begin(), v.end());
      string answer(sum, 'Z');
      do {
        string ans;
        solve(ans, v);
        if (!ans.empty()) {
          answer = min(answer, ans);
        }
      } while (next_permutation(v.begin(), v.end()));

      int L, R;
      cin >> L >> R;
      if (answer[0] == 'Z') {
        cout << "impossible" << endl;
      } else {

        cout << answer.substr(L - 1, R - L + 1) << endl;
      }
    }
  }
  return 0;
}