Problem p03883 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
long double ABS(long double a) { return max(a, -a); }
long long L[5100];
long long R[5100];
long long l[5100];
long long r[5100];
pair<long long, int> p[5100];
long long dp[5100][5100][2];
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++)
    scanf("%lld%lld", L + i, R + i);
  for (int i = 0; i < a; i++) {
    p[i] = make_pair(R[i] + L[i], i);
  }
  std::sort(p, p + a);
  for (int i = 0; i < a; i++) {
    l[i] = L[p[i].second];
    r[i] = R[p[i].second];
  }
  for (int i = 0; i < 5100; i++)
    for (int j = 0; j < 5100; j++)
      dp[i][j][0] = dp[i][j][1] = inf;
  dp[0][0][0] = 0;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j <= min(i, a / 2); j++) {
      for (int k = 0; k < 2; k++) {
        if (dp[i][j][k] == inf)
          continue;
        if (k == 0) {
          dp[i + 1][j][k + 1] =
              min(dp[i + 1][j][k + 1], dp[i][j][k] + (l[i] + r[i]) * (a / 2));
        }
        dp[i + 1][j + 1][k] =
            min(dp[i + 1][j + 1][k],
                dp[i][j][k] + l[i] * (a / 2 - j) + r[i] * (a / 2 - j - 1));
        dp[i + 1][j][k] = min(dp[i + 1][j][k],
                              dp[i][j][k] + l[i] * (a / 2 - (i - j - k) - 1) +
                                  r[i] * (a / 2 - (i - j - k)));
      }
    }
  }
  long long ret = dp[a][a / 2][a % 2];
  printf("%lld\n", ret);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
long double ABS(long double a) { return max(a, -a); }
long long L[5100];
long long R[5100];
long long l[5100];
long long r[5100];
pair<long long, int> p[5100];
long long dp[5100][2600][2];
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < a; i++)
    scanf("%lld%lld", L + i, R + i);
  for (int i = 0; i < a; i++) {
    p[i] = make_pair(R[i] + L[i], i);
  }
  std::sort(p, p + a);
  for (int i = 0; i < a; i++) {
    l[i] = L[p[i].second];
    r[i] = R[p[i].second];
  }
  for (int i = 0; i < 5100; i++)
    for (int j = 0; j < 5100; j++)
      dp[i][j][0] = dp[i][j][1] = inf;
  dp[0][0][0] = 0;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j <= min(i, a / 2); j++) {
      for (int k = 0; k < 2; k++) {
        if (dp[i][j][k] == inf)
          continue;
        if (k == 0) {
          dp[i + 1][j][k + 1] =
              min(dp[i + 1][j][k + 1], dp[i][j][k] + (l[i] + r[i]) * (a / 2));
        }
        dp[i + 1][j + 1][k] =
            min(dp[i + 1][j + 1][k],
                dp[i][j][k] + l[i] * (a / 2 - j) + r[i] * (a / 2 - j - 1));
        dp[i + 1][j][k] = min(dp[i + 1][j][k],
                              dp[i][j][k] + l[i] * (a / 2 - (i - j - k) - 1) +
                                  r[i] * (a / 2 - (i - j - k)));
      }
    }
  }
  long long ret = dp[a][a / 2][a % 2];
  printf("%lld\n", ret);
}