Problem p00246 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct s {
  char a[6];
  s() {
    for (int i = 0; i < 6; i++)
      a[i] = 0;
  }
  bool operator==(const s &obj) const {
    bool res = true;
    for (int i = 0; i < 6; i++) {
      res &= a[i] == obj.a[i];
    }
    return res;
  }
  bool operator<(const s &obj) const {
    for (int i = 0; i < 6; i++) {
      if (a[i] < obj.a[i])
        return true;
      else if (a[i] > obj.a[i])
        return false;
    }
    return false;
  }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector
 v;
    vector<int> in;
    int sum = 0;
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      in.push_back(tmp);
      sum += tmp;
    }
    sort(in.rbegin(), in.rend());
    v.push_back(s());
    int res = 0;
    for (int k = 0; k < in.size(); k++) {
      int m;
      vector
 nex;
      m = in[k];
      for (int i = 0; i < v.size(); i++) {
        res = max(res, (int)v[i].a[5]);
        if (res - v[i].a[5] > sum / 10)
          continue;
        for (int j = 0; j + m <= 10; j++) {
          s p = v[i];
          if (j == 0)
            ;
          else if (j <= 5) {
            if (p.a[j - 1] <= 0)
              continue;
            p.a[j - 1]--;
          } else {
            if (p.a[10 - j - 1] >= 0)
              continue;
            p.a[10 - j - 1]++;
          }

          if (j + m <= 5) {
            p.a[j + m - 1]++;
          } else if (j + m < 10) {
            p.a[10 - (j + m) - 1]--;
          } else {
            p.a[5]++;
          }
          //					cout << j + m << endl;
          nex.push_back(p);
        }
      }
      sort(nex.begin(), nex.end());
      nex.erase(unique(nex.begin(), nex.end()), nex.end());
      v = nex;
      sum -= in[k];
    }
    for (int i = 0; i < v.size(); i++) {
      res = max(res, (int)v[i].a[5]);
    }
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct s {
  char a[6];
  s() {
    for (int i = 0; i < 6; i++)
      a[i] = 0;
  }
  bool operator==(const s &obj) const {
    bool res = true;
    for (int i = 0; i < 6; i++) {
      res &= a[i] == obj.a[i];
    }
    return res;
  }
  bool operator<(const s &obj) const {
    for (int i = 0; i < 6; i++) {
      if (a[i] < obj.a[i])
        return true;
      else if (a[i] > obj.a[i])
        return false;
    }
    return false;
  }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector
 v;
    v.push_back(s());
    int res = 0;
    for (int k = 0; k < n; k++) {
      int m;
      vector
 nex;
      cin >> m;
      for (int i = 0; i < v.size(); i++) {
        res = max(res, (int)v[i].a[5]);
        if (res > v[i].a[5] + (n - k))
          continue;
        for (int j = 0; j + m <= 10; j++) {
          s p = v[i];
          if (j == 0)
            ;
          else if (j <= 5) {
            if (p.a[j - 1] <= 0)
              continue;
            p.a[j - 1]--;
          } else {
            if (p.a[10 - j - 1] >= 0)
              continue;
            p.a[10 - j - 1]++;
          }

          if (j + m <= 5) {
            p.a[j + m - 1]++;
          } else if (j + m < 10) {
            p.a[10 - (j + m) - 1]--;
          } else {
            p.a[5]++;
          }
          //					cout << j + m << endl;
          nex.push_back(p);
        }
      }
      sort(nex.begin(), nex.end());
      nex.erase(unique(nex.begin(), nex.end()), nex.end());
      v = nex;
    }
    for (int i = 0; i < v.size(); i++) {
      res = max(res, (int)v[i].a[5]);
    }
    cout << res << endl;
  }
}