Problem p03873 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long int int64;

struct Seg {
  long long l, r;
  Seg() : l(), r() {}

  void read() { scanf("%lld%lld", &l, &r); }

  bool operator<(const Seg &A) const {
    if (r + l != A.r + A.l)
      return r + l < A.r + A.l;
    return l < A.l;
  }
};

const int N = 5005;
Seg s[N];

long long dp[N / 2][N / 2][2];

long long INF = (long long)1e18;
void updDp(long long &a, long long b) { a = min(a, b); }

int main(int, char **) {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    s[i].read();
  sort(s, s + n);

  for (int i = 0; i <= n / 2; i++)
    for (int j = 0; j <= n / 2; j++)
      for (int t = 0; t < 2; t++)
        dp[i][j][t] = INF;

  //	int ptr = 0;
  //	while (ptr < n && s[ptr + 1].l + s[ptr + 1].r == s[ptr].l + s[ptr].r)
  //		ptr++;
  //	swap(s[0], s[ptr / 2] );
  //	int sti = 0;
  long long std = 0;
  //	if (n % 2 == 1)
  //	{
  //		sti = 1;
  //		std = (n / 2) * 1LL * (s[0].l + s[0].r);
  //	}
  dp[n / 2][n / 2][n % 2] = std;

  for (int i = 0; i < n; i++) {
    for (int rl = 0; rl <= n / 2; rl++) {
      for (int t = 0; t < 2; t++) {
        int rr = n - i - rl - t;
        if (rr < 0 || rr > n / 2)
          continue;

        if (rl != 0)
          updDp(dp[rl - 1][rr][t],
                (rl - 1) * 1LL * (s[i].l + s[i].r) + s[i].r + dp[rl][rr][t]);
        if (rr != 0)
          updDp(dp[rl][rr - 1][t],
                (rr - 1) * 1LL * (s[i].l + s[i].r) + s[i].l + dp[rl][rr][t]);
        if (t == 1)
          updDp(dp[rl][rr][0],
                dp[rl][rr][1] + (n / 2) * 1LL * (s[i].l + s[i].r));
      }
    }
  }
  long long ans = dp[0][0][0];
  printf("%lld\\n", ans);

  return 0;
}


Error: 127
/tmp/df169ced-d384-4667-bb4e-03a1f15cb701.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

typedef long long int int64;

struct Seg {
  long long l, r;
  Seg() : l(), r() {}

  void read() { scanf("%lld%lld", &l, &r); }

  bool operator<(const Seg &A) const {
    if (r + l != A.r + A.l)
      return r + l < A.r + A.l;
    return l < A.l;
  }
};

const int N = 5005;
Seg s[N];

long long dp[N / 2][N / 2][2];

long long INF = (long long)1e18;
void updDp(long long &a, long long b) { a = min(a, b); }

int main(int, char **) {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    s[i].read();
  sort(s, s + n);

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      for (int t = 0; t < 2; t++)
        dp[i][j][t] = INF;

  //	int ptr = 0;
  //	while (ptr < n && s[ptr + 1].l + s[ptr + 1].r == s[ptr].l + s[ptr].r)
  //		ptr++;
  //	swap(s[0], s[ptr / 2] );
  //	int sti = 0;
  long long std = 0;
  //	if (n % 2 == 1)
  //	{
  //		sti = 1;
  //		std = (n / 2) * 1LL * (s[0].l + s[0].r);
  //	}
  dp[n / 2][n / 2][n % 2] = std;

  for (int i = 0; i < n; i++) {
    for (int rl = 0; rl <= n / 2; rl++) {
      for (int t = 0; t < 2; t++) {
        int rr = n - i - rl - t;
        if (rr < 0 || rr > n / 2)
          continue;

        if (rl != 0)
          updDp(dp[rl - 1][rr][t],
                (rl - 1) * 1LL * (s[i].l + s[i].r) + s[i].r + dp[rl][rr][t]);
        if (rr != 0)
          updDp(dp[rl][rr - 1][t],
                (rr - 1) * 1LL * (s[i].l + s[i].r) + s[i].l + dp[rl][rr][t]);
        if (t == 1)
          updDp(dp[rl][rr][0],
                dp[rl][rr][1] + (n / 2) * 1LL * (s[i].l + s[i].r));
      }
    }
  }
  long long ans = dp[0][0][0];
  printf("%lld\\n", ans);

  return 0;
}