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

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef pair<int, int> P;
string dir = "ENSW";
typedef array<int, 6> dice;
dice rotate(dice d, int di) {
  dice ret;
  if (di == 0) {
    ret[0] = d[0];
    ret[1] = d[3];
    ret[2] = d[1];
    ret[3] = d[4];
    ret[4] = d[2];
    ret[5] = d[5];
  }
  if (di == 1) {
    ret[0] = d[4];
    ret[1] = d[0];
    ret[2] = d[2];
    ret[3] = d[3];
    ret[4] = d[5];
    ret[5] = d[1];
  }
  if (di == 2) {
    ret[0] = d[1];
    ret[1] = d[5];
    ret[2] = d[2];
    ret[3] = d[3];
    ret[4] = d[0];
    ret[5] = d[4];
  }
  if (di == 3) {
    ret[0] = d[0];
    ret[1] = d[2];
    ret[2] = d[4];
    ret[3] = d[1];
    ret[4] = d[3];
    ret[5] = d[5];
  }
  return ret;
}
bool can(dice d) {
  int a = d[0] + d[5], b = d[1] + d[4], c = d[2] + d[3];
  rep(i, 6) if (d[i] < 0) return false;
  return (a <= b + c + 1 && b <= a + c && c <= a + b + 1);
}
int main() {
  while (true) {
    dice a;
    int N = 0;
    rep(i, 6) cin >> a[i];
    rep(i, 6) N += a[i];
    if (N == 0)
      break;
    int p, q;
    cin >> p >> q;
    sort(all(a));
    string ans = "Z";
    int cnt = 0;
    do {
      //			if(cnt%10==0) show(cnt);
      cnt++;
      dice d = a;
      if (!can(d))
        continue;
      string now;
      rep(i, N) {
        rep(di, 4) {
          dice c = rotate(d, di);
          c[4]--;
          if (can(c)) {
            now += dir[di];
            d = c;
            break;
          }
        }
      }
      if (ans > now)
        ans = now;
    } while (next_permutation(all(a)));
    if (ans[0] == 'Z')
      puts("impossible");
    else
      cout << ans.substr(p - 1, q - p + 1) << endl;
    // show(ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef pair<int, int> P;
string dir = "ENSW";
typedef vector<int> dice;
dice rotate(dice d, int di) {
  dice ret(6, 0);
  if (di == 0) {
    ret[0] = d[0];
    ret[1] = d[3];
    ret[2] = d[1];
    ret[3] = d[4];
    ret[4] = d[2];
    ret[5] = d[5];
  }
  if (di == 1) {
    ret[0] = d[4];
    ret[1] = d[0];
    ret[2] = d[2];
    ret[3] = d[3];
    ret[4] = d[5];
    ret[5] = d[1];
  }
  if (di == 2) {
    ret[0] = d[1];
    ret[1] = d[5];
    ret[2] = d[2];
    ret[3] = d[3];
    ret[4] = d[0];
    ret[5] = d[4];
  }
  if (di == 3) {
    ret[0] = d[0];
    ret[1] = d[2];
    ret[2] = d[4];
    ret[3] = d[1];
    ret[4] = d[3];
    ret[5] = d[5];
  }
  return ret;
}
bool can(dice d) {
  int a = d[0] + d[5], b = d[1] + d[4], c = d[2] + d[3];
  rep(i, 6) if (d[i] < 0) return false;
  return (a <= b + c + 1 && b <= a + c && c <= a + b + 1);
}
int main() {
  while (true) {
    dice a;
    int N = 0;
    rep(i, 6) {
      int p;
      cin >> p;
      N += p;
      a.pb(p);
    }
    if (N == 0)
      break;
    int p, q;
    cin >> p >> q;
    sort(all(a));
    string ans = "Z";
    do {
      dice d = a;
      if (!can(d))
        continue;
      string now;
      rep(i, N) {
        //				rep(i,6) cout<<d[i]<<" ";
        //				cout<<endl;
        rep(di, 4) {
          dice c = rotate(d, di);
          c[4]--;
          if (can(c)) {
            now += dir[di];
            d = c;
            break;
          }
        }
      }
      if (ans > now)
        ans = now;
    } while (next_permutation(all(a)));
    if (ans[0] == 'Z')
      puts("impossible");
    else
      cout << ans.substr(p - 1, q - p + 1) << endl;
    // show(ans);
  }
}