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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
int n, m, h, K;
int score[1005];
int ti[1005];
vector<pair<int, bool>> s[1005][1005];
int main() {
  while (1) {
    for (int i = 0; i < 1005; i++) {
      for (int j = 0; j < 1005; j++) {
        s[i][j].resize(0);
      }
    }
    vector<P> tate2[1005];
    scanf("%d %d %d %d", &n, &m, &h, &K);
    if (!n)
      break;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &score[i]);
    }
    for (int i = 1; i <= m; i++) {
      int r, p;
      scanf("%d %d", &r, &p);
      tate2[r].pb(mp(p, r + 1));
      tate2[r + 1].pb(mp(p, r));
    }
    for (int i = 1; i <= n; i++) {
      sort(tate2[i].begin(), tate2[i].end());
    }
    for (int i = 1; i <= n; i++) {
      int now = i;
      for (int k = 1; k <= h; k++) {
        for (int op = 0; op < tate2[now].size(); op++) {
          if (tate2[now][op] == mp(k, now - 1)) {
            s[now - 1][k].pb(mp(i, false));
            now--;
            break;
          } else if (tate2[now][op] == mp(k, now + 1)) {
            s[now][k].pb(mp(i, true));
            now++;
            break;
          }
        }
      }
      ti[i] = score[now];
    }
    int maxi = 0;
    for (int i = 0; i < 1005; i++) {
      for (int j = 0; j < 1005; j++) {
        int z;
        z = (int)s[i][j].size();
        if (!z)
          continue;
        int ax = 0, bx = 0;
        for (int k = 0; k < z; k++) {
          if (s[i][j][k].second) {
            ax = ti[s[i][j][k].first];
          } else {
            bx = ti[s[i][j][k].first];
          }
          if (ax && bx)
            break;
        }
        int dif = 0;
        int rp;
        for (int ww = 0; ww < s[i][j].size(); ww++) {
          if (s[i][j][ww].first <= K && s[i][j][ww].second) {
            dif += (ax - bx);
          } else if (s[i][j][ww].first <= K) {
            dif += (bx - ax);
          }
        }
        maxi = max(maxi, dif);
      }
    }
    int ans = 0;
    for (int i = 1; i <= K; i++) {
      ans += ti[i];
    }
    ans -= maxi;
    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define pb push_back
#define mp make_pair
int n, m, h, k;
vector<short> s[1005][1005][2];
int main() {
  while (1) {
    for (int i = 0; i < 1005; i++) {
      for (int j = 0; j < 1005; j++) {
        s[i][j][0].clear();
        s[i][j][1].clear();
      }
    }
    int score[1005];
    int ti[1005];
    vector<P> tate2[1005];
    scanf("%d %d %d %d", &n, &m, &h, &k);
    if (!n)
      break;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &score[i]);
    }
    for (int i = 1; i <= m; i++) {
      int r, p;
      scanf("%d %d", &r, &p);
      tate2[r].pb(mp(p, r + 1));
      tate2[r + 1].pb(mp(p, r));
    }
    for (int i = 1; i <= n; i++) {
      sort(tate2[i].begin(), tate2[i].end());
    }
    for (int i = 1; i <= n; i++) {
      int now = i;
      for (int k = 1; k <= h; k++) {
        for (int op = 0; op < tate2[now].size(); op++) {
          if (tate2[now][op] == mp(k, now - 1)) {
            s[now - 1][k][1].pb((short)i);
            now--;
            break;
          } else if (tate2[now][op] == mp(k, now + 1)) {
            s[now][k][0].pb((short)i);
            now++;
            break;
          }
        }
      }
      ti[i] = score[now];
    }
    int maxi = 0;
    for (int i = 0; i < 1005; i++) {
      for (int j = 0; j < 1005; j++) {
        int z, w;
        z = (int)s[i][j][0].size();
        w = (int)s[i][j][1].size();
        if (!(z + w)) {
          s[i][j][0].resize(0);
          s[i][j][1].resize(0);
        }
        int ax = 0, bx = 0;
        if (s[i][j][0].size() != 0) {
          ax = ti[s[i][j][0][0]];
        }
        if (s[i][j][1].size() != 0) {
          bx = ti[s[i][j][1][0]];
        }
        int dif = 0;
        for (int ww = 0; ww < s[i][j][0].size(); ww++) {
          if (s[i][j][0][ww] <= k) {
            dif += (ax - bx);
          }
        }
        for (int ww = 0; ww < s[i][j][1].size(); ww++) {
          if (s[i][j][1][ww] <= k) {
            dif += (bx - ax);
          }
        }
        maxi = max(maxi, dif);
      }
    }
    int ans = 0;
    for (int i = 1; i <= k; i++) {
      ans += ti[i];
    }
    ans -= maxi;
    printf("%d\\n", ans);
  }
}