Problem p04006 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

typedef long long ll;
#define mop 1000000007
using namespace std;

static const ll MAX = 2001;
ll dp[MAX][MAX] = {};

int main() {

  ll n, x;
  cin >> n >> x;
  vector<ll> a;
  for (ll i = 0; i < n; i++) {
    ll p;
    cin >> p;
    a.push_back(p);
  }
  for (ll j = 0; j < n; j++) {
    dp[1][j] = a[j];
    if (j) {
      dp[2][j] = min(a[j], a[j - 1]);
    } else {
      dp[2][j] = min(a[j], a[n - 1]);
    }
  }
  for (ll i = 2; i <= n; i++) {
    for (ll j = 0; j < n; j++) {
      if (j - 1 >= 0 and j - i >= 0) {
        dp[i + 1][j] = min(dp[i - 1][j - 1], min(dp[1][j], dp[1][j - i]));
      } else if (j - i >= 0) {
        dp[i + 1][j] = min(dp[i - 1][j - 1 + n], min(dp[1][j], dp[1][j - i]));
      } else if (j - 1 >= 0) {
        dp[i + 1][j] = min(dp[i - 1][j - 1], min(dp[1][j], dp[1][j - i + n]));
      } else {
        dp[i + 1][j] =
            min(dp[i - 1][j - 1 + n], min(dp[1][j], dp[1][j - i + n]));
      }
    }
  }
  ll ans;
  for (ll num_magic = 0; num_magic < n; num_magic++) {
    ll pre = 0;
    for (ll j = 0; j < n; j++) {
      pre += dp[num_magic + 1][j];
      //            cout << dp[num_magic+1][j] << " ";
    }
    //        cout << endl;
    pre += num_magic * x;
    //        cout << pre << endl;
    if (num_magic == 0) {
      ans = pre;
    } else {
      if (ans > pre) {
        ans = pre;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

typedef long long ll;
#define mop 1000000007
using namespace std;

static const ll MAX = 2002;
ll dp[MAX][MAX] = {};

int main() {

  ll n, x;
  cin >> n >> x;
  vector<ll> a;
  for (ll i = 0; i < n; i++) {
    ll p;
    cin >> p;
    a.push_back(p);
  }
  for (ll j = 0; j < n; j++) {
    dp[1][j] = a[j];
    if (j) {
      dp[2][j] = min(a[j], a[j - 1]);
    } else {
      dp[2][j] = min(a[j], a[n - 1]);
    }
  }
  for (ll i = 2; i <= n; i++) {
    for (ll j = 0; j < n; j++) {
      if (j - 1 >= 0 and j - i >= 0) {
        dp[i + 1][j] = min(dp[i - 1][j - 1], min(dp[1][j], dp[1][j - i]));
      } else if (j - i >= 0) {
        dp[i + 1][j] = min(dp[i - 1][j - 1 + n], min(dp[1][j], dp[1][j - i]));
      } else if (j - 1 >= 0) {
        dp[i + 1][j] = min(dp[i - 1][j - 1], min(dp[1][j], dp[1][j - i + n]));
      } else {
        dp[i + 1][j] =
            min(dp[i - 1][j - 1 + n], min(dp[1][j], dp[1][j - i + n]));
      }
    }
  }
  ll ans;
  for (ll num_magic = 0; num_magic < n; num_magic++) {
    ll pre = 0;
    for (ll j = 0; j < n; j++) {
      pre += dp[num_magic + 1][j];
      //            cout << dp[num_magic+1][j] << " ";
    }
    //        cout << endl;
    pre += num_magic * x;
    //        cout << pre << endl;
    if (num_magic == 0) {
      ans = pre;
    } else {
      if (ans > pre) {
        ans = pre;
      }
    }
  }
  cout << ans << endl;
}