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

#include <bits/stdc++.h>

using namespace std;

int N;

set<vector<long long int>> ans;
void saiki(vector<long long int> town, vector<long long int> nowInput,
           long long int maxlen);
void saiki2(vector<long long int> nextTown, vector<long long int> nextInput,
            long long int maxlen, long long int townPoint) {
  bool f = true;
  if (nextInput[townPoint] <= 0)
    return;
  for (auto x : nextTown) {
    if (nextInput[abs(x - townPoint)] > 0) {
      nextInput[abs(x - townPoint)]--;
    } else {
      f = false;
      break;
    }
  }
  nextTown.push_back(townPoint);
  if (f) {
    saiki(nextTown, nextInput, maxlen);
  }
}

void saiki(vector<long long int> town, vector<long long int> nowInput,
           long long int maxlen) {
  while (maxlen > 0 && !nowInput[maxlen])
    maxlen--;
  if (maxlen == 0) {
    if (town.size() == N) {
      sort(town.begin(), town.end());
      ans.insert(town);
    }
    return;
  }
  long long int townPoint = maxlen;
  saiki2(town, nowInput, maxlen, townPoint);
  townPoint = town[1] - maxlen;
  saiki2(town, nowInput, maxlen, townPoint);
}

int main() {
  while (cin >> N, N) {
    ans = set<vector<long long int>>();
    vector<long long int> town(1), input(401);
    for (int i = 0; i < N * (N - 1) / 2; i++) {
      long long int x;
      cin >> x;
      if (i == 0) {
        town.push_back(x);
      } else {
        input[x]++;
      }
    }
    saiki(town, input, 400);
    for (auto now : ans) {
      for (int i = 0; i < N - 1; i++) {
        cout << now[i + 1] - now[i];
        if (i == N - 2) {
          cout << endl;
        } else {
          cout << " ";
        }
      }
    }
    cout << "-----" << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N;
void saiki(vector<long long int> nowVec,
           map<long long int, long long int> nowInput, long long int sum) {
  if (nowVec.size() == N - 1) {
    int memo = 0;
    reverse(nowVec.begin(), nowVec.end());
    vector<long long int> ans(nowVec.size());
    for (int i = 0; i < N - 1; i++) {
      ans[i] = nowVec[i] - memo;
      memo += nowVec[i] - memo;
    }
    reverse(ans.begin(), ans.end());
    for (int i = 0; i < N - 1; i++) {
      cout << ans[i];
      if (i == N - 2) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
    return;
  }
  for (auto x : nowInput) {
    if (sum < x.first) {
      return;
    }
    if (x.second > 0) {
      auto nextVec = nowVec;
      auto nextInput = nowInput;
      nextInput[x.first]--;
      bool f = true;
      for (auto &y : nextVec) {
        y += x.first;
        if (nextInput[y] <= 0) {
          f = false;
          break;
        }
        nextInput[y]--;
      }
      if (f) {
        nextVec.push_back(x.first);
        saiki(nextVec, nextInput, sum - x.first);
      }
    }
  }
}

int main() {
  while (cin >> N, N) {
    map<long long int, long long int> M;
    long long int sum;
    for (int i = 0; i < N * (N - 1) / 2; i++) {
      long long int x;
      cin >> x;
      M[x]++;
      if (i == 0)
        sum = x;
    }
    saiki(vector<long long int>(), M, sum);
    cout << "-----" << endl;
  }
}