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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int INF = 1e9;

class C {
public:
  int n, w, m;
  C() {}
  C(int _n, int _w, int _m) {
    n = _n;
    w = _w;
    m = _m;
  }
  bool operator>(const C &c) const {
    if (w != c.w)
      return w > c.w;
    return m < c.m;
  }
};

int main() {
  int M, N;
  while (cin >> M >> N && M) {
    vector<int> v(N + 1);
    FOR(i, 1, N + 1) cin >> v[i];
    FOR(i, 1, v.size()) v[i] += v[i - 1];
    priority_queue<C, vector<C>, greater<C>> open;
    open.push(C(1, 0, 0));
    vector<vector<int>> closed(N + 1, vector<int>(M + 1, INF));
    closed[1][0] = 0;
    while (!open.empty()) {
      C tmp = open.top();
      open.pop();
      int n = tmp.n, w = tmp.w, m = tmp.m;
      // cout <<"n = " <<n <<", w = " <<w <<", m = " <<m <<endl;
      if (m == M) {
        cout << w << endl;
        break;
      }
      if (n == N && m == M) {
        cout << w << endl;
        break;
      }
      if (m + 1 == M) {
        int nw = v[N] - v[n - 1];
        if (closed[N][m + 1] <= nw)
          open.push(C(N, max(w, nw), m + 1));
        open.push(C(N, max(w, nw), m + 1));
        continue;
      }
      FOR(next, n, N + 1) {
        int nw = v[next] - v[n - 1];
        // cout <<"    " <<next + 1 <<", " <<max(w, nw) <<", " <<m + 1 <<" | "
        // <<closed[next][m + 1] <<", " <<max(w, nw) <<endl;
        if (closed[next][m + 1] <= max(w, nw))
          continue;
        // cout <<"    " <<"!" <<endl;
        closed[next][m + 1] = nw;
        open.push(C(next + 1, max(w, nw), m + 1));
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int INF = 1e9;

class C {
public:
  int n, w, m;
  C() {}
  C(int _n, int _w, int _m) {
    n = _n;
    w = _w;
    m = _m;
  }
  bool operator>(const C &c) const {
    if (w != c.w)
      return w > c.w;
    return m < c.m;
  }
};

int main() {
  int M, N;
  while (cin >> M >> N && M) {
    vector<int> v(N + 1);
    FOR(i, 1, N + 1) cin >> v[i];
    FOR(i, 1, v.size()) v[i] += v[i - 1];
    priority_queue<C, vector<C>, greater<C>> open;
    open.push(C(1, 0, 0));
    while (!open.empty()) {
      C tmp = open.top();
      open.pop();
      int n = tmp.n, w = tmp.w, m = tmp.m;
      if (n == N && m == M) {
        cout << w << endl;
        break;
      }
      if (m + 1 == M) {
        int nw = v[N] - v[n - 1];
        open.push(C(N, max(w, nw), m + 1));
        continue;
      }
      FOR(next, n, N + 1) {
        int nw = v[next] - v[n - 1];
        open.push(C(next + 1, max(w, nw), m + 1));
      }
    }
  }
  return 0;
}