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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXM = 4e2 + 5;
const int MAXN = 26 + 5;
const ll linf = 0x3f3f3f3f3f3f3f3f;

int n = 26, m;
int c[MAXN];
int a[MAXM][MAXN];

int p[MAXM];
set<int> idx[MAXN];
ll ans;

void init(void) {
  for (int i = 1; i <= n; ++i) {
    idx[i].clear();
    idx[i].insert(0);
    idx[i].insert(m + 1);
  }

  for (int k = 1; k <= m; ++k)
    idx[p[k]].insert(k);
}

ll recalc(int pos, int x) {
  int old = p[pos];
  if (old == x)
    return ans;

  ll res = ans;

  idx[old].erase(pos);

  set<int>::iterator it = idx[old].upper_bound(pos);
  int nxt = *it;
  int lst = *(--it);

  res -= (pos - lst) * c[old] * (nxt - pos);
  res -= a[pos][old];

  it = idx[x].upper_bound(pos);
  nxt = *it;
  lst = *(--it);

  res += (pos - lst) * c[x] * (nxt - pos);
  res += a[pos][x];

  idx[x].insert(pos);

  p[pos] = x;
  return res;
}

int main(void) {
  srand(time(0));

  scanf("%d", &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= n; ++j)
      scanf("%d", &a[i][j]);

  static int lst[MAXN];

  ans = 0;
  for (int k = 1; k <= m; ++k) {
    ll mx = -linf;
    int mxi = -1;
    for (int i = 1; i <= n; ++i) {
      ll res = a[k][i];
      for (int j = 1; j <= n; ++j)
        if (j != i)
          res -= c[j] * (k - lst[j]);

      if (mxi == -1 || mx < res)
        mxi = i, mx = res;
    }
    lst[mxi] = k;

    ans += mx;
    p[k] = mxi;
  }

  init();

  int T = 1e6;
  while (T--) {
    int pos = rand() % m + 1;
    int x = rand() % n + 1;

    int old = p[pos];
    ll res = recalc(pos, x);
    if (res < ans)
      recalc(pos, old);
    else
      ans = res;
  }

  for (int i = 1; i <= m; ++i)
    printf("%d\\n", p[i]);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXM = 4e2 + 5;
const int MAXN = 26 + 5;
const ll linf = 0x3f3f3f3f3f3f3f3f;

int n = 26, m;
int c[MAXN];
int a[MAXM][MAXN];

int p[MAXM];
set<int> idx[MAXN];
ll ans;

ll solve(void) {
  static int lst[MAXN];
  memset(lst, 0, sizeof(lst));
  for (int i = 1; i <= n; ++i) {
    idx[i].clear();
    idx[i].insert(0);
    idx[i].insert(m + 1);
  }

  ll ans = 0;
  for (int k = 1; k <= m; ++k) {
    int x = p[k];
    ans += a[k][x];
    for (int i = 1; i <= n; ++i)
      if (i != x)
        ans -= c[i] * (k - lst[i]);
    lst[x] = k;

    idx[x].insert(k);
  }

  ::ans = ans;
  return ans;
}

ll recalc(int pos, int x) {
  int old = p[pos];
  if (old == x)
    return ans;

  ll res = ans;

  idx[old].erase(pos);

  set<int>::iterator it = idx[old].upper_bound(pos);
  int nxt = *it;
  int lst = *(--it);

  res -= (pos - lst) * c[old] * (nxt - pos);
  res -= a[pos][old];

  it = idx[x].upper_bound(pos);
  nxt = *it;
  lst = *(--it);

  res += (pos - lst) * c[x] * (nxt - pos);
  res += a[pos][x];

  idx[x].insert(pos);

  p[pos] = x;
  return res;
}

int main(void) {
  srand(time(0));

  scanf("%d", &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= n; ++j)
      scanf("%d", &a[i][j]);
  for (int i = 1; i <= m; ++i)
    scanf("%d", &p[i]);

  solve();

  int T = 1e6;
  while (T--) {
    int pos = rand() % m + 1;
    int x = rand() % n + 1;

    int old = p[x];
    ll res = recalc(pos, x);
    if (res < ans)
      recalc(pos, old);
    else
      ans = res;
  }

  for (int i = 1; i <= m; ++i)
    printf("%d\\n", p[i]);
  return 0;
}