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

#include <bits/stdc++.h>
using namespace std;

int a[6], t[6];
char c[] = {'E', 'N', 'S', 'W'};
int r[4][6]{{2, 1, 5, 3, 0, 4},
            {1, 5, 2, 0, 4, 3},
            {3, 0, 2, 5, 4, 1},
            {4, 1, 0, 3, 5, 2}};

bool ch(int *t) {
  int a[3] = {t[0] + t[5], t[1] + t[3], t[2] + t[4]};
  sort(a, a + 3);
  return a[2] <= a[0] + a[1];
}

bool check(int *t) {
  if (ch(t))
    return true;
  for (int i = 1; i <= 4; i++)
    if (t[i] > 0) {
      t[i]--;
      bool f = ch(t);
      t[i]++;
      if (f)
        return true;
    }
  return false;
}

int main() {
  ios ::sync_with_stdio(false);
  while (cin >> a[0]) {
    for (int i = 1; i < 6; i++)
      cin >> a[i];
    if (!a[5])
      break;
    string ans = "";
    int p[6] = {0, 1, 2, 3, 4, 5};
    do {
      /*
      if(p[1] > p[3] || p[2] > p[4])
              continue;
              */
      for (int i = 0; i < 6; i++)
        t[i] = a[p[i]];
      if (!check(t))
        continue;
      string tmp = "";
      int sum = accumulate(t, t + 6, 0);
      while (sum--) {
        for (int k = 0; k < 4; k++) {
          int s[6];
          for (int i = 0; i < 6; i++)
            s[i] = t[r[k][i]];
          s[0]--;
          if (s[0] >= 0 && check(s)) {
            tmp += c[k];
            for (int i = 0; i < 6; i++)
              t[i] = s[i];
            break;
          }
        }
      }
      if (ans == "" || tmp < ans)
        ans = tmp;
    } while (next_permutation(p, p + 6));

    {
      int p, q;
      cin >> p >> q;
      if (ans == "")
        cout << "impossible\\n";
      else {
        for (int i = p - 1; i < q; i++)
          cout << ans[i];
        cout << '\\n';
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int a[6], t[6];
char c[] = {'E', 'N', 'S', 'W'};
int r[4][6]{{2, 1, 5, 3, 0, 4},
            {1, 5, 2, 0, 4, 3},
            {3, 0, 2, 5, 4, 1},
            {4, 1, 0, 3, 5, 2}};

bool ch(int *t) {
  int a[3] = {t[0] + t[5], t[1] + t[3], t[2] + t[4]};
  sort(a, a + 3);
  return a[2] <= a[0] + a[1];
}

bool check(int *t) {
  bool f = ch(t);
  for (int i = 1; i <= 4; i++)
    if (t[i] > 0) {
      t[i]--;
      f |= ch(t);
      t[i]++;
    }
  return f;
}

int main() {
  ios ::sync_with_stdio(false);
  while (cin >> a[0]) {
    for (int i = 1; i < 6; i++)
      cin >> a[i];
    if (!a[5])
      break;
    string ans = "";
    int p[6] = {0, 1, 2, 3, 4, 5};
    do {
      for (int i = 0; i < 6; i++)
        t[i] = a[p[i]];
      if (!check(t))
        continue;
      string tmp = "";
      while (accumulate(t, t + 6, 0)) {
        for (int k = 0; k < 4; k++) {
          int s[6];
          for (int i = 0; i < 6; i++)
            s[i] = t[r[k][i]];
          s[0]--;
          if (s[0] >= 0 && check(s)) {
            tmp += c[k];
            for (int i = 0; i < 6; i++)
              t[i] = s[i];
            break;
          }
        }
      }
      if (ans == "" || tmp < ans)
        ans = tmp;
    } while (next_permutation(p, p + 6));

    {
      int p, q;
      cin >> p >> q;
      if (ans == "")
        cout << "impossible\\n";
      else {
        for (int i = p - 1; i < q; i++)
          cout << ans[i];
        cout << '\\n';
      }
    }
  }
  return 0;
}