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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(I, N) for (int I = 0; I < (int)(N); I++)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int N, M, p;
int ans = 1000000;
vector<int> v;

int main(void) {
  cin >> N >> M >> p;
  rep(i, M) {
    int a;
    cin >> a;
    //??¶??????????????¢
    a = (N + a - p) % N;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  rep(i, M) {
    //?????????
    if (i == 0) {
      ans = min(ans, N - v[i]);
    } else {
      ans = min(ans, 2 * (N - v[i]) + v[i - 1]);
    }
    //?????????
    if (i == M - 1) {
      ans = min(ans, v[i]);
    } else {
      ans = min(ans, 2 * v[i] - (v[i + 1]) + N);
    }
  }
  cout << ans * 100 << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(I, N) for (int I = 0; I < (int)(N); I++)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int N, M, NOW;
int d[10001];
struct Data {
  int cost, pos, turn;
  Data(int _cost, int _pos, int _turn) {
    cost = _cost;
    pos = _pos;
    turn = _turn;
  }
  Data() {}
};
int StrToInt(string s) {
  stringstream ss;
  ss << s;
  int val;
  ss >> val;
  return val;
}
string IntToStr(int i) {
  stringstream ss;
  ss << i;
  return ss.str();
}
string StrEra(string S, int i) {
  string L = IntToStr(i);
  int a = S.find(L);
  S.erase(a, 1);
  return S;
}

typedef pair<Data, vector<int>> vD;
int main(void) {
  cin >> N >> M >> NOW;
  vector<int> v;
  fill_n(d, 10001, -1);
  rep(i, M) {
    int a;
    cin >> a;
    d[a] = a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  queue<vD> que;
  que.push(vD(Data(0, NOW, 0), v));
  int mini = 1000000000;
  while (!que.empty()) {
    vD data = que.front();
    que.pop();
    int pos = data.first.pos;
    int cost = data.first.cost;
    int turn = data.first.turn;
    // cout << "pos:" << pos << endl;
    vector<int> now = data.second;
    if (mini <= cost)
      continue;
    if (now.empty()) {
      mini = min(mini, cost);
    }
    int cnt = 1;
    int next_cost;
    vector<int> backup = now;
    if (turn == 0 || turn == 1) {
      int p_pos = pos - 1;
      if (p_pos == -1)
        p_pos = N - 1;
      while (1) {
        if (d[p_pos] != -1)
          break;
        cnt++;
        p_pos--;
        if (p_pos == -1)
          p_pos = N - 1;
      }
      next_cost = cost + (cnt * 100);
      // cout << "p_pos: " << p_pos << endl;
      // cout << "next_cost:" << next_cost << endl;
      now.erase(remove(now.begin(), now.end(), d[p_pos]), now.end());
      que.push(vD(Data(next_cost, p_pos, turn), now));
      if (turn == 0) {
        que.push(vD(Data(next_cost, p_pos, 1), now));
      }
    }
    if (turn == 0 || turn == 2) {
      int n_pos = pos + 1;
      cnt = 1;
      if (n_pos == N)
        n_pos = 0;
      while (1) {
        if (d[n_pos] != -1)
          break;
        cnt++;
        n_pos++;
        if (n_pos == N)
          n_pos = 0;
      }
      next_cost = cost + (cnt * 100);
      // cout << "n_pos: " << n_pos << endl;
      // cout << "next_cost:" << next_cost << endl;
      backup.erase(remove(backup.begin(), backup.end(), d[n_pos]),
                   backup.end());
      que.push(vD(Data(next_cost, n_pos, turn), backup));
      if (turn == 2) {
        que.push(vD(Data(next_cost, n_pos, turn), backup));
      }
    }
  }
  FIN(mini);

  return 0;
}

/*

v.erase(remove(v.begin(), v.end(), 3), v.end());

  string str( "abcdefghijk" );
  char key = 'h';
  cout << str.find(key) << endl;???// 7

        string str( "abcdefghijk" );
        char key = 'z';

        if( str.find(key) == string::npos ) {
          cout << "Not Found" << endl;
        }
str.erase(str.begin()+3);    // "abcefghijk"

*/