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

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define all(a) a.begin(), a.end()

const int MAXN = 2050;

int n;
pair<int, int> a[MAXN];
long long dp[MAXN][MAXN];

long long calc(int pos, int l) {
  long long &res = dp[pos][l];
  if (res != -1)
    return res;

  res = 0;
  if (pos == n)
    return res;

  int r = n - 1 - (pos - l);
  res = 1ll * a[pos].first * abs(a[pos].second - l) + calc(pos + 1, l + 1);
  res =
      max(res, 1ll * a[pos].first * abs(a[pos].second - r) + calc(pos + 1, l));

  return res;
}

int main() {

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i].first);
    a[i].second = i;
  }

  sort(a, a + n);
  reverse(a, a + n);

  memset(dp, -1, sizeof(dp));
  cout << calc(0, 0) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define all(a) a.begin(), a.end()

const int MAXN = 1050;
const long long INF = (long long)1e15;

struct edge {
  int from, to;
  int flow, cap;
  long long cost;
};

int n;
vector<edge> e;
vector<int> g[MAXN];
long long phi[MAXN];
long long dist[MAXN];
bool used[MAXN];
int par[MAXN];
int edge_num;
int s = 0, t = MAXN - 1;

void fordBellman() {
  for (int i = s; i <= t; i++)
    dist[i] = INF;
  dist[s] = 0;
  while (true) {
    bool change = false;
    for (int i = 0; i < edge_num; i++) {
      int from = e[i].from, to = e[i].to;
      if (e[i].flow == e[i].cap)
        continue;
      if (dist[from] == INF)
        continue;
      if (dist[to] > dist[from] + e[i].cost) {
        dist[to] = dist[from] + e[i].cost;
        change = true;
      }
    }
    if (!change)
      break;
  }
}

void dijkstra() {
  for (int i = s; i <= t; i++) {
    dist[i] = INF;
    used[i] = false;
  }
  dist[s] = 0;

  for (int i = s; i <= t; i++) {
    int cur = -1;
    for (int j = s; j <= t; j++)
      if (!used[j])
        if (cur == -1 || dist[j] < dist[cur])
          cur = j;

    used[cur] = true;

    for (int j = 0; j < (int)g[cur].size(); j++) {
      int ind = g[cur][j];
      if (e[ind].flow == e[ind].cap)
        continue;
      int to = e[ind].to;
      int w = e[ind].cost + phi[cur] - phi[to];
      if (dist[cur] + w < dist[to]) {
        dist[to] = dist[cur] + w;
        par[to] = ind;
      }
    }
  }
}

long long minCost(int flow) {
  long long result = 0;

  fordBellman();
  for (int i = s; i <= t; i++)
    phi[i] = dist[i];

  while (true) {

    dijkstra();

    if (dist[t] == INF)
      return result;

    for (int i = s; i <= t; i++)
      phi[i] = min(phi[i] + dist[i], INF);

    int push = flow;
    int cur = t;
    while (cur != s) {
      edge tmp = e[par[cur]];
      int from = tmp.from, can_push = tmp.cap - tmp.flow;
      push = min(push, can_push);
      cur = from;
    }

    flow -= push;
    cur = t;
    while (cur != s) {
      edge tmp = e[par[cur]];
      int from = tmp.from;
      e[par[cur]].flow += push;
      e[par[cur] ^ 1].flow -= push;
      result += 1ll * push * tmp.cost;
      cur = from;
    }

    if (flow == 0)
      break;
  }
  return result;
}

void addEdge(int from, int to, int cap, long long cost) {
  edge tmp;
  tmp.from = from;
  tmp.to = to;
  tmp.flow = 0;
  tmp.cap = cap;
  tmp.cost = cost;
  e.push_back(tmp);
  g[from].push_back(edge_num);

  tmp.from = to;
  tmp.to = from;
  tmp.flow = cap;
  tmp.cap = cap;
  tmp.cost = -cost;
  e.push_back(tmp);
  g[to].push_back(edge_num + 1);

  edge_num += 2;
}

int main() {

  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  s = 0;
  t = 2 * n + 1;

  for (int i = 1; i <= n; i++)
    addEdge(s, i, 1, 0);

  for (int i = n + 1; i <= 2 * n; i++)
    addEdge(i, t, 1, 0);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      addEdge(i + 1, n + j + 1, 1, -1ll * a[i] * abs(i - j));

  cout << -minCost(n) << endl;

  return 0;
}