Problem p02709 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define name ""
#define ini                                                                    \
  freopen(name ".inp", "r", stdin);                                            \
  freopen(name ".out", "w", stdout)
#define foe(it, c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
const int N = 2001;

int n;
long dp[N][N];
pll a[N];

int main() {
  fastio;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a + 1, a + 1 + n);
  reverse(a + 1, a + 1 + n);
  long res = 0;
  for (int x = 0; x <= n; x++) {
    for (int y = 0; y <= n; y++) {
      int cur = x + y;
      if (cur == n)
        res = max(res, dp[x][y]);
      else {
        cur++;
        dp[x + 1][y] =
            max(dp[x + 1][y],
                dp[x][y] + abs(a[cur].second - (x + 1)) * a[cur].first);
        dp[x][y + 1] =
            max(dp[x][y + 1],
                dp[x][y] + abs(a[cur].second - (n - y)) * a[cur].first);
        if (x == 0 && y == 0) {
        }
      }
    }
  }
  cout << res;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define name ""
#define ini                                                                    \
  freopen(name ".inp", "r", stdin);                                            \
  freopen(name ".out", "w", stdout)
#define foe(it, c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
const int N = 2002;

int n;
long dp[N][N];
pll a[N];

int main() {
  fastio;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a + 1, a + 1 + n);
  reverse(a + 1, a + 1 + n);
  long res = 0;
  for (int x = 0; x <= n; x++) {
    for (int y = 0; y <= n; y++) {
      int cur = x + y;
      if (cur == n)
        res = max(res, dp[x][y]);
      else {
        cur++;
        dp[x + 1][y] =
            max(dp[x + 1][y],
                dp[x][y] + abs(a[cur].second - (x + 1)) * a[cur].first);
        dp[x][y + 1] =
            max(dp[x][y + 1],
                dp[x][y] + abs(a[cur].second - (n - y)) * a[cur].first);
        if (x == 0 && y == 0) {
        }
      }
    }
  }
  cout << res;
  return 0;
}