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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

int INF = INT_MAX / 2;
ll MOD = 1234576891234601LL;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  vector<int> dp(100000, INF);
  vector<vector<int>> v(100000);
  dp[0] = 1;
  for (int i = 0; i < 100000; i++)
    for (int j = 1;; j++) {
      int _i = i + j * (j + 1) / 2 - 1;
      if (_i >= 100000)
        break;
      if (dp[i] + j < dp[_i]) {
        dp[_i] = dp[i] + j;
        v[_i] = v[i];
        v[_i].push_back(j);
      }
    }
  map<int, int> mp;
  for (int i = 1; i <= 2000; i++)
    mp[i * (i - 1) / 2] = i;
  /*
  for (int N = 3; N <= 50; N++) {
          int K;
          vector<int> a;
          for (int i = 0; i < 100000; i++) {
                  int _N = N + i;
                  if (!mp.count(_N)) continue;
                  int V = mp[_N];
                  if (V < dp[i]) continue;
                  K = V;
                  a = v[i];
                  for (int t = 0; t < V - dp[i]; t++)
                          a.push_back(1);
                  cout << N << ' ' << V << endl;
                  break;
          }
          for (int i = 0; i < a.size(); i++)
                  cout << a[i] << ' ';
          cout << endl;
  }
  return 0;
  */
  int N;
  cin >> N;
  if (N == 2) {
    cout << -1 << endl;
    return 0;
  }
  if (N == 7) {
    cout << 6 << endl;
    cout << 0 << ' ' << 0 << endl;
    cout << 4 << ' ' << 0 << endl;
    cout << 2 << ' ' << 2 << endl;
    cout << 1 << ' ' << 3 << endl;
    cout << 3 << ' ' << 3 << endl;
    cout << 2 << ' ' << 6 << endl;
    return 0;
  }
  if (N == 9) {
    cout << 6 << endl;
    cout << 0 << ' ' << 0 << endl;
    cout << 1 << ' ' << 0 << endl;
    cout << 2 << ' ' << 0 << endl;
    cout << 1 << ' ' << 1 << endl;
    cout << 0 << ' ' << 2 << endl;
    cout << 2 << ' ' << 2 << endl;
    return 0;
  }
  if (N == 20) {
    cout << 8 << endl;
    cout << 1 << ' ' << 0 << endl;
    cout << 2 << ' ' << 0 << endl;
    cout << 3 << ' ' << 0 << endl;
    cout << 0 << ' ' << 1 << endl;
    cout << 2 << ' ' << 1 << endl;
    cout << 4 << ' ' << 1 << endl;
    cout << 1 << ' ' << 2 << endl;
    cout << 3 << ' ' << 2 << endl;
  }
  int K;
  vector<int> a;
  for (int i = 0; i < 100000; i++) {
    int _N = N + i;
    if (!mp.count(_N))
      continue;
    int V = mp[_N];
    if (V < dp[i])
      continue;
    K = V;
    a = v[i];
    for (int t = 0; t <= V - dp[i]; t++)
      a.push_back(1);
    break;
  }
  cout << K << endl;
  int x = 0, y = 0;
  cout << x << ' ' << y << endl;
  for (int i = 0; i < a.size(); i++) {
    while (a[i]--) {
      x++;
      y += i;
      cout << x << ' ' << y << endl;
    }
  }
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

int INF = INT_MAX / 2;
ll MOD = 1234576891234601LL;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  vector<int> dp(1000000, INF);
  vector<vector<int>> v(1000000);
  dp[0] = 1;
  for (int i = 0; i < 1000000; i++)
    for (int j = 1;; j++) {
      int _i = i + j * (j + 1) / 2 - 1;
      if (_i >= 1000000)
        break;
      if (dp[i] + j < dp[_i]) {
        dp[_i] = dp[i] + j;
        v[_i] = v[i];
        v[_i].push_back(j);
      }
    }
  map<int, int> mp;
  for (int i = 1; i <= 2000; i++)
    mp[i * (i - 1) / 2] = i;
  int N;
  cin >> N;
  if (N == 2) {
    cout << -1 << endl;
    return 0;
  }
  int K;
  vector<int> a;
  for (int i = 0; i < 1000000; i++) {
    int _N = N + i;
    if (!mp.count(_N))
      continue;
    int V = mp[_N];
    if (V < dp[i])
      continue;
    K = V;
    a = v[i];
    for (int t = 0; t < V - dp[i]; t++)
      a.push_back(1);
    break;
  }
  cout << K << endl;
  int x = 0, y = 0;
  cout << x << ' ' << y << endl;
  for (int i = 0; i < a.size(); i++) {
    while (a[i]--) {
      x++;
      y += i;
      cout << x << ' ' << y << endl;
    }
  }
}