Problem p00778 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int ord[] = {0, 1, 3, 2};

string dir = "ENWS";
string rdir = "WSEN";

int roll[4][6] = {{4, 1, 0, 3, 5, 2},
                  {3, 0, 2, 5, 4, 1},
                  {2, 1, 5, 3, 0, 4},
                  {1, 5, 2, 0, 4, 3}};

bool check(const vector<int> &dice) {
  if (dice[0] < 0)
    return false;
  int v1 = dice[0] + dice[5], v2 = dice[1] + dice[3], v3 = dice[2] + dice[4];
  if (v1 >= v2 + v3 + 1)
    return false;
  if (v2 > v1 + v3 + 1)
    return false;
  if (v3 > v1 + v2 + 1)
    return false;
  return true;
}

vector<int> get_roll(const vector<int> &dice, int d) {
  vector<int> res(6);
  for (int i = 0; i < 6; i++)
    res[roll[d][i]] = dice[i];
  return res;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  vector<int> t(6);
  while (true) {
    for (int i = 0; i < 6; i++) {
      cin >> t[i];
    }
    if (accumulate(t.begin(), t.end(), 0) == 0)
      break;
    int p, q;
    cin >> p >> q;
    --p;
    sort(t.begin(), t.end());
    string res = "Z";
    do {
      if (!check(t))
        continue;
      auto dice = t;
      string s;
      while (*max_element(dice.begin(), dice.end()) > 0) {
        for (int i = 0; i < 4; i++) {
          char c = dir[ord[i]];
          dice = get_roll(dice, ord[i]);
          dice[0]--;
          if (check(dice)) {
            s.push_back(c);
            break;
          }
          dice[0]++;
          dice = get_roll(dice, (ord[i] + 2) % 4);
        }
      }
      res = min(res, s);
    } while (next_permutation(t.begin(), t.end()));
    if (res == "Z") {
      cout << "impossible" << endl;
    } else {
      cout << res.substr(p, q - p) << endl;
    }
  }
  return 0;
}


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

int ord[] = {0, 1, 3, 2};

string dir = "ENWS";
string rdir = "WSEN";

int roll[4][6] = {{4, 1, 0, 3, 5, 2},
                  {3, 0, 2, 5, 4, 1},
                  {2, 1, 5, 3, 0, 4},
                  {1, 5, 2, 0, 4, 3}};

bool check(const vector<int> &dice) {
  if (dice[0] < 0)
    return false;
  int v1 = dice[0] + dice[5], v2 = dice[1] + dice[3], v3 = dice[2] + dice[4];
  if (v1 >= v2 + v3 + 1)
    return false;
  if (v2 > v1 + v3 + 1)
    return false;
  if (v3 > v1 + v2 + 1)
    return false;
  return true;
}

vector<int> get_roll(const vector<int> &dice, int d) {
  vector<int> res(6);
  for (int i = 0; i < 6; i++)
    res[roll[d][i]] = dice[i];
  return res;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  vector<int> t(6);
  while (true) {
    for (int i = 0; i < 6; i++) {
      cin >> t[i];
    }
    if (accumulate(t.begin(), t.end(), 0) == 0)
      break;
    int p, q;
    cin >> p >> q;
    --p;
    sort(t.begin(), t.end());
    string res = "Z";
    do {
      if (!check(t))
        continue;
      auto dice = t;
      string s;
      while (*max_element(dice.begin(), dice.end()) > 0) {
        for (int i = 0; i < 4; i++) {
          char c = dir[ord[i]];
          dice = get_roll(dice, ord[i]);
          dice[0]--;
          if (check(dice)) {
            s.push_back(c);
            break;
          }
          dice[0]++;
          dice = get_roll(dice, (ord[i] + 2) % 4);
        }
        if (s > res)
          break;
      }
      res = min(res, s);
    } while (next_permutation(t.begin(), t.end()));
    if (res == "Z") {
      cout << "impossible" << endl;
    } else {
      cout << res.substr(p, q - p) << endl;
    }
  }
  return 0;
}